@@ -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 | |
@@ -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 | } |