aviat4ion /
Query
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php declare(strict_types=1); |
||
| 2 | /** |
||
| 3 | * Query |
||
| 4 | * |
||
| 5 | * SQL Query Builder / Database Abstraction Layer |
||
| 6 | * |
||
| 7 | * PHP version 7 |
||
| 8 | * |
||
| 9 | * @package Query |
||
| 10 | * @author Timothy J. Warren <[email protected]> |
||
| 11 | * @copyright 2012 - 2016 Timothy J. Warren |
||
| 12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
| 13 | * @link https://git.timshomepage.net/aviat4ion/Query |
||
| 14 | */ |
||
| 15 | |||
| 16 | |||
| 17 | use Query\ConnectionManager; |
||
| 18 | |||
| 19 | require __DIR__ . '/../vendor/autoload.php'; |
||
| 20 | |||
| 21 | // -------------------------------------------------------------------------- |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Global functions that don't really fit anywhere else |
||
| 25 | */ |
||
| 26 | |||
| 27 | if ( ! function_exists('mb_trim')) |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Multibyte-safe trim function |
||
| 31 | * |
||
| 32 | * @param string $string |
||
| 33 | * @return string |
||
| 34 | */ |
||
| 35 | function mb_trim(string $string): string |
||
| 36 | { |
||
| 37 | return preg_replace("/(^\s+)|(\s+$)/us", "", $string); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | // -------------------------------------------------------------------------- |
||
| 42 | |||
| 43 | if ( ! function_exists('db_filter')) |
||
| 44 | { |
||
| 45 | /** |
||
| 46 | * Filter out db rows into one array |
||
| 47 | * |
||
| 48 | * @param array $array |
||
| 49 | * @param mixed $index |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | function db_filter(array $array, $index): array |
||
| 53 | { |
||
| 54 | $new_array = []; |
||
| 55 | |||
| 56 | foreach($array as $a) |
||
| 57 | { |
||
| 58 | $new_array[] = $a[$index]; |
||
| 59 | } |
||
| 60 | |||
| 61 | return $new_array; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | // -------------------------------------------------------------------------- |
||
| 66 | |||
| 67 | if ( ! function_exists('from_camel_case')) |
||
| 68 | { |
||
| 69 | /** |
||
| 70 | * Create a snake_case string from camelCase |
||
| 71 | * |
||
| 72 | * @see http://stackoverflow.com/questions/1993721/how-to-convert-camelcase-to-camel-case |
||
| 73 | * |
||
| 74 | * @param string $input |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | function from_camel_case(string $input): string |
||
| 78 | { |
||
| 79 | preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); |
||
| 80 | $ret = $matches[0]; |
||
| 81 | foreach ($ret as &$match) { |
||
| 82 | $match = strtolower($match); |
||
| 83 | } |
||
| 84 | return implode('_', $ret); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | if ( ! function_exists('to_camel_case')) |
||
| 89 | { |
||
| 90 | /** |
||
| 91 | * Create a camelCase string from snake_case |
||
| 92 | * |
||
| 93 | * @param string $snake_case |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | function to_camel_case(string $snake_case): string |
||
| 97 | { |
||
| 98 | $pieces = explode('_', $snake_case); |
||
| 99 | |||
| 100 | $pieces[0] = mb_strtolower($pieces[0]); |
||
| 101 | for($i = 1; $i < count($pieces); $i++) |
||
|
0 ignored issues
–
show
|
|||
| 102 | { |
||
| 103 | $pieces[$i] = ucfirst(mb_strtolower($pieces[$i])); |
||
| 104 | } |
||
| 105 | |||
| 106 | return implode('', $pieces); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | // -------------------------------------------------------------------------- |
||
| 111 | |||
| 112 | if ( ! function_exists('array_zipper')) |
||
| 113 | { |
||
| 114 | /** |
||
| 115 | * Zip a set of arrays together on common keys |
||
| 116 | * |
||
| 117 | * The $zipper_input array is an array of arrays indexed by their place in the output |
||
| 118 | * array. |
||
| 119 | * |
||
| 120 | * @param array $zipper_input |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | function array_zipper(array $zipper_input): array |
||
| 124 | { |
||
| 125 | $output = []; |
||
| 126 | |||
| 127 | foreach($zipper_input as $append_key => $values) |
||
| 128 | { |
||
| 129 | foreach($values as $index => $value) |
||
| 130 | { |
||
| 131 | if ( ! isset($output[$index])) |
||
| 132 | { |
||
| 133 | $output[$index] = []; |
||
| 134 | } |
||
| 135 | $output[$index][$append_key] = $value; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | return $output; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // -------------------------------------------------------------------------- |
||
| 144 | |||
| 145 | if ( ! function_exists('regex_in_array')) |
||
| 146 | { |
||
| 147 | /** |
||
| 148 | * Determine whether a value in the passed array matches the pattern |
||
| 149 | * passed |
||
| 150 | * |
||
| 151 | * @param array $array |
||
| 152 | * @param string $pattern |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | function regex_in_array(array $array, string $pattern): bool |
||
| 156 | { |
||
| 157 | if (empty($array)) |
||
| 158 | { |
||
| 159 | return FALSE; |
||
| 160 | } |
||
| 161 | |||
| 162 | foreach($array as $item) |
||
| 163 | { |
||
| 164 | if (is_scalar($item) && preg_match($pattern, $item)) |
||
| 165 | { |
||
| 166 | return TRUE; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return FALSE; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | // -------------------------------------------------------------------------- |
||
| 175 | |||
| 176 | if ( ! function_exists('Query')) |
||
| 177 | { |
||
| 178 | /** |
||
| 179 | * Connection function |
||
| 180 | * |
||
| 181 | * Send an array or object as connection parameters to create a connection. If |
||
| 182 | * the array or object has an 'alias' parameter, passing that string to this |
||
| 183 | * function will return that connection. Passing no parameters returns the last |
||
| 184 | * connection created. |
||
| 185 | * |
||
| 186 | * @param string|object|array $params |
||
| 187 | * @return Query\QueryBuilder|null |
||
| 188 | */ |
||
| 189 | function Query($params = '') |
||
| 190 | { |
||
| 191 | $manager = ConnectionManager::get_instance(); |
||
| 192 | |||
| 193 | // If you are getting a previously created connection |
||
| 194 | if (is_scalar($params)) |
||
| 195 | { |
||
| 196 | return $manager->get_connection($params); |
||
| 197 | } |
||
| 198 | elseif ( ! is_scalar($params) && ! is_null($params)) |
||
| 199 | { |
||
| 200 | $params_object = (object) $params; |
||
| 201 | |||
| 202 | // Otherwise, return a new connection |
||
| 203 | return $manager->connect($params_object); |
||
| 204 | } |
||
| 205 | |||
| 206 | return NULL; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | // End of common.php |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: