@@ -26,16 +26,16 @@ discard block |
||
26 | 26 | $this->expression = $expression; |
27 | 27 | $this->keyPath = $this->buildKey($keyPath); |
28 | 28 | $this->value = $this->recursiveGet($data, $this->keyPath, null); |
29 | - if($validator === null) { |
|
30 | - $validator = function ($data) { |
|
31 | - if(is_array($data)) { |
|
29 | + if ($validator === null) { |
|
30 | + $validator = function($data) { |
|
31 | + if (is_array($data)) { |
|
32 | 32 | return $this->isValidArray($data); |
33 | 33 | } |
34 | 34 | return (string) $data !== ''; |
35 | 35 | }; |
36 | 36 | } |
37 | - if($validationResultHandler === null) { |
|
38 | - $validationResultHandler = static function () {}; |
|
37 | + if ($validationResultHandler === null) { |
|
38 | + $validationResultHandler = static function() {}; |
|
39 | 39 | } |
40 | 40 | $this->validator = $validator; |
41 | 41 | $this->validationResultHandler = $validationResultHandler; |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function isValid(): bool { |
55 | 55 | $result = true; |
56 | - if($this->validator !== null) { |
|
56 | + if ($this->validator !== null) { |
|
57 | 57 | $result = call_user_func($this->validator, $this->value); |
58 | 58 | call_user_func($this->validationResultHandler, $result, [ |
59 | 59 | 'value' => $this->value, |
@@ -75,10 +75,10 @@ discard block |
||
75 | 75 | * @return string[] |
76 | 76 | */ |
77 | 77 | private function buildKey($keyPath): array { |
78 | - if(is_string($keyPath)) { |
|
78 | + if (is_string($keyPath)) { |
|
79 | 79 | $keyPath = explode('.', $keyPath); |
80 | 80 | } |
81 | - if(!is_array($keyPath)) { |
|
81 | + if (!is_array($keyPath)) { |
|
82 | 82 | throw new RuntimeException('Invalid key'); |
83 | 83 | } |
84 | 84 | return $keyPath; |
@@ -89,8 +89,8 @@ discard block |
||
89 | 89 | * @return bool |
90 | 90 | */ |
91 | 91 | private function isValidArray(array $array): bool { |
92 | - $data = array_filter($array, function ($value) { |
|
93 | - if(is_array($value)) { |
|
92 | + $data = array_filter($array, function($value) { |
|
93 | + if (is_array($value)) { |
|
94 | 94 | return $this->isValidArray($value); |
95 | 95 | } |
96 | 96 | return (string) $value !== ''; |
@@ -109,9 +109,9 @@ discard block |
||
109 | 109 | if (!$count) { |
110 | 110 | return $default; |
111 | 111 | } |
112 | - foreach($path as $idxValue) { |
|
112 | + foreach ($path as $idxValue) { |
|
113 | 113 | $part = $idxValue; |
114 | - if(!array_key_exists($part, $array)) { |
|
114 | + if (!array_key_exists($part, $array)) { |
|
115 | 115 | return $default; |
116 | 116 | } |
117 | 117 | $array = $array[$part]; |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | * @inheritDoc |
79 | 79 | */ |
80 | 80 | public function fetchRowsLazy($callback = null) { |
81 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
81 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
82 | 82 | yield from $this->fetchLazy($callback, PDO::FETCH_ASSOC); |
83 | 83 | } |
84 | 84 | |
@@ -86,8 +86,8 @@ discard block |
||
86 | 86 | * @inheritDoc |
87 | 87 | */ |
88 | 88 | public function fetchRow($callback = null): array { |
89 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
90 | - return $this->fetch($callback, PDO::FETCH_ASSOC, null, static function ($row) { |
|
89 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
90 | + return $this->fetch($callback, PDO::FETCH_ASSOC, null, static function($row) { |
|
91 | 91 | return ['valid' => is_array($row), 'default' => []]; |
92 | 92 | }); |
93 | 93 | } |
@@ -96,17 +96,17 @@ discard block |
||
96 | 96 | * @inheritDoc |
97 | 97 | */ |
98 | 98 | public function fetchObjects(string $className = 'stdClass', $callback = null): array { |
99 | - return $this->createTempStatement(function (QueryStatement $statement) use ($className, $callback) { |
|
99 | + return $this->createTempStatement(function(QueryStatement $statement) use ($className, $callback) { |
|
100 | 100 | $data = $statement->fetchAll(PDO::FETCH_CLASS, $className); |
101 | - if($this->preserveTypes) { |
|
101 | + if ($this->preserveTypes) { |
|
102 | 102 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
103 | - $data = array_map(static function ($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
103 | + $data = array_map(static function($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
104 | 104 | } |
105 | - if($callback !== null) { |
|
106 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
107 | - foreach($data as $row) { |
|
105 | + if ($callback !== null) { |
|
106 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
107 | + foreach ($data as $row) { |
|
108 | 108 | $result = $callback($row); |
109 | - if($result !== null && !($result instanceof DBIgnoreRow)) { |
|
109 | + if ($result !== null && !($result instanceof DBIgnoreRow)) { |
|
110 | 110 | $resultData[] = $result; |
111 | 111 | } else { |
112 | 112 | $resultData[] = $row; |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | * @inheritDoc |
124 | 124 | */ |
125 | 125 | public function fetchObjectsLazy($className = null, $callback = null) { |
126 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
126 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
127 | 127 | yield from $this->fetchLazy($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName); |
128 | 128 | } |
129 | 129 | |
@@ -131,8 +131,8 @@ discard block |
||
131 | 131 | * @inheritDoc |
132 | 132 | */ |
133 | 133 | public function fetchObject($className = null, $callback = null) { |
134 | - $callback = $callback ?? (static function ($row) { return $row; }); |
|
135 | - return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static function ($row) { |
|
134 | + $callback = $callback ?? (static function($row) { return $row; }); |
|
135 | + return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, static function($row) { |
|
136 | 136 | return ['valid' => is_object($row), 'default' => null]; |
137 | 137 | }); |
138 | 138 | } |
@@ -141,11 +141,11 @@ discard block |
||
141 | 141 | * @inheritDoc |
142 | 142 | */ |
143 | 143 | public function fetchKeyValue($treatValueAsArray = false): array { |
144 | - return $this->createTempStatement(static function (QueryStatement $statement) use ($treatValueAsArray) { |
|
145 | - if($treatValueAsArray) { |
|
144 | + return $this->createTempStatement(static function(QueryStatement $statement) use ($treatValueAsArray) { |
|
145 | + if ($treatValueAsArray) { |
|
146 | 146 | $rows = $statement->fetchAll(PDO::FETCH_ASSOC); |
147 | 147 | $result = []; |
148 | - foreach($rows as $row) { |
|
148 | + foreach ($rows as $row) { |
|
149 | 149 | [$key] = array_values($row); |
150 | 150 | $result[$key] = $row; |
151 | 151 | } |
@@ -161,12 +161,12 @@ discard block |
||
161 | 161 | public function fetchGroups(array $fields): array { |
162 | 162 | $rows = $this->fetchRows(); |
163 | 163 | $result = []; |
164 | - foreach($rows as $row) { |
|
164 | + foreach ($rows as $row) { |
|
165 | 165 | /** @var array<string, mixed> $tmp */ |
166 | 166 | $tmp = &$result; |
167 | - foreach($fields as $field) { |
|
167 | + foreach ($fields as $field) { |
|
168 | 168 | $value = (string) $row[$field]; |
169 | - if(!array_key_exists($value, $tmp)) { |
|
169 | + if (!array_key_exists($value, $tmp)) { |
|
170 | 170 | $tmp[$value] = []; |
171 | 171 | } |
172 | 172 | $tmp = &$tmp[$value]; |
@@ -180,8 +180,8 @@ discard block |
||
180 | 180 | * @inheritDoc |
181 | 181 | */ |
182 | 182 | public function fetchArray(?callable $fn = null): array { |
183 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($fn) { |
|
184 | - if($fn !== null) { |
|
183 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($fn) { |
|
184 | + if ($fn !== null) { |
|
185 | 185 | return $stmt->fetchAll(PDO::FETCH_FUNC, $fn); |
186 | 186 | } |
187 | 187 | return $stmt->fetchAll(PDO::FETCH_COLUMN); |
@@ -192,9 +192,9 @@ discard block |
||
192 | 192 | * @inheritDoc |
193 | 193 | */ |
194 | 194 | public function fetchValue($default = null, ?callable $fn = null) { |
195 | - return $this->createTempStatement(static function (QueryStatement $stmt) use ($default, $fn) { |
|
195 | + return $this->createTempStatement(static function(QueryStatement $stmt) use ($default, $fn) { |
|
196 | 196 | $result = $stmt->fetchAll(PDO::FETCH_COLUMN); |
197 | - if($result !== false && array_key_exists(0, $result)) { |
|
197 | + if ($result !== false && array_key_exists(0, $result)) { |
|
198 | 198 | return $fn !== null ? $fn($result[0]) : $result[0]; |
199 | 199 | } |
200 | 200 | return $default; |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | $query = $this->__toString(); |
230 | 230 | $statement = $db->prepare($query); |
231 | 231 | $statement->execute($this->values); |
232 | - if($this->getCalcFoundRows()) { |
|
232 | + if ($this->getCalcFoundRows()) { |
|
233 | 233 | $this->foundRows = (int) $db->query('SELECT FOUND_ROWS()')->fetchColumn(); |
234 | 234 | } |
235 | 235 | return $statement; |
@@ -249,21 +249,21 @@ discard block |
||
249 | 249 | * @return array<string, mixed>[] |
250 | 250 | */ |
251 | 251 | private function fetchAll($callback = null, int $mode = 0, $arg0 = null) { |
252 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
252 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0) { |
|
253 | 253 | $statement->setFetchMode($mode, $arg0); |
254 | 254 | $data = $statement->fetchAll(); |
255 | - if($this->preserveTypes) { |
|
255 | + if ($this->preserveTypes) { |
|
256 | 256 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
257 | - $data = array_map(static function ($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
257 | + $data = array_map(static function($row) use ($columnDefinitions) { return FieldValueConverter::convertValues($row, $columnDefinitions); }, $data); |
|
258 | 258 | } |
259 | - if($callback !== null) { |
|
260 | - return call_user_func(static function ($resultData = []) use ($data, $callback) { |
|
261 | - foreach($data as $row) { |
|
259 | + if ($callback !== null) { |
|
260 | + return call_user_func(static function($resultData = []) use ($data, $callback) { |
|
261 | + foreach ($data as $row) { |
|
262 | 262 | $result = $callback($row); |
263 | - if($result instanceof DBIgnoreRow) { |
|
263 | + if ($result instanceof DBIgnoreRow) { |
|
264 | 264 | continue; |
265 | 265 | } |
266 | - if($result !== null) { |
|
266 | + if ($result !== null) { |
|
267 | 267 | $resultData[] = $result; |
268 | 268 | } else { |
269 | 269 | $resultData[] = $row; |
@@ -288,18 +288,18 @@ discard block |
||
288 | 288 | $statement = $this->createStatement(); |
289 | 289 | $statement->setFetchMode($mode, $arg0); |
290 | 290 | try { |
291 | - while($row = $statement->fetch()) { |
|
291 | + while ($row = $statement->fetch()) { |
|
292 | 292 | /** @var T $row */ |
293 | 293 | // if($this->preserveTypes) { |
294 | 294 | // $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
295 | 295 | // $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
296 | 296 | // } |
297 | 297 | $result = $callback($row); |
298 | - if($result instanceof DBIgnoreRow) { |
|
298 | + if ($result instanceof DBIgnoreRow) { |
|
299 | 299 | // Skip row in this case |
300 | 300 | continue; |
301 | 301 | } |
302 | - if($result !== null) { |
|
302 | + if ($result !== null) { |
|
303 | 303 | yield $result; |
304 | 304 | } else { |
305 | 305 | yield $row; |
@@ -320,20 +320,20 @@ discard block |
||
320 | 320 | * @return T|U|array<string, mixed> |
321 | 321 | */ |
322 | 322 | private function fetch($callback, int $mode = PDO::FETCH_ASSOC, $arg0 = null, Closure $resultValidator = null) { |
323 | - return $this->createTempStatement(function (QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
323 | + return $this->createTempStatement(function(QueryStatement $statement) use ($callback, $mode, $arg0, $resultValidator) { |
|
324 | 324 | $statement->setFetchMode($mode, $arg0); |
325 | 325 | $row = $statement->fetch(); |
326 | 326 | $result = $resultValidator === null ? ['valid' => true] : $resultValidator($row); |
327 | - if(!$result['valid']) { |
|
327 | + if (!$result['valid']) { |
|
328 | 328 | return $result['default']; |
329 | 329 | } |
330 | - if($this->preserveTypes) { |
|
330 | + if ($this->preserveTypes) { |
|
331 | 331 | $columnDefinitions = FieldTypeProvider::getFieldTypes($statement); |
332 | 332 | $row = FieldValueConverter::convertValues($row, $columnDefinitions); |
333 | 333 | } |
334 | - if($callback !== null) { |
|
334 | + if ($callback !== null) { |
|
335 | 335 | $result = $callback($row); |
336 | - if($result !== null) { |
|
336 | + if ($result !== null) { |
|
337 | 337 | $row = $result; |
338 | 338 | } |
339 | 339 | } |
@@ -14,34 +14,34 @@ |
||
14 | 14 | * @return string |
15 | 15 | */ |
16 | 16 | protected function buildTableName(?string $alias, $name): string { |
17 | - if(is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) { |
|
17 | + if (is_object($name) && !($name instanceof VirtualTable) && method_exists($name, '__toString')) { |
|
18 | 18 | $name = (string) $name; |
19 | 19 | $lines = explode("\n", $name); |
20 | - $lines = array_map(static function (string $line) { return "\t{$line}"; }, $lines); |
|
20 | + $lines = array_map(static function(string $line) { return "\t{$line}"; }, $lines); |
|
21 | 21 | $name = implode("\n", $lines); |
22 | - $name = '(' . trim(rtrim(trim($name), ';')) . ')'; |
|
22 | + $name = '('.trim(rtrim(trim($name), ';')).')'; |
|
23 | 23 | } |
24 | - if(is_array($name)) { |
|
24 | + if (is_array($name)) { |
|
25 | 25 | $parts = []; |
26 | - foreach($name as $index => $bucket) { |
|
27 | - if(is_scalar($bucket) && ctype_digit((string) $index)) { |
|
26 | + foreach ($name as $index => $bucket) { |
|
27 | + if (is_scalar($bucket) && ctype_digit((string) $index)) { |
|
28 | 28 | $parts[] = "SELECT {$this->db()->quote($bucket)} AS {$this->db()->quoteField('value')}"; |
29 | 29 | } else { |
30 | 30 | $values = []; |
31 | - foreach($bucket as $field => $value) { |
|
31 | + foreach ($bucket as $field => $value) { |
|
32 | 32 | $values[] = sprintf('%s AS %s', $this->db()->quote($value), $this->db()->quoteField($field)); |
33 | 33 | } |
34 | 34 | $parts[] = sprintf("SELECT %s", implode(', ', $values)); |
35 | 35 | } |
36 | 36 | } |
37 | - $name = '(' . implode("\n\tUNION\n\t", $parts) . ')'; |
|
37 | + $name = '('.implode("\n\tUNION\n\t", $parts).')'; |
|
38 | 38 | } |
39 | - if((is_string($name) || $name instanceof VirtualTable) && $this->db()->getVirtualTables()->has($name)) { |
|
39 | + if ((is_string($name) || $name instanceof VirtualTable) && $this->db()->getVirtualTables()->has($name)) { |
|
40 | 40 | $select = (string) $this->db()->getVirtualTables()->get($name); |
41 | 41 | $name = sprintf('(%s)', implode("\n\t", explode("\n", trim($select)))); |
42 | 42 | } |
43 | 43 | $name = $this->aliasReplacer()->replace((string) $name); |
44 | - if($alias !== null) { |
|
44 | + if ($alias !== null) { |
|
45 | 45 | return sprintf("%s %s", $name, $alias); |
46 | 46 | } |
47 | 47 | return $name; |
@@ -14,9 +14,9 @@ discard block |
||
14 | 14 | * @return $this |
15 | 15 | */ |
16 | 16 | public function groupBy(...$args) { |
17 | - foreach($args as $expression) { |
|
18 | - if(is_array($expression)) { |
|
19 | - if(!count($expression)) { |
|
17 | + foreach ($args as $expression) { |
|
18 | + if (is_array($expression)) { |
|
19 | + if (!count($expression)) { |
|
20 | 20 | continue; |
21 | 21 | } |
22 | 22 | $expression = $this->quoteExpr($expression[0], array_slice($expression, 1)); |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | * @return string |
32 | 32 | */ |
33 | 33 | protected function buildGroups(string $query): string { |
34 | - if(!count($this->groupBy)) { |
|
34 | + if (!count($this->groupBy)) { |
|
35 | 35 | return $query; |
36 | 36 | } |
37 | 37 | $query .= "GROUP BY\n"; |
38 | 38 | $arr = []; |
39 | - foreach($this->groupBy as $expression) { |
|
39 | + foreach ($this->groupBy as $expression) { |
|
40 | 40 | $arr[] = "\t{$expression}"; |
41 | 41 | } |
42 | 42 | return $query.implode(",\n", $arr)."\n"; |
@@ -17,7 +17,7 @@ |
||
17 | 17 | * @return $this |
18 | 18 | */ |
19 | 19 | public function having($expression, ...$args): self { |
20 | - $fn = function ($expression, $args) { $this->having[] = [$expression, $args]; }; |
|
20 | + $fn = function($expression, $args) { $this->having[] = [$expression, $args]; }; |
|
21 | 21 | ConditionAddHelper::addCondition($fn, $expression, $args); |
22 | 22 | return $this; |
23 | 23 | } |
@@ -26,10 +26,10 @@ |
||
26 | 26 | */ |
27 | 27 | protected function buildTables(string $query): string { |
28 | 28 | $arr = []; |
29 | - foreach($this->tables as $table) { |
|
29 | + foreach ($this->tables as $table) { |
|
30 | 30 | $arr[] = "\t".$this->buildTableName($table['alias'], $table['name']); |
31 | 31 | } |
32 | - if(count($arr)) { |
|
32 | + if (count($arr)) { |
|
33 | 33 | $query .= implode(",\n", $arr)."\n"; |
34 | 34 | } |
35 | 35 | return $query; |
@@ -15,8 +15,8 @@ discard block |
||
15 | 15 | * @return $this |
16 | 16 | */ |
17 | 17 | public function orderBy($expression, string $direction = 'ASC'): self { |
18 | - if($expression instanceof OrderBySpecification) { |
|
19 | - foreach($expression->getFields() as $field) { |
|
18 | + if ($expression instanceof OrderBySpecification) { |
|
19 | + foreach ($expression->getFields() as $field) { |
|
20 | 20 | $this->addOrder($field[0], $field[1]); |
21 | 21 | } |
22 | 22 | return $this; |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | */ |
33 | 33 | public function orderByValues(string $fieldName, array $values): self { |
34 | 34 | $expr = []; |
35 | - foreach(array_values($values) as $idx => $value) { |
|
35 | + foreach (array_values($values) as $idx => $value) { |
|
36 | 36 | $expr[] = $this->db()->quoteExpression("WHEN ? THEN ?", [$value, $idx]); |
37 | 37 | } |
38 | 38 | $this->orderBy[] = [sprintf("CASE %s\n\t\t%s\n\tEND", $this->db()->quoteField($fieldName), implode("\n\t\t", $expr)), 'ASC']; |
@@ -44,12 +44,12 @@ discard block |
||
44 | 44 | * @return string |
45 | 45 | */ |
46 | 46 | protected function buildOrder(string $query): string { |
47 | - if(!count($this->orderBy)) { |
|
47 | + if (!count($this->orderBy)) { |
|
48 | 48 | return $query; |
49 | 49 | } |
50 | 50 | $query .= "ORDER BY\n"; |
51 | 51 | $arr = []; |
52 | - foreach($this->orderBy as [$expression, $direction]) { |
|
52 | + foreach ($this->orderBy as [$expression, $direction]) { |
|
53 | 53 | $arr[] = sprintf("\t%s %s", $expression, strtoupper($direction)); |
54 | 54 | } |
55 | 55 | return $query.implode(",\n", $arr)."\n"; |
@@ -61,8 +61,8 @@ discard block |
||
61 | 61 | */ |
62 | 62 | private function addOrder($expression, string $direction): void { |
63 | 63 | $direction = $this->fixDirection($direction); |
64 | - if(is_array($expression)) { |
|
65 | - if(count($expression) < 1) { |
|
64 | + if (is_array($expression)) { |
|
65 | + if (count($expression) < 1) { |
|
66 | 66 | return; |
67 | 67 | } |
68 | 68 | $expr = (string) $expression[0]; |
@@ -17,7 +17,7 @@ |
||
17 | 17 | * @return $this |
18 | 18 | */ |
19 | 19 | public function where($expression, ...$args): self { |
20 | - $fn = function ($expression, $args) { $this->where[] = [$expression, $args]; }; |
|
20 | + $fn = function($expression, $args) { $this->where[] = [$expression, $args]; }; |
|
21 | 21 | ConditionAddHelper::addCondition($fn, $expression, $args); |
22 | 22 | return $this; |
23 | 23 | } |
@@ -31,13 +31,13 @@ |
||
31 | 31 | */ |
32 | 32 | protected function buildFieldList(array $fields, array $query = []): array { |
33 | 33 | foreach ($fields as $fieldName => $fieldValue) { |
34 | - if($fieldValue instanceof DefaultValue) { |
|
34 | + if ($fieldValue instanceof DefaultValue) { |
|
35 | 35 | $fieldValue = 'DEFAULT'; |
36 | 36 | } |
37 | - if(is_array($this->mask) && !in_array($fieldName, $this->mask, true)) { |
|
37 | + if (is_array($this->mask) && !in_array($fieldName, $this->mask, true)) { |
|
38 | 38 | continue; |
39 | 39 | } |
40 | - if(is_int($fieldName)) { |
|
40 | + if (is_int($fieldName)) { |
|
41 | 41 | if (is_array($fieldValue)) { |
42 | 42 | $fieldValue = $this->db()->quoteExpression($fieldValue[0], array_slice($fieldValue, 1)); |
43 | 43 | } |