Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
5 / 5 |
CRAP | n/a |
0 / 0 |
|
mb_trim | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
dbFilter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
arrayZipper | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
regexInArray | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
Query | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 |
1 | <?php declare(strict_types=1); |
2 | /** |
3 | * Query |
4 | * |
5 | * SQL Query Builder / Database Abstraction Layer |
6 | * |
7 | * PHP version 8.1 |
8 | * |
9 | * @package Query |
10 | * @author Timothy J. Warren <tim@timshome.page> |
11 | * @copyright 2012 - 2023 Timothy J. Warren |
12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
13 | * @link https://git.timshomepage.net/aviat/Query |
14 | * @version 4.0.0 |
15 | */ |
16 | namespace { |
17 | |
18 | use Query\ConnectionManager; |
19 | use Query\QueryBuilderInterface; |
20 | |
21 | /** |
22 | * Global functions that don't really fit anywhere else |
23 | */ |
24 | /** |
25 | * Multibyte-safe trim function |
26 | */ |
27 | function mb_trim(string $string): string |
28 | { |
29 | return preg_replace('/(^\s+)|(\s+$)/u', '', $string); |
30 | } |
31 | |
32 | /** |
33 | * Filter out db rows into one array |
34 | */ |
35 | function dbFilter(array $array, mixed $index): array |
36 | { |
37 | $newArray = []; |
38 | |
39 | foreach ($array as $a) |
40 | { |
41 | $newArray[] = $a[$index]; |
42 | } |
43 | |
44 | return $newArray; |
45 | } |
46 | |
47 | /** |
48 | * Zip a set of arrays together on common keys |
49 | * |
50 | * The $zipperInput array is an array of arrays indexed by their place in the output |
51 | * array. |
52 | */ |
53 | function arrayZipper(array $zipperInput): array |
54 | { |
55 | $output = []; |
56 | |
57 | foreach ($zipperInput as $appendKey => $values) |
58 | { |
59 | foreach ($values as $index => $value) |
60 | { |
61 | if ( ! isset($output[$index])) |
62 | { |
63 | $output[$index] = []; |
64 | } |
65 | $output[$index][$appendKey] = $value; |
66 | } |
67 | } |
68 | |
69 | return $output; |
70 | } |
71 | |
72 | /** |
73 | * Determine whether a value in the passed array matches the pattern |
74 | * passed |
75 | */ |
76 | function regexInArray(array $array, string $pattern): bool |
77 | { |
78 | if (empty($array)) |
79 | { |
80 | return FALSE; |
81 | } |
82 | |
83 | foreach ($array as $item) |
84 | { |
85 | if (is_scalar($item) && preg_match($pattern, (string) $item)) |
86 | { |
87 | return TRUE; |
88 | } |
89 | } |
90 | |
91 | return FALSE; |
92 | } |
93 | |
94 | /** |
95 | * Connection function |
96 | * |
97 | * Send an array or object as connection parameters to create a connection. If |
98 | * the array or object has an 'alias' parameter, passing that string to this |
99 | * function will return that connection. Passing no parameters returns the last |
100 | * connection created. |
101 | */ |
102 | function Query(string|object|array|null $params = ''): ?QueryBuilderInterface |
103 | { |
104 | if ($params === NULL) |
105 | { |
106 | return NULL; |
107 | } |
108 | |
109 | $manager = ConnectionManager::getInstance(); |
110 | |
111 | // If you are getting a previously created connection |
112 | if (is_string($params)) |
113 | { |
114 | return $manager->getConnection($params); |
115 | } |
116 | |
117 | $paramsObject = (object)$params; |
118 | |
119 | // Otherwise, return a new connection |
120 | return $manager->connect($paramsObject); |
121 | } |
122 | } |
123 | // End of common.php |