@@ -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 | } |
@@ -111,13 +111,13 @@ discard block |
||
| 111 | 111 | public static function mergeRecursiveCustom( |
| 112 | 112 | array $existing_row, |
| 113 | 113 | array $conflict_row, |
| 114 | - callable $merge_resolver=null, |
|
| 115 | - $max_depth=null |
|
| 116 | - ){ |
|
| 114 | + callable $merge_resolver = null, |
|
| 115 | + $max_depth = null |
|
| 116 | + ) { |
|
| 117 | 117 | foreach ($conflict_row as $column => $conflict_value) { |
| 118 | 118 | |
| 119 | 119 | // not existing in first array |
| 120 | - if (!isset($existing_row[$column])) { |
|
| 120 | + if ( ! isset($existing_row[$column])) { |
|
| 121 | 121 | $existing_row[$column] = $conflict_value; |
| 122 | 122 | continue; |
| 123 | 123 | } |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | } |
| 151 | 151 | else { |
| 152 | 152 | // same reslution as array_merge_recursive |
| 153 | - if (!is_array($existing_value)) { |
|
| 153 | + if ( ! is_array($existing_value)) { |
|
| 154 | 154 | $existing_row[$column] = [$existing_value]; |
| 155 | 155 | } |
| 156 | 156 | |
@@ -173,13 +173,13 @@ discard block |
||
| 173 | 173 | public static function mergePreservingDistincts( |
| 174 | 174 | array $existing_row, |
| 175 | 175 | array $conflict_row |
| 176 | - ){ |
|
| 176 | + ) { |
|
| 177 | 177 | return static::mergeRecursiveCustom( |
| 178 | 178 | $existing_row, |
| 179 | 179 | $conflict_row, |
| 180 | - function ($existing_value, $conflict_value, $column) { |
|
| 180 | + function($existing_value, $conflict_value, $column) { |
|
| 181 | 181 | |
| 182 | - if (!is_array($existing_value)) |
|
| 182 | + if ( ! is_array($existing_value)) |
|
| 183 | 183 | $existing_value = [$existing_value]; |
| 184 | 184 | |
| 185 | 185 | // We store the new value with their previous ones |
@@ -204,10 +204,10 @@ discard block |
||
| 204 | 204 | * |
| 205 | 205 | * @see mergePreservingDistincts() |
| 206 | 206 | */ |
| 207 | - public static function keepUniqueColumnValues(array $row, array $excluded_columns=[]) |
|
| 207 | + public static function keepUniqueColumnValues(array $row, array $excluded_columns = []) |
|
| 208 | 208 | { |
| 209 | 209 | foreach ($row as $column => &$values) { |
| 210 | - if (!is_array($values)) |
|
| 210 | + if ( ! is_array($values)) |
|
| 211 | 211 | continue; |
| 212 | 212 | |
| 213 | 213 | if (in_array($column, $excluded_columns)) |
@@ -237,7 +237,7 @@ discard block |
||
| 237 | 237 | */ |
| 238 | 238 | public function groupByTransformed( |
| 239 | 239 | callable $indexGenerator, |
| 240 | - callable $rowTransformer, // todo check this behavior |
|
| 240 | + callable $rowTransformer, // todo check this behavior |
|
| 241 | 241 | callable $conflictResolver ) |
| 242 | 242 | { |
| 243 | 243 | // The goal here is to remove the second parameter has it makes the |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | $out = []; |
| 251 | 251 | foreach ($this->data as $key => $row) { |
| 252 | 252 | |
| 253 | - if (!$row) |
|
| 253 | + if ( ! $row) |
|
| 254 | 254 | continue; |
| 255 | 255 | |
| 256 | 256 | $newIndex = call_user_func($indexGenerator, $key, $row); |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | ? call_user_func($rowTransformer, $row) |
| 260 | 260 | : $row; |
| 261 | 261 | |
| 262 | - if (!isset($out[$newIndex])) { |
|
| 262 | + if ( ! isset($out[$newIndex])) { |
|
| 263 | 263 | $out[$newIndex] = $transformedRow; |
| 264 | 264 | } |
| 265 | 265 | else { |
@@ -284,12 +284,12 @@ discard block |
||
| 284 | 284 | * rows have the same index. |
| 285 | 285 | * @return static |
| 286 | 286 | */ |
| 287 | - public function mergeWith( $otherTable, callable $conflictResolver=null ) |
|
| 287 | + public function mergeWith($otherTable, callable $conflictResolver = null) |
|
| 288 | 288 | { |
| 289 | 289 | if (is_array($otherTable)) |
| 290 | 290 | $otherTable = new static($otherTable); |
| 291 | 291 | |
| 292 | - if (!$otherTable instanceof static) { |
|
| 292 | + if ( ! $otherTable instanceof static) { |
|
| 293 | 293 | self::throwUsageException( |
| 294 | 294 | '$otherTable must be an array or an instance of '.static::class.' instead of: ' |
| 295 | 295 | .var_export($otherTable, true) |
@@ -299,7 +299,7 @@ discard block |
||
| 299 | 299 | $out = $this->data; |
| 300 | 300 | foreach ($otherTable->getArray() as $key => $row) { |
| 301 | 301 | |
| 302 | - if (!isset($out[$key])) { |
|
| 302 | + if ( ! isset($out[$key])) { |
|
| 303 | 303 | $out[$key] = $row; |
| 304 | 304 | } |
| 305 | 305 | else { |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | * (same as self::mergeWith with the other table as $this) |
| 328 | 328 | * @return static |
| 329 | 329 | */ |
| 330 | - public function mergeIn( $otherTable, callable $conflictResolver=null ) |
|
| 330 | + public function mergeIn($otherTable, callable $conflictResolver = null) |
|
| 331 | 331 | { |
| 332 | 332 | $otherTable->mergeWith($this, $conflictResolver); |
| 333 | 333 | return $this; |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | */ |
| 354 | 354 | public function each(callable $rowTransformer) |
| 355 | 355 | { |
| 356 | - $out = []; |
|
| 356 | + $out = []; |
|
| 357 | 357 | foreach ($this->data as $key => $row) { |
| 358 | 358 | $out[$key] = call_user_func_array( |
| 359 | 359 | $rowTransformer, |
@@ -383,7 +383,7 @@ discard block |
||
| 383 | 383 | */ |
| 384 | 384 | public function renameColumns(array $old_to_new_names) |
| 385 | 385 | { |
| 386 | - $out = []; |
|
| 386 | + $out = []; |
|
| 387 | 387 | foreach ($this->data as $key => $row) { |
| 388 | 388 | try { |
| 389 | 389 | foreach ($old_to_new_names as $old_name => $new_name) { |
@@ -392,7 +392,7 @@ discard block |
||
| 392 | 392 | } |
| 393 | 393 | } |
| 394 | 394 | catch (\Exception $e) { |
| 395 | - self::throwUsageException( $e->getMessage() ); |
|
| 395 | + self::throwUsageException($e->getMessage()); |
|
| 396 | 396 | } |
| 397 | 397 | |
| 398 | 398 | $out[$key] = $row; |
@@ -444,20 +444,20 @@ discard block |
||
| 444 | 444 | * nor a static. |
| 445 | 445 | * @return static $this |
| 446 | 446 | */ |
| 447 | - public function append($new_rows, callable $conflict_resolver=null) |
|
| 447 | + public function append($new_rows, callable $conflict_resolver = null) |
|
| 448 | 448 | { |
| 449 | 449 | if ($new_rows instanceof static) |
| 450 | 450 | $new_rows = $new_rows->getArray(); |
| 451 | 451 | |
| 452 | - if (!is_array($new_rows)) { |
|
| 452 | + if ( ! is_array($new_rows)) { |
|
| 453 | 453 | $this->throwUsageException( |
| 454 | - "\$new_rows parameter must be an array or an instance of " . __CLASS__ |
|
| 454 | + "\$new_rows parameter must be an array or an instance of ".__CLASS__ |
|
| 455 | 455 | ); |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - if (!$conflict_resolver) { |
|
| 458 | + if ( ! $conflict_resolver) { |
|
| 459 | 459 | // default conflict resolver: append with numeric key |
| 460 | - $conflict_resolver = function (&$data, $existing_row, $confliuct_row, $key) { |
|
| 460 | + $conflict_resolver = function(&$data, $existing_row, $confliuct_row, $key) { |
|
| 461 | 461 | $data[] = $confliuct_row; |
| 462 | 462 | }; |
| 463 | 463 | } |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | * |
| 490 | 490 | * @return static |
| 491 | 491 | */ |
| 492 | - public function dimensionsAsColumns(array $columnNames, array $options=null) |
|
| 492 | + public function dimensionsAsColumns(array $columnNames, array $options = null) |
|
| 493 | 493 | { |
| 494 | 494 | $out = $this->dimensionsAsColumns_recurser($this->data, $columnNames); |
| 495 | 495 | return $this->returnConstant($out); |
@@ -526,12 +526,12 @@ discard block |
||
| 526 | 526 | * ], |
| 527 | 527 | * ] |
| 528 | 528 | */ |
| 529 | - protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts=[]) |
|
| 529 | + protected function dimensionsAsColumns_recurser(array $data, $columnNames, $rowIdParts = []) |
|
| 530 | 530 | { |
| 531 | 531 | $out = []; |
| 532 | 532 | // if (!$columnNames) |
| 533 | 533 | // return $data; |
| 534 | - $no_more_column = !(bool) $columnNames; |
|
| 534 | + $no_more_column = ! (bool) $columnNames; |
|
| 535 | 535 | |
| 536 | 536 | // If all the names have been given to the dimensions |
| 537 | 537 | // we compile the index key of the row at the current level |
@@ -555,7 +555,7 @@ discard block |
||
| 555 | 555 | self::throwUsageException( |
| 556 | 556 | "Trying to populate a column '$name' that " |
| 557 | 557 | ."already exists with a different value " |
| 558 | - .var_export($data[$name], true). " => '$value'" |
|
| 558 | + .var_export($data[$name], true)." => '$value'" |
|
| 559 | 559 | ); |
| 560 | 560 | } |
| 561 | 561 | $data[$name] = $value; |
@@ -586,7 +586,7 @@ discard block |
||
| 586 | 586 | } |
| 587 | 587 | else { |
| 588 | 588 | |
| 589 | - if (!isset($rows)) { |
|
| 589 | + if ( ! isset($rows)) { |
|
| 590 | 590 | echo json_encode([ |
| 591 | 591 | '$rowIdParts' => $rowIdParts, |
| 592 | 592 | '$row' => $row, |
@@ -645,12 +645,12 @@ discard block |
||
| 645 | 645 | |
| 646 | 646 | if (is_string($value) && array_key_exists($value, $row)) { |
| 647 | 647 | $part_name .= $value; |
| 648 | - $group_value = $row[ $value ]; |
|
| 648 | + $group_value = $row[$value]; |
|
| 649 | 649 | } |
| 650 | 650 | elseif (is_callable($value)) { |
| 651 | 651 | |
| 652 | 652 | if (is_string($value)) { |
| 653 | - $part_name .= $value; |
|
| 653 | + $part_name .= $value; |
|
| 654 | 654 | } |
| 655 | 655 | // elseif (is_function($value)) { |
| 656 | 656 | elseif (is_object($value) && ($value instanceof Closure)) { |
@@ -666,19 +666,19 @@ discard block |
||
| 666 | 666 | ]); |
| 667 | 667 | } |
| 668 | 668 | elseif (is_int($value)) { |
| 669 | - $part_name .= $value ? : '0'; |
|
| 670 | - $group_value = $row[ $value ]; |
|
| 669 | + $part_name .= $value ?: '0'; |
|
| 670 | + $group_value = $row[$value]; |
|
| 671 | 671 | } |
| 672 | 672 | else { |
| 673 | 673 | self::throwUsageException( |
| 674 | 674 | 'Bad value provided for groupBy id generation: ' |
| 675 | 675 | .var_export($value, true) |
| 676 | - ."\n" . var_export($row, true) |
|
| 676 | + ."\n".var_export($row, true) |
|
| 677 | 677 | ); |
| 678 | 678 | } |
| 679 | 679 | |
| 680 | - if (!is_null($part_name)) |
|
| 681 | - $group_parts[ $part_name ] = $group_value; |
|
| 680 | + if ( ! is_null($part_name)) |
|
| 681 | + $group_parts[$part_name] = $group_value; |
|
| 682 | 682 | } |
| 683 | 683 | |
| 684 | 684 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -696,9 +696,9 @@ discard block |
||
| 696 | 696 | /** |
| 697 | 697 | * Returns the first element of the array |
| 698 | 698 | */ |
| 699 | - public function first($strict=false) |
|
| 699 | + public function first($strict = false) |
|
| 700 | 700 | { |
| 701 | - if (!$this->count()) { |
|
| 701 | + if ( ! $this->count()) { |
|
| 702 | 702 | if ($strict) |
| 703 | 703 | throw new \ErrorException("No first element found in this array"); |
| 704 | 704 | else |
@@ -718,9 +718,9 @@ discard block |
||
| 718 | 718 | * |
| 719 | 719 | * @todo Preserve the offset |
| 720 | 720 | */ |
| 721 | - public function last($strict=false) |
|
| 721 | + public function last($strict = false) |
|
| 722 | 722 | { |
| 723 | - if (!$this->count()) { |
|
| 723 | + if ( ! $this->count()) { |
|
| 724 | 724 | if ($strict) |
| 725 | 725 | throw new \ErrorException("No last element found in this array"); |
| 726 | 726 | else |
@@ -738,9 +738,9 @@ discard block |
||
| 738 | 738 | /** |
| 739 | 739 | * |
| 740 | 740 | */ |
| 741 | - public function firstKey($strict=false) |
|
| 741 | + public function firstKey($strict = false) |
|
| 742 | 742 | { |
| 743 | - if (!$this->count()) { |
|
| 743 | + if ( ! $this->count()) { |
|
| 744 | 744 | if ($strict) |
| 745 | 745 | throw new \ErrorException("No last element found in this array"); |
| 746 | 746 | else |
@@ -759,16 +759,16 @@ discard block |
||
| 759 | 759 | /** |
| 760 | 760 | * |
| 761 | 761 | */ |
| 762 | - public function lastKey($strict=false) |
|
| 762 | + public function lastKey($strict = false) |
|
| 763 | 763 | { |
| 764 | - if (!$this->count()) { |
|
| 764 | + if ( ! $this->count()) { |
|
| 765 | 765 | if ($strict) |
| 766 | 766 | throw new \ErrorException("No last element found in this array"); |
| 767 | 767 | else |
| 768 | 768 | $lastKey = null; |
| 769 | 769 | } |
| 770 | 770 | else { |
| 771 | - $key = key($this->data); |
|
| 771 | + $key = key($this->data); |
|
| 772 | 772 | end($this->data); |
| 773 | 773 | $lastKey = key($this->data); |
| 774 | 774 | $this->move($key); |
@@ -780,7 +780,7 @@ discard block |
||
| 780 | 780 | /** |
| 781 | 781 | * Move the internal pointer of the array to the key given as parameter |
| 782 | 782 | */ |
| 783 | - public function move($key, $strict=true) |
|
| 783 | + public function move($key, $strict = true) |
|
| 784 | 784 | { |
| 785 | 785 | if (array_key_exists($key, $this->data)) { |
| 786 | 786 | foreach ($this->data as $i => &$value) { |
@@ -847,13 +847,13 @@ discard block |
||
| 847 | 847 | * @see http://php.net/manual/fr/function.var-dump.php |
| 848 | 848 | * @todo Handle xdebug dump formatting |
| 849 | 849 | */ |
| 850 | - public function dump($exit=false) |
|
| 850 | + public function dump($exit = false) |
|
| 851 | 851 | { |
| 852 | 852 | $bt = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
| 853 | 853 | $caller = $bt[0]; |
| 854 | 854 | |
| 855 | 855 | var_export([ |
| 856 | - 'location' => $caller['file'] . ':' . $caller['line'], |
|
| 856 | + 'location' => $caller['file'].':'.$caller['line'], |
|
| 857 | 857 | 'data' => $this->data, |
| 858 | 858 | ]); |
| 859 | 859 | |
@@ -870,7 +870,7 @@ discard block |
||
| 870 | 870 | * @todo move it to an Arrays class storing static methods |
| 871 | 871 | */ |
| 872 | 872 | public static function replaceEntries( |
| 873 | - array $array, callable $replacer, $max_depth=null |
|
| 873 | + array $array, callable $replacer, $max_depth = null |
|
| 874 | 874 | ) { |
| 875 | 875 | foreach ($array as $key => &$row) { |
| 876 | 876 | $arguments = [&$row, $key]; |
@@ -878,7 +878,7 @@ discard block |
||
| 878 | 878 | |
| 879 | 879 | if (is_array($row) && $max_depth !== 0) { // allowing null to have no depth limit |
| 880 | 880 | $row = self::replaceEntries( |
| 881 | - $row, $replacer, $max_depth ? $max_depth-1 : $max_depth |
|
| 881 | + $row, $replacer, $max_depth ? $max_depth - 1 : $max_depth |
|
| 882 | 882 | ); |
| 883 | 883 | } |
| 884 | 884 | } |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | * |
| 895 | 895 | * @return static $this or a new static. |
| 896 | 896 | */ |
| 897 | - public function extract($callback=null) |
|
| 897 | + public function extract($callback = null) |
|
| 898 | 898 | { |
| 899 | 899 | if ($callback) { |
| 900 | 900 | |
@@ -902,7 +902,7 @@ discard block |
||
| 902 | 902 | $callback = new \JClaveau\LogicalFilter\LogicalFilter($callback); |
| 903 | 903 | } |
| 904 | 904 | |
| 905 | - if (!is_callable($callback)) { |
|
| 905 | + if ( ! is_callable($callback)) { |
|
| 906 | 906 | $this->throwUsageException( |
| 907 | 907 | "\$callback must be a logical filter description array or a callable" |
| 908 | 908 | ." instead of " |
@@ -914,7 +914,7 @@ discard block |
||
| 914 | 914 | foreach ($this->data as $key => $value) { |
| 915 | 915 | if ($callback($value, $key)) { |
| 916 | 916 | $out[$key] = $value; |
| 917 | - unset( $this->data[$key] ); |
|
| 917 | + unset($this->data[$key]); |
|
| 918 | 918 | } |
| 919 | 919 | } |
| 920 | 920 | } |
@@ -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 | } |
@@ -147,8 +149,7 @@ discard block |
||
| 147 | 149 | $column, |
| 148 | 150 | ] |
| 149 | 151 | ); |
| 150 | - } |
|
| 151 | - else { |
|
| 152 | + } else { |
|
| 152 | 153 | // same reslution as array_merge_recursive |
| 153 | 154 | if (!is_array($existing_value)) { |
| 154 | 155 | $existing_row[$column] = [$existing_value]; |
@@ -179,8 +180,9 @@ discard block |
||
| 179 | 180 | $conflict_row, |
| 180 | 181 | function ($existing_value, $conflict_value, $column) { |
| 181 | 182 | |
| 182 | - if (!is_array($existing_value)) |
|
| 183 | - $existing_value = [$existing_value]; |
|
| 183 | + if (!is_array($existing_value)) { |
|
| 184 | + $existing_value = [$existing_value]; |
|
| 185 | + } |
|
| 184 | 186 | |
| 185 | 187 | // We store the new value with their previous ones |
| 186 | 188 | if ( ! is_array($conflict_value)) { |
@@ -207,15 +209,18 @@ discard block |
||
| 207 | 209 | public static function keepUniqueColumnValues(array $row, array $excluded_columns=[]) |
| 208 | 210 | { |
| 209 | 211 | foreach ($row as $column => &$values) { |
| 210 | - if (!is_array($values)) |
|
| 211 | - continue; |
|
| 212 | + if (!is_array($values)) { |
|
| 213 | + continue; |
|
| 214 | + } |
|
| 212 | 215 | |
| 213 | - if (in_array($column, $excluded_columns)) |
|
| 214 | - continue; |
|
| 216 | + if (in_array($column, $excluded_columns)) { |
|
| 217 | + continue; |
|
| 218 | + } |
|
| 215 | 219 | |
| 216 | 220 | $values = array_unique($values); |
| 217 | - if (count($values) == 1) |
|
| 218 | - $values = $values[0]; |
|
| 221 | + if (count($values) == 1) { |
|
| 222 | + $values = $values[0]; |
|
| 223 | + } |
|
| 219 | 224 | } |
| 220 | 225 | |
| 221 | 226 | return $row; |
@@ -250,8 +255,9 @@ discard block |
||
| 250 | 255 | $out = []; |
| 251 | 256 | foreach ($this->data as $key => $row) { |
| 252 | 257 | |
| 253 | - if (!$row) |
|
| 254 | - continue; |
|
| 258 | + if (!$row) { |
|
| 259 | + continue; |
|
| 260 | + } |
|
| 255 | 261 | |
| 256 | 262 | $newIndex = call_user_func($indexGenerator, $key, $row); |
| 257 | 263 | |
@@ -261,8 +267,7 @@ discard block |
||
| 261 | 267 | |
| 262 | 268 | if (!isset($out[$newIndex])) { |
| 263 | 269 | $out[$newIndex] = $transformedRow; |
| 264 | - } |
|
| 265 | - else { |
|
| 270 | + } else { |
|
| 266 | 271 | $out[$newIndex] = call_user_func( |
| 267 | 272 | $conflictResolver, |
| 268 | 273 | $newIndex, |
@@ -286,8 +291,9 @@ discard block |
||
| 286 | 291 | */ |
| 287 | 292 | public function mergeWith( $otherTable, callable $conflictResolver=null ) |
| 288 | 293 | { |
| 289 | - if (is_array($otherTable)) |
|
| 290 | - $otherTable = new static($otherTable); |
|
| 294 | + if (is_array($otherTable)) { |
|
| 295 | + $otherTable = new static($otherTable); |
|
| 296 | + } |
|
| 291 | 297 | |
| 292 | 298 | if (!$otherTable instanceof static) { |
| 293 | 299 | self::throwUsageException( |
@@ -301,10 +307,10 @@ discard block |
||
| 301 | 307 | |
| 302 | 308 | if (!isset($out[$key])) { |
| 303 | 309 | $out[$key] = $row; |
| 304 | - } |
|
| 305 | - else { |
|
| 306 | - if ($conflictResolver === null) |
|
| 307 | - self::throwUsageException('No conflict resolver for a merge provoking one'); |
|
| 310 | + } else { |
|
| 311 | + if ($conflictResolver === null) { |
|
| 312 | + self::throwUsageException('No conflict resolver for a merge provoking one'); |
|
| 313 | + } |
|
| 308 | 314 | |
| 309 | 315 | $arguments = [ |
| 310 | 316 | &$key, |
@@ -390,8 +396,7 @@ discard block |
||
| 390 | 396 | $row[$new_name] = $row[$old_name]; |
| 391 | 397 | unset($row[$old_name]); |
| 392 | 398 | } |
| 393 | - } |
|
| 394 | - catch (\Exception $e) { |
|
| 399 | + } catch (\Exception $e) { |
|
| 395 | 400 | self::throwUsageException( $e->getMessage() ); |
| 396 | 401 | } |
| 397 | 402 | |
@@ -412,17 +417,19 @@ discard block |
||
| 412 | 417 | public function limit() |
| 413 | 418 | { |
| 414 | 419 | $arguments = func_get_args(); |
| 415 | - if (count($arguments) == 1 && is_numeric($arguments[0])) |
|
| 416 | - $max = $arguments[0]; |
|
| 417 | - else |
|
| 418 | - self::throwUsageException("Bad arguments type and count for limit()"); |
|
| 420 | + if (count($arguments) == 1 && is_numeric($arguments[0])) { |
|
| 421 | + $max = $arguments[0]; |
|
| 422 | + } else { |
|
| 423 | + self::throwUsageException("Bad arguments type and count for limit()"); |
|
| 424 | + } |
|
| 419 | 425 | |
| 420 | 426 | $out = []; |
| 421 | 427 | $count = 0; |
| 422 | 428 | foreach ($this->data as $key => $row) { |
| 423 | 429 | |
| 424 | - if ($max <= $count) |
|
| 425 | - break; |
|
| 430 | + if ($max <= $count) { |
|
| 431 | + break; |
|
| 432 | + } |
|
| 426 | 433 | |
| 427 | 434 | $out[$key] = $row; |
| 428 | 435 | |
@@ -446,8 +453,9 @@ discard block |
||
| 446 | 453 | */ |
| 447 | 454 | public function append($new_rows, callable $conflict_resolver=null) |
| 448 | 455 | { |
| 449 | - if ($new_rows instanceof static) |
|
| 450 | - $new_rows = $new_rows->getArray(); |
|
| 456 | + if ($new_rows instanceof static) { |
|
| 457 | + $new_rows = $new_rows->getArray(); |
|
| 458 | + } |
|
| 451 | 459 | |
| 452 | 460 | if (!is_array($new_rows)) { |
| 453 | 461 | $this->throwUsageException( |
@@ -472,8 +480,7 @@ discard block |
||
| 472 | 480 | ]; |
| 473 | 481 | |
| 474 | 482 | call_user_func_array($conflict_resolver, $arguments); |
| 475 | - } |
|
| 476 | - else { |
|
| 483 | + } else { |
|
| 477 | 484 | $this->data[$key] = $new_row; |
| 478 | 485 | } |
| 479 | 486 | } |
@@ -583,8 +590,7 @@ discard block |
||
| 583 | 590 | foreach ($rows as $row_id => $joined_row) { |
| 584 | 591 | $out[$row_id] = $joined_row; |
| 585 | 592 | } |
| 586 | - } |
|
| 587 | - else { |
|
| 593 | + } else { |
|
| 588 | 594 | |
| 589 | 595 | if (!isset($rows)) { |
| 590 | 596 | echo json_encode([ |
@@ -594,8 +600,9 @@ discard block |
||
| 594 | 600 | exit; |
| 595 | 601 | } |
| 596 | 602 | |
| 597 | - foreach ($rowIdParts as $rowIdPartName => $rowIdPartValue) |
|
| 598 | - $row[$rowIdPartName] = $rowIdPartValue; |
|
| 603 | + foreach ($rowIdParts as $rowIdPartName => $rowIdPartValue) { |
|
| 604 | + $row[$rowIdPartName] = $rowIdPartValue; |
|
| 605 | + } |
|
| 599 | 606 | |
| 600 | 607 | $indexParts = []; |
| 601 | 608 | foreach ($rowIdParts as $name => $value) { |
@@ -646,8 +653,7 @@ discard block |
||
| 646 | 653 | if (is_string($value) && array_key_exists($value, $row)) { |
| 647 | 654 | $part_name .= $value; |
| 648 | 655 | $group_value = $row[ $value ]; |
| 649 | - } |
|
| 650 | - elseif (is_callable($value)) { |
|
| 656 | + } elseif (is_callable($value)) { |
|
| 651 | 657 | |
| 652 | 658 | if (is_string($value)) { |
| 653 | 659 | $part_name .= $value; |
@@ -656,20 +662,17 @@ discard block |
||
| 656 | 662 | elseif (is_object($value) && ($value instanceof Closure)) { |
| 657 | 663 | $part_name .= 'unnamed-closure-' |
| 658 | 664 | . hash('crc32b', var_export($value, true)); |
| 659 | - } |
|
| 660 | - elseif (is_array($value)) { |
|
| 665 | + } elseif (is_array($value)) { |
|
| 661 | 666 | $part_name .= implode('::', $value); |
| 662 | 667 | } |
| 663 | 668 | |
| 664 | 669 | $group_value = call_user_func_array($value, [ |
| 665 | 670 | $row, &$part_name |
| 666 | 671 | ]); |
| 667 | - } |
|
| 668 | - elseif (is_int($value)) { |
|
| 672 | + } elseif (is_int($value)) { |
|
| 669 | 673 | $part_name .= $value ? : '0'; |
| 670 | 674 | $group_value = $row[ $value ]; |
| 671 | - } |
|
| 672 | - else { |
|
| 675 | + } else { |
|
| 673 | 676 | self::throwUsageException( |
| 674 | 677 | 'Bad value provided for groupBy id generation: ' |
| 675 | 678 | .var_export($value, true) |
@@ -677,8 +680,9 @@ discard block |
||
| 677 | 680 | ); |
| 678 | 681 | } |
| 679 | 682 | |
| 680 | - if (!is_null($part_name)) |
|
| 681 | - $group_parts[ $part_name ] = $group_value; |
|
| 683 | + if (!is_null($part_name)) { |
|
| 684 | + $group_parts[ $part_name ] = $group_value; |
|
| 685 | + } |
|
| 682 | 686 | } |
| 683 | 687 | |
| 684 | 688 | // sort the groups by names (without it the same group could have multiple ids) |
@@ -699,12 +703,12 @@ discard block |
||
| 699 | 703 | public function first($strict=false) |
| 700 | 704 | { |
| 701 | 705 | if (!$this->count()) { |
| 702 | - if ($strict) |
|
| 703 | - throw new \ErrorException("No first element found in this array"); |
|
| 704 | - else |
|
| 705 | - $first = null; |
|
| 706 | - } |
|
| 707 | - else { |
|
| 706 | + if ($strict) { |
|
| 707 | + throw new \ErrorException("No first element found in this array"); |
|
| 708 | + } else { |
|
| 709 | + $first = null; |
|
| 710 | + } |
|
| 711 | + } else { |
|
| 708 | 712 | $key = key($this->data); |
| 709 | 713 | $first = reset($this->data); |
| 710 | 714 | $this->move($key); |
@@ -721,12 +725,12 @@ discard block |
||
| 721 | 725 | public function last($strict=false) |
| 722 | 726 | { |
| 723 | 727 | if (!$this->count()) { |
| 724 | - if ($strict) |
|
| 725 | - throw new \ErrorException("No last element found in this array"); |
|
| 726 | - else |
|
| 727 | - $last = null; |
|
| 728 | - } |
|
| 729 | - else { |
|
| 728 | + if ($strict) { |
|
| 729 | + throw new \ErrorException("No last element found in this array"); |
|
| 730 | + } else { |
|
| 731 | + $last = null; |
|
| 732 | + } |
|
| 733 | + } else { |
|
| 730 | 734 | $key = key($this->data); |
| 731 | 735 | $last = end($this->data); |
| 732 | 736 | $this->move($key); |
@@ -741,12 +745,12 @@ discard block |
||
| 741 | 745 | public function firstKey($strict=false) |
| 742 | 746 | { |
| 743 | 747 | if (!$this->count()) { |
| 744 | - if ($strict) |
|
| 745 | - throw new \ErrorException("No last element found in this array"); |
|
| 746 | - else |
|
| 747 | - $firstKey = null; |
|
| 748 | - } |
|
| 749 | - else { |
|
| 748 | + if ($strict) { |
|
| 749 | + throw new \ErrorException("No last element found in this array"); |
|
| 750 | + } else { |
|
| 751 | + $firstKey = null; |
|
| 752 | + } |
|
| 753 | + } else { |
|
| 750 | 754 | $key = key($this->data); |
| 751 | 755 | reset($this->data); |
| 752 | 756 | $firstKey = key($this->data); |
@@ -762,12 +766,12 @@ discard block |
||
| 762 | 766 | public function lastKey($strict=false) |
| 763 | 767 | { |
| 764 | 768 | if (!$this->count()) { |
| 765 | - if ($strict) |
|
| 766 | - throw new \ErrorException("No last element found in this array"); |
|
| 767 | - else |
|
| 768 | - $lastKey = null; |
|
| 769 | - } |
|
| 770 | - else { |
|
| 769 | + if ($strict) { |
|
| 770 | + throw new \ErrorException("No last element found in this array"); |
|
| 771 | + } else { |
|
| 772 | + $lastKey = null; |
|
| 773 | + } |
|
| 774 | + } else { |
|
| 771 | 775 | $key = key($this->data); |
| 772 | 776 | end($this->data); |
| 773 | 777 | $lastKey = key($this->data); |
@@ -789,8 +793,7 @@ discard block |
||
| 789 | 793 | break; |
| 790 | 794 | } |
| 791 | 795 | } |
| 792 | - } |
|
| 793 | - elseif ($strict) { |
|
| 796 | + } elseif ($strict) { |
|
| 794 | 797 | throw new \ErrorException("Unable to move the internal pointer to a key that doesn't exist."); |
| 795 | 798 | } |
| 796 | 799 | |
@@ -857,8 +860,9 @@ discard block |
||
| 857 | 860 | 'data' => $this->data, |
| 858 | 861 | ]); |
| 859 | 862 | |
| 860 | - if ($exit) |
|
| 861 | - exit; |
|
| 863 | + if ($exit) { |
|
| 864 | + exit; |
|
| 865 | + } |
|
| 862 | 866 | |
| 863 | 867 | return $this; |
| 864 | 868 | } |