Skip to content

Instantly share code, notes, and snippets.

@slifin
Last active May 6, 2019 11:11
Show Gist options
  • Select an option

  • Save slifin/42c3a2d83ed6dea3023efd87bac96f4b to your computer and use it in GitHub Desktop.

Select an option

Save slifin/42c3a2d83ed6dea3023efd87bac96f4b to your computer and use it in GitHub Desktop.
honeysql clone for PHP
<?php
$data =
[':select' => [':a', ':b', ':c'],
':from' => [':foo']];
function recur(array $map) : object {
return new \RecursiveIteratorIterator(
new \RecursiveArrayIterator($map),
\RecursiveIteratorIterator::CHILD_FIRST
);
}
function map_to_sql(string $sql, array $map) : string {
$out = [];
$map = recur($map);
foreach ($map as $key => $result) {
switch (TRUE) {
case $key === ':select':
$out[] = 'SELECT ' . implode(', ', $result);
break;
case $key === ':from':
$out[] = 'FROM ' . implode(' ', $result);
break;
}
}
return implode("\n", $out);
}
$out = map_to_sql('', $data);
var_dump($out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment