Last active
May 6, 2019 11:11
-
-
Save slifin/42c3a2d83ed6dea3023efd87bac96f4b to your computer and use it in GitHub Desktop.
honeysql clone for PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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