@@ -134,13 +134,13 @@ discard block |
||
134 | 134 | public static function mergeRecursiveCustom( |
135 | 135 | array $existing_row, |
136 | 136 | array $conflict_row, |
137 | - callable $merge_resolver=null, |
|
138 | - $max_depth=null |
|
139 | - ){ |
|
137 | + callable $merge_resolver = null, |
|
138 | + $max_depth = null |
|
139 | + ) { |
|
140 | 140 | foreach ($conflict_row as $column => $conflict_value) { |
141 | 141 | |
142 | 142 | // not existing in first array |
143 | - if (!isset($existing_row[$column])) { |
|
143 | + if ( ! isset($existing_row[$column])) { |
|
144 | 144 | $existing_row[$column] = $conflict_value; |
145 | 145 | continue; |
146 | 146 | } |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | } |
174 | 174 | else { |
175 | 175 | // same resolution as array_merge_recursive |
176 | - if (!is_array($existing_value)) { |
|
176 | + if ( ! is_array($existing_value)) { |
|
177 | 177 | $existing_row[$column] = [$existing_value]; |
178 | 178 | } |
179 | 179 | |
@@ -196,11 +196,11 @@ discard block |
||
196 | 196 | public static function mergePreservingDistincts( |
197 | 197 | array $existing_row, |
198 | 198 | array $conflict_row |
199 | - ){ |
|
199 | + ) { |
|
200 | 200 | $merge = static::mergeRecursiveCustom( |
201 | 201 | $existing_row, |
202 | 202 | $conflict_row, |
203 | - function ($existing_value, $conflict_value, $column) { |
|
203 | + function($existing_value, $conflict_value, $column) { |
|
204 | 204 | |
205 | 205 | if ( ! $existing_value instanceof MergeBucket) { |
206 | 206 | $existing_value = MergeBucket::from()->push($existing_value); |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | { |
231 | 231 | foreach ($merged_row as $entry => $values) { |
232 | 232 | if ($values instanceof MergeBucket) { |
233 | - $out[ $entry ] = $values->toArray(); |
|
233 | + $out[$entry] = $values->toArray(); |
|
234 | 234 | } |
235 | 235 | } |
236 | 236 | |
@@ -242,10 +242,10 @@ discard block |
||
242 | 242 | * |
243 | 243 | * @see mergePreservingDistincts() |
244 | 244 | */ |
245 | - public static function keepUniqueColumnValues(array $row, array $excluded_columns=[]) |
|
245 | + public static function keepUniqueColumnValues(array $row, array $excluded_columns = []) |
|
246 | 246 | { |
247 | 247 | foreach ($row as $column => &$values) { |
248 | - if (!is_array($values)) |
|
248 | + if ( ! is_array($values)) |
|
249 | 249 | continue; |
250 | 250 | |
251 | 251 | if (in_array($column, $excluded_columns)) |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | */ |
270 | 270 | public static function unique($array) |
271 | 271 | { |
272 | - if (! is_array($array) && ! $array instanceof \Traversable) { |
|
272 | + if ( ! is_array($array) && ! $array instanceof \Traversable) { |
|
273 | 273 | throw \InvalidArgumentEXception( |
274 | 274 | "\$array must be an array or a \Traversable instead of: \n" |
275 | 275 | .var_export($array) |
@@ -285,13 +285,13 @@ discard block |
||
285 | 285 | $id = serialize($value); |
286 | 286 | } |
287 | 287 | |
288 | - if (isset($ids[ $id ])) { |
|
289 | - unset($array[ $key ]); |
|
290 | - $ids[ $id ][] = $key; |
|
288 | + if (isset($ids[$id])) { |
|
289 | + unset($array[$key]); |
|
290 | + $ids[$id][] = $key; |
|
291 | 291 | continue; |
292 | 292 | } |
293 | 293 | |
294 | - $ids[ $id ] = [$key]; |
|
294 | + $ids[$id] = [$key]; |
|
295 | 295 | } |
296 | 296 | |
297 | 297 | return $array; |
@@ -64,14 +64,12 @@ discard block |
||
64 | 64 | ) |
65 | 65 | { |
66 | 66 | $array1[$key] = self::merge($array1[$key], $value); |
67 | - } |
|
68 | - else |
|
67 | + } else |
|
69 | 68 | { |
70 | 69 | $array1[$key] = $value; |
71 | 70 | } |
72 | 71 | } |
73 | - } |
|
74 | - else |
|
72 | + } else |
|
75 | 73 | { |
76 | 74 | foreach ($array2 as $value) |
77 | 75 | { |
@@ -96,14 +94,12 @@ discard block |
||
96 | 94 | ) |
97 | 95 | { |
98 | 96 | $array1[$key] = self::merge($array1[$key], $value); |
99 | - } |
|
100 | - else |
|
97 | + } else |
|
101 | 98 | { |
102 | 99 | $array1[$key] = $value; |
103 | 100 | } |
104 | 101 | } |
105 | - } |
|
106 | - else |
|
102 | + } else |
|
107 | 103 | { |
108 | 104 | foreach ($array2 as $value) |
109 | 105 | { |
@@ -170,8 +166,7 @@ discard block |
||
170 | 166 | $column, |
171 | 167 | ] |
172 | 168 | ); |
173 | - } |
|
174 | - else { |
|
169 | + } else { |
|
175 | 170 | // same resolution as array_merge_recursive |
176 | 171 | if (!is_array($existing_value)) { |
177 | 172 | $existing_row[$column] = [$existing_value]; |
@@ -245,15 +240,18 @@ discard block |
||
245 | 240 | public static function keepUniqueColumnValues(array $row, array $excluded_columns=[]) |
246 | 241 | { |
247 | 242 | foreach ($row as $column => &$values) { |
248 | - if (!is_array($values)) |
|
249 | - continue; |
|
243 | + if (!is_array($values)) { |
|
244 | + continue; |
|
245 | + } |
|
250 | 246 | |
251 | - if (in_array($column, $excluded_columns)) |
|
252 | - continue; |
|
247 | + if (in_array($column, $excluded_columns)) { |
|
248 | + continue; |
|
249 | + } |
|
253 | 250 | |
254 | 251 | $values = array_unique($values); |
255 | - if (count($values) == 1) |
|
256 | - $values = $values[0]; |
|
252 | + if (count($values) == 1) { |
|
253 | + $values = $values[0]; |
|
254 | + } |
|
257 | 255 | } |
258 | 256 | |
259 | 257 | return $row; |
@@ -280,8 +278,7 @@ discard block |
||
280 | 278 | foreach ($array as $key => $value) { |
281 | 279 | if (is_scalar($value)) { |
282 | 280 | $id = $value; |
283 | - } |
|
284 | - else { |
|
281 | + } else { |
|
285 | 282 | $id = serialize($value); |
286 | 283 | } |
287 | 284 |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @return array The array containing the grouped rows. |
20 | 20 | */ |
21 | - public function groupBy( callable $indexGenerator, callable $conflictResolver=null ) |
|
21 | + public function groupBy(callable $indexGenerator, callable $conflictResolver = null) |
|
22 | 22 | { |
23 | 23 | // todo : this doesn't work |
24 | 24 | // return $this->groupByTransformed($indexGenerator, null, $conflictResolver); |
@@ -26,15 +26,15 @@ discard block |
||
26 | 26 | $out = []; |
27 | 27 | foreach ($this->data as $key => $row) { |
28 | 28 | |
29 | - if (!$row) |
|
29 | + if ( ! $row) |
|
30 | 30 | continue; |
31 | 31 | |
32 | 32 | $newIndexes = call_user_func($indexGenerator, $key, $row); |
33 | - if (!is_array($newIndexes)) |
|
33 | + if ( ! is_array($newIndexes)) |
|
34 | 34 | $newIndexes = [$newIndexes]; |
35 | 35 | |
36 | 36 | foreach ($newIndexes as $newIndex) { |
37 | - if (!isset($out[$newIndex])) { |
|
37 | + if ( ! isset($out[$newIndex])) { |
|
38 | 38 | $out[$newIndex] = $row; |
39 | 39 | } |
40 | 40 | else { |
@@ -69,26 +69,26 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return array The array containing the grouped rows. |
71 | 71 | */ |
72 | - public function groupInArrays( callable $indexGenerator ) |
|
72 | + public function groupInArrays(callable $indexGenerator) |
|
73 | 73 | { |
74 | 74 | $out = []; |
75 | 75 | foreach ($this->data as $key => $row) { |
76 | 76 | |
77 | - if (!$row) |
|
77 | + if ( ! $row) |
|
78 | 78 | continue; |
79 | 79 | |
80 | 80 | $new_keys = call_user_func($indexGenerator, $row, $key); |
81 | - if (!is_array($new_keys)) |
|
81 | + if ( ! is_array($new_keys)) |
|
82 | 82 | $new_keys = [$new_keys]; |
83 | 83 | |
84 | 84 | foreach ($new_keys as $new_key) { |
85 | - if (!isset($out[ $new_key ])) { |
|
86 | - $out[ $new_key ] = [ |
|
85 | + if ( ! isset($out[$new_key])) { |
|
86 | + $out[$new_key] = [ |
|
87 | 87 | $key => $row |
88 | 88 | ]; |
89 | 89 | } |
90 | 90 | else { |
91 | - $out[ $new_key ][ $key ] = $row; |
|
91 | + $out[$new_key][$key] = $row; |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | } |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | public static function mergeRecursiveCustom( |
103 | 103 | array $existing_row, |
104 | 104 | array $conflict_row, |
105 | - callable $merge_resolver=null, |
|
106 | - $max_depth=null |
|
107 | - ){ |
|
105 | + callable $merge_resolver = null, |
|
106 | + $max_depth = null |
|
107 | + ) { |
|
108 | 108 | return Arrays::mergePreservingDistincts( |
109 | 109 | $existing_row, |
110 | 110 | $conflict_row, |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | /** |
127 | 127 | * @deprecated Arrays::mergePreservingDistincts() |
128 | 128 | */ |
129 | - public static function keepUniqueColumnValues(array $row, array $excluded_columns=[]) |
|
129 | + public static function keepUniqueColumnValues(array $row, array $excluded_columns = []) |
|
130 | 130 | { |
131 | 131 | return Arrays::keepUniqueColumnValues($row, $excluded_columns); |
132 | 132 | } |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | */ |
148 | 148 | public function groupByTransformed( |
149 | 149 | callable $indexGenerator, |
150 | - callable $rowTransformer, // todo check this behavior |
|
150 | + callable $rowTransformer, // todo check this behavior |
|
151 | 151 | callable $conflictResolver ) |
152 | 152 | { |
153 | 153 | // The goal here is to remove the second parameter has it makes the |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | $out = []; |
161 | 161 | foreach ($this->data as $key => $row) { |
162 | 162 | |
163 | - if (!$row) |
|
163 | + if ( ! $row) |
|
164 | 164 | continue; |
165 | 165 | |
166 | 166 | $newIndex = call_user_func($indexGenerator, $key, $row); |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | ? call_user_func($rowTransformer, $row) |
170 | 170 | : $row; |
171 | 171 | |
172 | - if (!isset($out[$newIndex])) { |
|
172 | + if ( ! isset($out[$newIndex])) { |
|
173 | 173 | $out[$newIndex] = $transformedRow; |
174 | 174 | } |
175 | 175 | else { |
@@ -194,12 +194,12 @@ discard block |
||
194 | 194 | * rows have the same index. |
195 | 195 | * @return static |
196 | 196 | */ |
197 | - public function mergeWith( $otherTable, callable $conflictResolver=null ) |
|
197 | + public function mergeWith($otherTable, callable $conflictResolver = null) |
|
198 | 198 | { |
199 | 199 | if (is_array($otherTable)) |
200 | 200 | $otherTable = new static($otherTable); |
201 | 201 | |
202 | - if (!$otherTable instanceof static) { |
|
202 | + if ( ! $otherTable instanceof static) { |
|
203 | 203 | self::throwUsageException( |
204 | 204 | '$otherTable must be an array or an instance of '.static::class.' instead of: ' |
205 | 205 | .var_export($otherTable, true) |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | $out = $this->data; |
210 | 210 | foreach ($otherTable->getArray() as $key => $row) { |
211 | 211 | |
212 | - if (!isset($out[$key])) { |
|
212 | + if ( ! isset($out[$key])) { |
|
213 | 213 | $out[$key] = $row; |
214 | 214 | } |
215 | 215 | else { |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * (same as self::mergeWith with the other table as $this) |
238 | 238 | * @return static |
239 | 239 | */ |
240 | - public function mergeIn( $otherTable, callable $conflictResolver=null ) |
|
240 | + public function mergeIn($otherTable, callable $conflictResolver = null) |
|
241 | 241 | { |
242 | 242 | $otherTable->mergeWith($this, $conflictResolver); |
243 | 243 | return $this; |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | */ |
264 | 264 | public function each(callable $rowTransformer) |
265 | 265 | { |
266 | - $out = []; |
|
266 | + $out = []; |
|
267 | 267 | foreach ($this->data as $key => $row) { |
268 | 268 | $out[$key] = call_user_func_array( |
269 | 269 | $rowTransformer, |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | */ |
294 | 294 | public function renameColumns(array $old_to_new_names) |
295 | 295 | { |
296 | - $out = []; |
|
296 | + $out = []; |
|
297 | 297 | foreach ($this->data as $key => $row) { |
298 | 298 | try { |
299 | 299 | foreach ($old_to_new_names as $old_name => $new_name) { |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | } |
303 | 303 | } |
304 | 304 | catch (\Exception $e) { |
305 | - self::throwUsageException( $e->getMessage() ); |
|
305 | + self::throwUsageException($e->getMessage()); |
|
306 | 306 | } |
307 | 307 | |
308 | 308 | $out[$key] = $row; |
@@ -354,20 +354,20 @@ discard block |
||
354 | 354 | * nor a static. |
355 | 355 | * @return static $this |
356 | 356 | */ |
357 | - public function append($new_rows, callable $conflict_resolver=null) |
|
357 | + public function append($new_rows, callable $conflict_resolver = null) |
|
358 | 358 | { |
359 | 359 | if ($new_rows instanceof static) |
360 | 360 | $new_rows = $new_rows->getArray(); |
361 | 361 | |
362 | - if (!is_array($new_rows)) { |
|
362 | + if ( ! is_array($new_rows)) { |
|
363 | 363 | $this->throwUsageException( |
364 | - "\$new_rows parameter must be an array or an instance of " . __CLASS__ |
|
364 | + "\$new_rows parameter must be an array or an instance of ".__CLASS__ |
|
365 | 365 | ); |
366 | 366 | } |
367 | 367 | |
368 | - if (!$conflict_resolver) { |
|
368 | + if ( ! $conflict_resolver) { |
|
369 | 369 | // default conflict resolver: append with numeric key |
370 | - $conflict_resolver = function (&$data, $existing_row, $confliuct_row, $key) { |
|
370 | + $conflict_resolver = function(&$data, $existing_row, $confliuct_row, $key) { |
|
371 | 371 | $data[] = $confliuct_row; |
372 | 372 | }; |
373 | 373 | } |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | * |
400 | 400 | * @return static |
401 | 401 | */ |
402 | - public function dimensionsAsColumns(array $columnNames, array $options=null) |
|
402 | + public function dimensionsAsColumns(array $columnNames, array $options = null) |
|
403 | 403 | { |
404 | 404 | $out = $this->dimensionsAsColumns_recurser($this->data, $columnNames); |
405 | 405 | return $this->returnConstant($out); |
@@ -436,12 +436,12 @@ discard block |
||
436 | 436 | * ], |
437 | 437 | * ] |
438 | 438 | */ |
439 | - protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts=[]) |
|
439 | + protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts = []) |
|
440 | 440 | { |
441 | 441 | $out = []; |
442 | 442 | // if (!$columnNames) |
443 | 443 | // return $data; |
444 | - $no_more_column = !(bool) $columnNames; |
|
444 | + $no_more_column = ! (bool) $columnNames; |
|
445 | 445 | |
446 | 446 | // If all the names have been given to the dimensions |
447 | 447 | // we compile the index key of the row at the current level |
@@ -465,7 +465,7 @@ discard block |
||
465 | 465 | self::throwUsageException( |
466 | 466 | "Trying to populate a column '$name' that " |
467 | 467 | ."already exists with a different value " |
468 | - .var_export($data[$name], true). " => '$value'" |
|
468 | + .var_export($data[$name], true)." => '$value'" |
|
469 | 469 | ); |
470 | 470 | } |
471 | 471 | $data[$name] = $value; |
@@ -496,7 +496,7 @@ discard block |
||
496 | 496 | } |
497 | 497 | else { |
498 | 498 | |
499 | - if (!isset($rows)) { |
|
499 | + if ( ! isset($rows)) { |
|
500 | 500 | echo json_encode([ |
501 | 501 | '$rowIdParts' => $rowIdParts, |
502 | 502 | '$row' => $row, |
@@ -555,12 +555,12 @@ discard block |
||
555 | 555 | |
556 | 556 | if (is_string($value) && array_key_exists($value, $row)) { |
557 | 557 | $part_name .= $value; |
558 | - $group_value = $row[ $value ]; |
|
558 | + $group_value = $row[$value]; |
|
559 | 559 | } |
560 | 560 | elseif (is_callable($value)) { |
561 | 561 | |
562 | 562 | if (is_string($value)) { |
563 | - $part_name .= $value; |
|
563 | + $part_name .= $value; |
|
564 | 564 | } |
565 | 565 | // elseif (is_function($value)) { |
566 | 566 | elseif (is_object($value) && ($value instanceof Closure)) { |
@@ -576,19 +576,19 @@ discard block |
||
576 | 576 | ]); |
577 | 577 | } |
578 | 578 | elseif (is_int($value)) { |
579 | - $part_name .= $value ? : '0'; |
|
580 | - $group_value = $row[ $value ]; |
|
579 | + $part_name .= $value ?: '0'; |
|
580 | + $group_value = $row[$value]; |
|
581 | 581 | } |
582 | 582 | else { |
583 | 583 | self::throwUsageException( |
584 | 584 | 'Bad value provided for groupBy id generation: ' |
585 | 585 | .var_export($value, true) |
586 | - ."\n" . var_export($row, true) |
|
586 | + ."\n".var_export($row, true) |
|
587 | 587 | ); |
588 | 588 | } |
589 | 589 | |
590 | - if (!is_null($part_name)) |
|
591 | - $group_parts[ $part_name ] = $group_value; |
|
590 | + if ( ! is_null($part_name)) |
|
591 | + $group_parts[$part_name] = $group_value; |
|
592 | 592 | } |
593 | 593 | |
594 | 594 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -606,9 +606,9 @@ discard block |
||
606 | 606 | /** |
607 | 607 | * Returns the first element of the array |
608 | 608 | */ |
609 | - public function first($strict=false) |
|
609 | + public function first($strict = false) |
|
610 | 610 | { |
611 | - if (!$this->count()) { |
|
611 | + if ( ! $this->count()) { |
|
612 | 612 | if ($strict) |
613 | 613 | throw new \ErrorException("No first element found in this array"); |
614 | 614 | else |
@@ -628,9 +628,9 @@ discard block |
||
628 | 628 | * |
629 | 629 | * @todo Preserve the offset |
630 | 630 | */ |
631 | - public function last($strict=false) |
|
631 | + public function last($strict = false) |
|
632 | 632 | { |
633 | - if (!$this->count()) { |
|
633 | + if ( ! $this->count()) { |
|
634 | 634 | if ($strict) |
635 | 635 | throw new \ErrorException("No last element found in this array"); |
636 | 636 | else |
@@ -648,9 +648,9 @@ discard block |
||
648 | 648 | /** |
649 | 649 | * |
650 | 650 | */ |
651 | - public function firstKey($strict=false) |
|
651 | + public function firstKey($strict = false) |
|
652 | 652 | { |
653 | - if (!$this->count()) { |
|
653 | + if ( ! $this->count()) { |
|
654 | 654 | if ($strict) |
655 | 655 | throw new \ErrorException("No last element found in this array"); |
656 | 656 | else |
@@ -669,16 +669,16 @@ discard block |
||
669 | 669 | /** |
670 | 670 | * |
671 | 671 | */ |
672 | - public function lastKey($strict=false) |
|
672 | + public function lastKey($strict = false) |
|
673 | 673 | { |
674 | - if (!$this->count()) { |
|
674 | + if ( ! $this->count()) { |
|
675 | 675 | if ($strict) |
676 | 676 | throw new \ErrorException("No last element found in this array"); |
677 | 677 | else |
678 | 678 | $lastKey = null; |
679 | 679 | } |
680 | 680 | else { |
681 | - $key = key($this->data); |
|
681 | + $key = key($this->data); |
|
682 | 682 | end($this->data); |
683 | 683 | $lastKey = key($this->data); |
684 | 684 | $this->move($key); |
@@ -690,7 +690,7 @@ discard block |
||
690 | 690 | /** |
691 | 691 | * Move the internal pointer of the array to the key given as parameter |
692 | 692 | */ |
693 | - public function move($key, $strict=true) |
|
693 | + public function move($key, $strict = true) |
|
694 | 694 | { |
695 | 695 | if (array_key_exists($key, $this->data)) { |
696 | 696 | foreach ($this->data as $i => &$value) { |
@@ -757,13 +757,13 @@ discard block |
||
757 | 757 | * @see http://php.net/manual/fr/function.var-dump.php |
758 | 758 | * @todo Handle xdebug dump formatting |
759 | 759 | */ |
760 | - public function dump($exit=false) |
|
760 | + public function dump($exit = false) |
|
761 | 761 | { |
762 | 762 | $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
763 | 763 | $caller = $bt[0]; |
764 | 764 | |
765 | 765 | var_export([ |
766 | - 'location' => $caller['file'] . ':' . $caller['line'], |
|
766 | + 'location' => $caller['file'].':'.$caller['line'], |
|
767 | 767 | 'data' => $this->data, |
768 | 768 | ]); |
769 | 769 | |
@@ -780,7 +780,7 @@ discard block |
||
780 | 780 | * @todo move it to an Arrays class storing static methods |
781 | 781 | */ |
782 | 782 | public static function replaceEntries( |
783 | - array $array, callable $replacer, $max_depth=null |
|
783 | + array $array, callable $replacer, $max_depth = null |
|
784 | 784 | ) { |
785 | 785 | foreach ($array as $key => &$row) { |
786 | 786 | $arguments = [&$row, $key]; |
@@ -788,7 +788,7 @@ discard block |
||
788 | 788 | |
789 | 789 | if (is_array($row) && $max_depth !== 0) { // allowing null to have no depth limit |
790 | 790 | $row = self::replaceEntries( |
791 | - $row, $replacer, $max_depth ? $max_depth-1 : $max_depth |
|
791 | + $row, $replacer, $max_depth ? $max_depth - 1 : $max_depth |
|
792 | 792 | ); |
793 | 793 | } |
794 | 794 | } |
@@ -804,7 +804,7 @@ discard block |
||
804 | 804 | * |
805 | 805 | * @return static $this or a new static. |
806 | 806 | */ |
807 | - public function extract($callback=null) |
|
807 | + public function extract($callback = null) |
|
808 | 808 | { |
809 | 809 | if ($callback) { |
810 | 810 | |
@@ -812,7 +812,7 @@ discard block |
||
812 | 812 | $callback = new \JClaveau\LogicalFilter\LogicalFilter($callback); |
813 | 813 | } |
814 | 814 | |
815 | - if (!is_callable($callback)) { |
|
815 | + if ( ! is_callable($callback)) { |
|
816 | 816 | $this->throwUsageException( |
817 | 817 | "\$callback must be a logical filter description array or a callable" |
818 | 818 | ." instead of " |
@@ -824,7 +824,7 @@ discard block |
||
824 | 824 | foreach ($this->data as $key => $value) { |
825 | 825 | if ($callback($value, $key)) { |
826 | 826 | $out[$key] = $value; |
827 | - unset( $this->data[$key] ); |
|
827 | + unset($this->data[$key]); |
|
828 | 828 | } |
829 | 829 | } |
830 | 830 | } |
@@ -26,18 +26,19 @@ discard block |
||
26 | 26 | $out = []; |
27 | 27 | foreach ($this->data as $key => $row) { |
28 | 28 | |
29 | - if (!$row) |
|
30 | - continue; |
|
29 | + if (!$row) { |
|
30 | + continue; |
|
31 | + } |
|
31 | 32 | |
32 | 33 | $newIndexes = call_user_func($indexGenerator, $key, $row); |
33 | - if (!is_array($newIndexes)) |
|
34 | - $newIndexes = [$newIndexes]; |
|
34 | + if (!is_array($newIndexes)) { |
|
35 | + $newIndexes = [$newIndexes]; |
|
36 | + } |
|
35 | 37 | |
36 | 38 | foreach ($newIndexes as $newIndex) { |
37 | 39 | if (!isset($out[$newIndex])) { |
38 | 40 | $out[$newIndex] = $row; |
39 | - } |
|
40 | - else { |
|
41 | + } else { |
|
41 | 42 | if ($conflictResolver === null) { |
42 | 43 | self::throwUsageException( |
43 | 44 | "A 'group by' provoking a conflict" |
@@ -74,20 +75,21 @@ discard block |
||
74 | 75 | $out = []; |
75 | 76 | foreach ($this->data as $key => $row) { |
76 | 77 | |
77 | - if (!$row) |
|
78 | - continue; |
|
78 | + if (!$row) { |
|
79 | + continue; |
|
80 | + } |
|
79 | 81 | |
80 | 82 | $new_keys = call_user_func($indexGenerator, $row, $key); |
81 | - if (!is_array($new_keys)) |
|
82 | - $new_keys = [$new_keys]; |
|
83 | + if (!is_array($new_keys)) { |
|
84 | + $new_keys = [$new_keys]; |
|
85 | + } |
|
83 | 86 | |
84 | 87 | foreach ($new_keys as $new_key) { |
85 | 88 | if (!isset($out[ $new_key ])) { |
86 | 89 | $out[ $new_key ] = [ |
87 | 90 | $key => $row |
88 | 91 | ]; |
89 | - } |
|
90 | - else { |
|
92 | + } else { |
|
91 | 93 | $out[ $new_key ][ $key ] = $row; |
92 | 94 | } |
93 | 95 | } |
@@ -160,8 +162,9 @@ discard block |
||
160 | 162 | $out = []; |
161 | 163 | foreach ($this->data as $key => $row) { |
162 | 164 | |
163 | - if (!$row) |
|
164 | - continue; |
|
165 | + if (!$row) { |
|
166 | + continue; |
|
167 | + } |
|
165 | 168 | |
166 | 169 | $newIndex = call_user_func($indexGenerator, $key, $row); |
167 | 170 | |
@@ -171,8 +174,7 @@ discard block |
||
171 | 174 | |
172 | 175 | if (!isset($out[$newIndex])) { |
173 | 176 | $out[$newIndex] = $transformedRow; |
174 | - } |
|
175 | - else { |
|
177 | + } else { |
|
176 | 178 | $out[$newIndex] = call_user_func( |
177 | 179 | $conflictResolver, |
178 | 180 | $newIndex, |
@@ -196,8 +198,9 @@ discard block |
||
196 | 198 | */ |
197 | 199 | public function mergeWith( $otherTable, callable $conflictResolver=null ) |
198 | 200 | { |
199 | - if (is_array($otherTable)) |
|
200 | - $otherTable = new static($otherTable); |
|
201 | + if (is_array($otherTable)) { |
|
202 | + $otherTable = new static($otherTable); |
|
203 | + } |
|
201 | 204 | |
202 | 205 | if (!$otherTable instanceof static) { |
203 | 206 | self::throwUsageException( |
@@ -211,10 +214,10 @@ discard block |
||
211 | 214 | |
212 | 215 | if (!isset($out[$key])) { |
213 | 216 | $out[$key] = $row; |
214 | - } |
|
215 | - else { |
|
216 | - if ($conflictResolver === null) |
|
217 | - self::throwUsageException('No conflict resolver for a merge provoking one'); |
|
217 | + } else { |
|
218 | + if ($conflictResolver === null) { |
|
219 | + self::throwUsageException('No conflict resolver for a merge provoking one'); |
|
220 | + } |
|
218 | 221 | |
219 | 222 | $arguments = [ |
220 | 223 | &$key, |
@@ -300,8 +303,7 @@ discard block |
||
300 | 303 | $row[$new_name] = $row[$old_name]; |
301 | 304 | unset($row[$old_name]); |
302 | 305 | } |
303 | - } |
|
304 | - catch (\Exception $e) { |
|
306 | + } catch (\Exception $e) { |
|
305 | 307 | self::throwUsageException( $e->getMessage() ); |
306 | 308 | } |
307 | 309 | |
@@ -322,17 +324,19 @@ discard block |
||
322 | 324 | public function limit() |
323 | 325 | { |
324 | 326 | $arguments = func_get_args(); |
325 | - if (count($arguments) == 1 && is_numeric($arguments[0])) |
|
326 | - $max = $arguments[0]; |
|
327 | - else |
|
328 | - self::throwUsageException("Bad arguments type and count for limit()"); |
|
327 | + if (count($arguments) == 1 && is_numeric($arguments[0])) { |
|
328 | + $max = $arguments[0]; |
|
329 | + } else { |
|
330 | + self::throwUsageException("Bad arguments type and count for limit()"); |
|
331 | + } |
|
329 | 332 | |
330 | 333 | $out = []; |
331 | 334 | $count = 0; |
332 | 335 | foreach ($this->data as $key => $row) { |
333 | 336 | |
334 | - if ($max <= $count) |
|
335 | - break; |
|
337 | + if ($max <= $count) { |
|
338 | + break; |
|
339 | + } |
|
336 | 340 | |
337 | 341 | $out[$key] = $row; |
338 | 342 | |
@@ -356,8 +360,9 @@ discard block |
||
356 | 360 | */ |
357 | 361 | public function append($new_rows, callable $conflict_resolver=null) |
358 | 362 | { |
359 | - if ($new_rows instanceof static) |
|
360 | - $new_rows = $new_rows->getArray(); |
|
363 | + if ($new_rows instanceof static) { |
|
364 | + $new_rows = $new_rows->getArray(); |
|
365 | + } |
|
361 | 366 | |
362 | 367 | if (!is_array($new_rows)) { |
363 | 368 | $this->throwUsageException( |
@@ -382,8 +387,7 @@ discard block |
||
382 | 387 | ]; |
383 | 388 | |
384 | 389 | call_user_func_array($conflict_resolver, $arguments); |
385 | - } |
|
386 | - else { |
|
390 | + } else { |
|
387 | 391 | $this->data[$key] = $new_row; |
388 | 392 | } |
389 | 393 | } |
@@ -493,8 +497,7 @@ discard block |
||
493 | 497 | foreach ($rows as $row_id => $joined_row) { |
494 | 498 | $out[$row_id] = $joined_row; |
495 | 499 | } |
496 | - } |
|
497 | - else { |
|
500 | + } else { |
|
498 | 501 | |
499 | 502 | if (!isset($rows)) { |
500 | 503 | echo json_encode([ |
@@ -504,8 +507,9 @@ discard block |
||
504 | 507 | exit; |
505 | 508 | } |
506 | 509 | |
507 | - foreach ($rowIdParts as $rowIdPartName => $rowIdPartValue) |
|
508 | - $row[$rowIdPartName] = $rowIdPartValue; |
|
510 | + foreach ($rowIdParts as $rowIdPartName => $rowIdPartValue) { |
|
511 | + $row[$rowIdPartName] = $rowIdPartValue; |
|
512 | + } |
|
509 | 513 | |
510 | 514 | $indexParts = []; |
511 | 515 | foreach ($rowIdParts as $name => $value) { |
@@ -556,8 +560,7 @@ discard block |
||
556 | 560 | if (is_string($value) && array_key_exists($value, $row)) { |
557 | 561 | $part_name .= $value; |
558 | 562 | $group_value = $row[ $value ]; |
559 | - } |
|
560 | - elseif (is_callable($value)) { |
|
563 | + } elseif (is_callable($value)) { |
|
561 | 564 | |
562 | 565 | if (is_string($value)) { |
563 | 566 | $part_name .= $value; |
@@ -566,20 +569,17 @@ discard block |
||
566 | 569 | elseif (is_object($value) && ($value instanceof Closure)) { |
567 | 570 | $part_name .= 'unnamed-closure-' |
568 | 571 | . hash('crc32b', var_export($value, true)); |
569 | - } |
|
570 | - elseif (is_array($value)) { |
|
572 | + } elseif (is_array($value)) { |
|
571 | 573 | $part_name .= implode('::', $value); |
572 | 574 | } |
573 | 575 | |
574 | 576 | $group_value = call_user_func_array($value, [ |
575 | 577 | $row, &$part_name |
576 | 578 | ]); |
577 | - } |
|
578 | - elseif (is_int($value)) { |
|
579 | + } elseif (is_int($value)) { |
|
579 | 580 | $part_name .= $value ? : '0'; |
580 | 581 | $group_value = $row[ $value ]; |
581 | - } |
|
582 | - else { |
|
582 | + } else { |
|
583 | 583 | self::throwUsageException( |
584 | 584 | 'Bad value provided for groupBy id generation: ' |
585 | 585 | .var_export($value, true) |
@@ -587,8 +587,9 @@ discard block |
||
587 | 587 | ); |
588 | 588 | } |
589 | 589 | |
590 | - if (!is_null($part_name)) |
|
591 | - $group_parts[ $part_name ] = $group_value; |
|
590 | + if (!is_null($part_name)) { |
|
591 | + $group_parts[ $part_name ] = $group_value; |
|
592 | + } |
|
592 | 593 | } |
593 | 594 | |
594 | 595 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -609,12 +610,12 @@ discard block |
||
609 | 610 | public function first($strict=false) |
610 | 611 | { |
611 | 612 | if (!$this->count()) { |
612 | - if ($strict) |
|
613 | - throw new \ErrorException("No first element found in this array"); |
|
614 | - else |
|
615 | - $first = null; |
|
616 | - } |
|
617 | - else { |
|
613 | + if ($strict) { |
|
614 | + throw new \ErrorException("No first element found in this array"); |
|
615 | + } else { |
|
616 | + $first = null; |
|
617 | + } |
|
618 | + } else { |
|
618 | 619 | $key = key($this->data); |
619 | 620 | $first = reset($this->data); |
620 | 621 | $this->move($key); |
@@ -631,12 +632,12 @@ discard block |
||
631 | 632 | public function last($strict=false) |
632 | 633 | { |
633 | 634 | if (!$this->count()) { |
634 | - if ($strict) |
|
635 | - throw new \ErrorException("No last element found in this array"); |
|
636 | - else |
|
637 | - $last = null; |
|
638 | - } |
|
639 | - else { |
|
635 | + if ($strict) { |
|
636 | + throw new \ErrorException("No last element found in this array"); |
|
637 | + } else { |
|
638 | + $last = null; |
|
639 | + } |
|
640 | + } else { |
|
640 | 641 | $key = key($this->data); |
641 | 642 | $last = end($this->data); |
642 | 643 | $this->move($key); |
@@ -651,12 +652,12 @@ discard block |
||
651 | 652 | public function firstKey($strict=false) |
652 | 653 | { |
653 | 654 | if (!$this->count()) { |
654 | - if ($strict) |
|
655 | - throw new \ErrorException("No last element found in this array"); |
|
656 | - else |
|
657 | - $firstKey = null; |
|
658 | - } |
|
659 | - else { |
|
655 | + if ($strict) { |
|
656 | + throw new \ErrorException("No last element found in this array"); |
|
657 | + } else { |
|
658 | + $firstKey = null; |
|
659 | + } |
|
660 | + } else { |
|
660 | 661 | $key = key($this->data); |
661 | 662 | reset($this->data); |
662 | 663 | $firstKey = key($this->data); |
@@ -672,12 +673,12 @@ discard block |
||
672 | 673 | public function lastKey($strict=false) |
673 | 674 | { |
674 | 675 | if (!$this->count()) { |
675 | - if ($strict) |
|
676 | - throw new \ErrorException("No last element found in this array"); |
|
677 | - else |
|
678 | - $lastKey = null; |
|
679 | - } |
|
680 | - else { |
|
676 | + if ($strict) { |
|
677 | + throw new \ErrorException("No last element found in this array"); |
|
678 | + } else { |
|
679 | + $lastKey = null; |
|
680 | + } |
|
681 | + } else { |
|
681 | 682 | $key = key($this->data); |
682 | 683 | end($this->data); |
683 | 684 | $lastKey = key($this->data); |
@@ -699,8 +700,7 @@ discard block |
||
699 | 700 | break; |
700 | 701 | } |
701 | 702 | } |
702 | - } |
|
703 | - elseif ($strict) { |
|
703 | + } elseif ($strict) { |
|
704 | 704 | throw new \ErrorException("Unable to move the internal pointer to a key that doesn't exist."); |
705 | 705 | } |
706 | 706 | |
@@ -767,8 +767,9 @@ discard block |
||
767 | 767 | 'data' => $this->data, |
768 | 768 | ]); |
769 | 769 | |
770 | - if ($exit) |
|
771 | - exit; |
|
770 | + if ($exit) { |
|
771 | + exit; |
|
772 | + } |
|
772 | 773 | |
773 | 774 | return $this; |
774 | 775 | } |