@@ -31,7 +31,7 @@ discard block |
||
31 | 31 | |
32 | 32 | /** |
33 | 33 | * @internal |
34 | - * @param \Closure|\Iterator $iterator |
|
34 | + * @param \Closure $iterator |
|
35 | 35 | * @param bool $isClosure |
36 | 36 | */ |
37 | 37 | private function __construct($iterator, $isClosure = true) |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | * <p>Three methods are defined to extend the type {@link OrderedEnumerable}, which is the return type of this method. These three methods, namely {@link OrderedEnumerable::thenBy thenBy}, {@link OrderedEnumerable::thenByDescending thenByDescending} and {@link OrderedEnumerable::thenByDir thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made. |
214 | 214 | * <p>Because OrderedEnumerable inherits from Enumerable, you can call {@link orderBy}, {@link orderByDescending} or {@link orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering. |
215 | 215 | * <p>This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used. |
216 | - * @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value). |
|
216 | + * @param boolean $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value). |
|
217 | 217 | * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value. |
218 | 218 | * @param callable|int|null $comparer {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b. Can also be a combination of SORT_ flags. |
219 | 219 | * @return OrderedEnumerable |
@@ -920,7 +920,7 @@ discard block |
||
920 | 920 | |
921 | 921 | /** |
922 | 922 | * Proc for {@link toArrayDeep}. |
923 | - * @param $enum \Traversable Source sequence. |
|
923 | + * @param Enumerable $enum \Traversable Source sequence. |
|
924 | 924 | * @return array An array that contains the elements from the input sequence. |
925 | 925 | * @package YaLinqo\Conversion |
926 | 926 | */ |
@@ -970,7 +970,7 @@ discard block |
||
970 | 970 | |
971 | 971 | /** |
972 | 972 | * Proc for {@link toListDeep}. |
973 | - * @param $enum \Traversable Source sequence. |
|
973 | + * @param Enumerable $enum \Traversable Source sequence. |
|
974 | 974 | * @return array An array that contains the elements from the input sequence. |
975 | 975 | * @package YaLinqo\Conversion |
976 | 976 | */ |
@@ -75,22 +75,22 @@ discard block |
||
75 | 75 | { |
76 | 76 | switch ($type) { |
77 | 77 | case 'array': |
78 | - return $this->select(function($v) { return (array)$v; }); |
|
78 | + return $this->select(function($v) { return (array) $v; }); |
|
79 | 79 | case 'int': |
80 | 80 | case 'integer': |
81 | 81 | case 'long': |
82 | - return $this->select(function($v) { return (int)$v; }); |
|
82 | + return $this->select(function($v) { return (int) $v; }); |
|
83 | 83 | case 'float': |
84 | 84 | case 'real': |
85 | 85 | case 'double': |
86 | - return $this->select(function($v) { return (float)$v; }); |
|
86 | + return $this->select(function($v) { return (float) $v; }); |
|
87 | 87 | case 'null': |
88 | 88 | case 'unset': |
89 | 89 | return $this->select(function($v) { return null; }); |
90 | 90 | case 'object': |
91 | - return $this->select(function($v) { return (object)$v; }); |
|
91 | + return $this->select(function($v) { return (object) $v; }); |
|
92 | 92 | case 'string': |
93 | - return $this->select(function($v) { return (string)$v; }); |
|
93 | + return $this->select(function($v) { return (string) $v; }); |
|
94 | 94 | default: |
95 | 95 | throw new \InvalidArgumentException(Errors::UNSUPPORTED_BUILTIN_TYPE); |
96 | 96 | } |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | $outerKeySelector = Utils::createLambda($outerKeySelector, 'v,k', Functions::$key); |
285 | 285 | $innerKeySelector = Utils::createLambda($innerKeySelector, 'v,k', Functions::$key); |
286 | 286 | /** @noinspection PhpUnusedParameterInspection */ |
287 | - $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,e,k', function($v, $e, $k) { return [ $v, $e ]; }); |
|
287 | + $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,e,k', function($v, $e, $k) { return [$v, $e]; }); |
|
288 | 288 | /** @noinspection PhpUnusedParameterInspection */ |
289 | 289 | $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v,e,k', function($v, $e, $k) { return $k; }); |
290 | 290 | |
@@ -318,7 +318,7 @@ discard block |
||
318 | 318 | $outerKeySelector = Utils::createLambda($outerKeySelector, 'v,k', Functions::$key); |
319 | 319 | $innerKeySelector = Utils::createLambda($innerKeySelector, 'v,k', Functions::$key); |
320 | 320 | /** @noinspection PhpUnusedParameterInspection */ |
321 | - $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v1,v2,k', function($v1, $v2, $k) { return [ $v1, $v2 ]; }); |
|
321 | + $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v1,v2,k', function($v1, $v2, $k) { return [$v1, $v2]; }); |
|
322 | 322 | /** @noinspection PhpUnusedParameterInspection */ |
323 | 323 | $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v1,v2,k', function($v1, $v2, $k) { return $k; }); |
324 | 324 |
@@ -150,8 +150,9 @@ discard block |
||
150 | 150 | $selectorKey = Utils::createLambda($selectorKey, 'v,k', Functions::$key); |
151 | 151 | |
152 | 152 | return new self(function() use ($selectorValue, $selectorKey) { |
153 | - foreach ($this as $k => $v) |
|
154 | - yield $selectorKey($v, $k) => $selectorValue($v, $k); |
|
153 | + foreach ($this as $k => $v) { |
|
154 | + yield $selectorKey($v, $k) => $selectorValue($v, $k); |
|
155 | + } |
|
155 | 156 | }); |
156 | 157 | } |
157 | 158 | |
@@ -175,13 +176,15 @@ discard block |
||
175 | 176 | $collectionSelector = Utils::createLambda($collectionSelector, 'v,k', Functions::$value); |
176 | 177 | $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,k1,k2', Functions::$value); |
177 | 178 | $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v,k1,k2', false); |
178 | - if ($resultSelectorKey === false) |
|
179 | - $resultSelectorKey = Functions::increment(); |
|
179 | + if ($resultSelectorKey === false) { |
|
180 | + $resultSelectorKey = Functions::increment(); |
|
181 | + } |
|
180 | 182 | |
181 | 183 | return new self(function() use ($collectionSelector, $resultSelectorValue, $resultSelectorKey) { |
182 | - foreach ($this as $ok => $ov) |
|
183 | - foreach ($collectionSelector($ov, $ok) as $ik => $iv) |
|
184 | + foreach ($this as $ok => $ov) { |
|
185 | + foreach ($collectionSelector($ov, $ok) as $ik => $iv) |
|
184 | 186 | yield $resultSelectorKey($iv, $ok, $ik) => $resultSelectorValue($iv, $ok, $ik); |
187 | + } |
|
185 | 188 | }); |
186 | 189 | } |
187 | 190 | |
@@ -197,9 +200,10 @@ discard block |
||
197 | 200 | $predicate = Utils::createLambda($predicate, 'v,k'); |
198 | 201 | |
199 | 202 | return new self(function() use ($predicate) { |
200 | - foreach ($this as $k => $v) |
|
201 | - if ($predicate($v, $k)) |
|
203 | + foreach ($this as $k => $v) { |
|
204 | + if ($predicate($v, $k)) |
|
202 | 205 | yield $k => $v; |
206 | + } |
|
203 | 207 | }); |
204 | 208 | } |
205 | 209 | |
@@ -326,9 +330,10 @@ discard block |
||
326 | 330 | $lookup = $inner->toLookup($innerKeySelector); |
327 | 331 | foreach ($this as $ok => $ov) { |
328 | 332 | $key = $outerKeySelector($ov, $ok); |
329 | - if (isset($lookup[$key])) |
|
330 | - foreach ($lookup[$key] as $iv) |
|
333 | + if (isset($lookup[$key])) { |
|
334 | + foreach ($lookup[$key] as $iv) |
|
331 | 335 | yield $resultSelectorKey($ov, $iv, $key) => $resultSelectorValue($ov, $iv, $key); |
336 | + } |
|
332 | 337 | } |
333 | 338 | }); |
334 | 339 | } |
@@ -386,20 +391,19 @@ discard block |
||
386 | 391 | foreach ($this as $k => $v) { |
387 | 392 | $result = $func($result, $v, $k); |
388 | 393 | } |
389 | - } |
|
390 | - else { |
|
394 | + } else { |
|
391 | 395 | $assigned = false; |
392 | 396 | foreach ($this as $k => $v) { |
393 | 397 | if ($assigned) { |
394 | 398 | $result = $func($result, $v, $k); |
395 | - } |
|
396 | - else { |
|
399 | + } else { |
|
397 | 400 | $result = $v; |
398 | 401 | $assigned = true; |
399 | 402 | } |
400 | 403 | } |
401 | - if (!$assigned) |
|
402 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
404 | + if (!$assigned) { |
|
405 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
406 | + } |
|
403 | 407 | } |
404 | 408 | return $result; |
405 | 409 | } |
@@ -426,13 +430,11 @@ discard block |
||
426 | 430 | $result = $func($result, $v, $k); |
427 | 431 | $assigned = true; |
428 | 432 | } |
429 | - } |
|
430 | - else { |
|
433 | + } else { |
|
431 | 434 | foreach ($this as $k => $v) { |
432 | 435 | if ($assigned) { |
433 | 436 | $result = $func($result, $v, $k); |
434 | - } |
|
435 | - else { |
|
437 | + } else { |
|
436 | 438 | $result = $v; |
437 | 439 | $assigned = true; |
438 | 440 | } |
@@ -461,8 +463,9 @@ discard block |
||
461 | 463 | $sum += $selector($v, $k); |
462 | 464 | $count++; |
463 | 465 | } |
464 | - if ($count === 0) |
|
465 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
466 | + if ($count === 0) { |
|
467 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
468 | + } |
|
466 | 469 | return $sum / $count; |
467 | 470 | } |
468 | 471 | |
@@ -480,15 +483,17 @@ discard block |
||
480 | 483 | { |
481 | 484 | $it = $this->getIterator(); |
482 | 485 | |
483 | - if ($it instanceof \Countable && $predicate === null) |
|
484 | - return count($it); |
|
486 | + if ($it instanceof \Countable && $predicate === null) { |
|
487 | + return count($it); |
|
488 | + } |
|
485 | 489 | |
486 | 490 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$value); |
487 | 491 | $count = 0; |
488 | 492 | |
489 | - foreach ($it as $k => $v) |
|
490 | - if ($predicate($v, $k)) |
|
493 | + foreach ($it as $k => $v) { |
|
494 | + if ($predicate($v, $k)) |
|
491 | 495 | $count++; |
496 | + } |
|
492 | 497 | return $count; |
493 | 498 | } |
494 | 499 | |
@@ -513,8 +518,9 @@ discard block |
||
513 | 518 | $max = max($max, $selector($v, $k)); |
514 | 519 | $assigned = true; |
515 | 520 | } |
516 | - if (!$assigned) |
|
517 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
521 | + if (!$assigned) { |
|
522 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
523 | + } |
|
518 | 524 | return $max; |
519 | 525 | } |
520 | 526 | |
@@ -535,8 +541,9 @@ discard block |
||
535 | 541 | $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict); |
536 | 542 | $enum = $this; |
537 | 543 | |
538 | - if ($selector !== null) |
|
539 | - $enum = $enum->select($selector); |
|
544 | + if ($selector !== null) { |
|
545 | + $enum = $enum->select($selector); |
|
546 | + } |
|
540 | 547 | return $enum->aggregate(function($a, $b) use ($comparer) { return $comparer($a, $b) > 0 ? $a : $b; }); |
541 | 548 | } |
542 | 549 | |
@@ -561,8 +568,9 @@ discard block |
||
561 | 568 | $min = min($min, $selector($v, $k)); |
562 | 569 | $assigned = true; |
563 | 570 | } |
564 | - if (!$assigned) |
|
565 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
571 | + if (!$assigned) { |
|
572 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
573 | + } |
|
566 | 574 | return $min; |
567 | 575 | } |
568 | 576 | |
@@ -583,8 +591,9 @@ discard block |
||
583 | 591 | $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict); |
584 | 592 | $enum = $this; |
585 | 593 | |
586 | - if ($selector !== null) |
|
587 | - $enum = $enum->select($selector); |
|
594 | + if ($selector !== null) { |
|
595 | + $enum = $enum->select($selector); |
|
596 | + } |
|
588 | 597 | return $enum->aggregate(function($a, $b) use ($comparer) { return $comparer($a, $b) < 0 ? $a : $b; }); |
589 | 598 | } |
590 | 599 | |
@@ -604,8 +613,9 @@ discard block |
||
604 | 613 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
605 | 614 | |
606 | 615 | $sum = 0; |
607 | - foreach ($this as $k => $v) |
|
608 | - $sum += $selector($v, $k); |
|
616 | + foreach ($this as $k => $v) { |
|
617 | + $sum += $selector($v, $k); |
|
618 | + } |
|
609 | 619 | return $sum; |
610 | 620 | } |
611 | 621 | |
@@ -626,8 +636,9 @@ discard block |
||
626 | 636 | $predicate = Utils::createLambda($predicate, 'v,k'); |
627 | 637 | |
628 | 638 | foreach ($this as $k => $v) { |
629 | - if (!$predicate($v, $k)) |
|
630 | - return false; |
|
639 | + if (!$predicate($v, $k)) { |
|
640 | + return false; |
|
641 | + } |
|
631 | 642 | } |
632 | 643 | return true; |
633 | 644 | } |
@@ -648,15 +659,16 @@ discard block |
||
648 | 659 | |
649 | 660 | if ($predicate) { |
650 | 661 | foreach ($this as $k => $v) { |
651 | - if ($predicate($v, $k)) |
|
652 | - return true; |
|
662 | + if ($predicate($v, $k)) { |
|
663 | + return true; |
|
664 | + } |
|
653 | 665 | } |
654 | 666 | return false; |
655 | - } |
|
656 | - else { |
|
667 | + } else { |
|
657 | 668 | $it = $this->getIterator(); |
658 | - if ($it instanceof \Countable) |
|
659 | - return count($it) > 0; |
|
669 | + if ($it instanceof \Countable) { |
|
670 | + return count($it) > 0; |
|
671 | + } |
|
660 | 672 | $it->rewind(); |
661 | 673 | return $it->valid(); |
662 | 674 | } |
@@ -710,8 +722,9 @@ discard block |
||
710 | 722 | public function contains($value): bool |
711 | 723 | { |
712 | 724 | foreach ($this as $v) { |
713 | - if ($v === $value) |
|
714 | - return true; |
|
725 | + if ($v === $value) { |
|
726 | + return true; |
|
727 | + } |
|
715 | 728 | } |
716 | 729 | return false; |
717 | 730 | } |
@@ -735,8 +748,9 @@ discard block |
||
735 | 748 | $set = []; |
736 | 749 | foreach ($this as $k => $v) { |
737 | 750 | $key = $keySelector($v, $k); |
738 | - if (isset($set[$key])) |
|
739 | - continue; |
|
751 | + if (isset($set[$key])) { |
|
752 | + continue; |
|
753 | + } |
|
740 | 754 | $set[$key] = true; |
741 | 755 | yield $k => $v; |
742 | 756 | } |
@@ -768,8 +782,9 @@ discard block |
||
768 | 782 | } |
769 | 783 | foreach ($this as $k => $v) { |
770 | 784 | $key = $keySelector($v, $k); |
771 | - if (isset($set[$key])) |
|
772 | - continue; |
|
785 | + if (isset($set[$key])) { |
|
786 | + continue; |
|
787 | + } |
|
773 | 788 | $set[$key] = true; |
774 | 789 | yield $k => $v; |
775 | 790 | } |
@@ -801,8 +816,9 @@ discard block |
||
801 | 816 | } |
802 | 817 | foreach ($this as $k => $v) { |
803 | 818 | $key = $keySelector($v, $k); |
804 | - if (!isset($set[$key])) |
|
805 | - continue; |
|
819 | + if (!isset($set[$key])) { |
|
820 | + continue; |
|
821 | + } |
|
806 | 822 | unset($set[$key]); |
807 | 823 | yield $k => $v; |
808 | 824 | } |
@@ -850,15 +866,17 @@ discard block |
||
850 | 866 | $set = []; |
851 | 867 | foreach ($this as $k => $v) { |
852 | 868 | $key = $keySelector($v, $k); |
853 | - if (isset($set[$key])) |
|
854 | - continue; |
|
869 | + if (isset($set[$key])) { |
|
870 | + continue; |
|
871 | + } |
|
855 | 872 | $set[$key] = true; |
856 | 873 | yield $k => $v; |
857 | 874 | } |
858 | 875 | foreach ($other as $k => $v) { |
859 | 876 | $key = $keySelector($v, $k); |
860 | - if (isset($set[$key])) |
|
861 | - continue; |
|
877 | + if (isset($set[$key])) { |
|
878 | + continue; |
|
879 | + } |
|
862 | 880 | $set[$key] = true; |
863 | 881 | yield $k => $v; |
864 | 882 | } |
@@ -882,12 +900,14 @@ discard block |
||
882 | 900 | { |
883 | 901 | /** @var $it \Iterator|\ArrayIterator */ |
884 | 902 | $it = $this->getIterator(); |
885 | - if ($it instanceof \ArrayIterator) |
|
886 | - return $it->getArrayCopy(); |
|
903 | + if ($it instanceof \ArrayIterator) { |
|
904 | + return $it->getArrayCopy(); |
|
905 | + } |
|
887 | 906 | |
888 | 907 | $array = []; |
889 | - foreach ($it as $k => $v) |
|
890 | - $array[$k] = $v; |
|
908 | + foreach ($it as $k => $v) { |
|
909 | + $array[$k] = $v; |
|
910 | + } |
|
891 | 911 | return $array; |
892 | 912 | } |
893 | 913 | |
@@ -914,8 +934,9 @@ discard block |
||
914 | 934 | protected function toArrayDeepProc($enum): array |
915 | 935 | { |
916 | 936 | $array = []; |
917 | - foreach ($enum as $k => $v) |
|
918 | - $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
|
937 | + foreach ($enum as $k => $v) { |
|
938 | + $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
|
939 | + } |
|
919 | 940 | return $array; |
920 | 941 | } |
921 | 942 | |
@@ -932,12 +953,14 @@ discard block |
||
932 | 953 | { |
933 | 954 | /** @var $it \Iterator|\ArrayIterator */ |
934 | 955 | $it = $this->getIterator(); |
935 | - if ($it instanceof \ArrayIterator) |
|
936 | - return array_values($it->getArrayCopy()); |
|
956 | + if ($it instanceof \ArrayIterator) { |
|
957 | + return array_values($it->getArrayCopy()); |
|
958 | + } |
|
937 | 959 | |
938 | 960 | $array = []; |
939 | - foreach ($it as $v) |
|
940 | - $array[] = $v; |
|
961 | + foreach ($it as $v) { |
|
962 | + $array[] = $v; |
|
963 | + } |
|
941 | 964 | return $array; |
942 | 965 | } |
943 | 966 | |
@@ -964,8 +987,9 @@ discard block |
||
964 | 987 | protected function toListDeepProc($enum): array |
965 | 988 | { |
966 | 989 | $array = []; |
967 | - foreach ($enum as $v) |
|
968 | - $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
|
990 | + foreach ($enum as $v) { |
|
991 | + $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
|
992 | + } |
|
969 | 993 | return $array; |
970 | 994 | } |
971 | 995 | |
@@ -984,8 +1008,9 @@ discard block |
||
984 | 1008 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
985 | 1009 | |
986 | 1010 | $dic = []; |
987 | - foreach ($this as $k => $v) |
|
988 | - $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
|
1011 | + foreach ($this as $k => $v) { |
|
1012 | + $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
|
1013 | + } |
|
989 | 1014 | return $dic; |
990 | 1015 | } |
991 | 1016 | |
@@ -1018,8 +1043,9 @@ discard block |
||
1018 | 1043 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
1019 | 1044 | |
1020 | 1045 | $lookup = []; |
1021 | - foreach ($this as $k => $v) |
|
1022 | - $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
|
1046 | + foreach ($this as $k => $v) { |
|
1047 | + $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
|
1048 | + } |
|
1023 | 1049 | return $lookup; |
1024 | 1050 | } |
1025 | 1051 | |
@@ -1061,8 +1087,9 @@ discard block |
||
1061 | 1087 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
1062 | 1088 | |
1063 | 1089 | $obj = new \stdClass(); |
1064 | - foreach ($this as $k => $v) |
|
1065 | - $obj->{$propertySelector($v, $k)} = $valueSelector($v, $k); |
|
1090 | + foreach ($this as $k => $v) { |
|
1091 | + $obj->{$propertySelector($v, $k)} = $valueSelector($v, $k); |
|
1092 | + } |
|
1066 | 1093 | return $obj; |
1067 | 1094 | } |
1068 | 1095 | |
@@ -1119,8 +1146,9 @@ discard block |
||
1119 | 1146 | { |
1120 | 1147 | $action = Utils::createLambda($action, 'v,k', Functions::$blank); |
1121 | 1148 | |
1122 | - foreach ($this as $k => $v) |
|
1123 | - $action($v, $k); |
|
1149 | + foreach ($this as $k => $v) { |
|
1150 | + $action($v, $k); |
|
1151 | + } |
|
1124 | 1152 | } |
1125 | 1153 | |
1126 | 1154 | /** |
@@ -140,7 +140,7 @@ |
||
140 | 140 | |
141 | 141 | /** |
142 | 142 | * Increment function: returns incremental integers starting from 0. |
143 | - * @return callable |
|
143 | + * @return \Closure |
|
144 | 144 | */ |
145 | 145 | public static function increment() |
146 | 146 | { |
@@ -94,39 +94,43 @@ |
||
94 | 94 | self::$blank = function() { }; |
95 | 95 | |
96 | 96 | self::$compareStrict = function($a, $b) { |
97 | - if ($a === $b) |
|
98 | - return 0; |
|
99 | - elseif ($a > $b) |
|
100 | - return 1; |
|
101 | - else |
|
102 | - return -1; |
|
97 | + if ($a === $b) { |
|
98 | + return 0; |
|
99 | + } elseif ($a > $b) { |
|
100 | + return 1; |
|
101 | + } else { |
|
102 | + return -1; |
|
103 | + } |
|
103 | 104 | }; |
104 | 105 | |
105 | 106 | self::$compareStrictReversed = function($a, $b) { |
106 | - if ($a === $b) |
|
107 | - return 0; |
|
108 | - elseif ($a > $b) |
|
109 | - return -1; |
|
110 | - else |
|
111 | - return 1; |
|
107 | + if ($a === $b) { |
|
108 | + return 0; |
|
109 | + } elseif ($a > $b) { |
|
110 | + return -1; |
|
111 | + } else { |
|
112 | + return 1; |
|
113 | + } |
|
112 | 114 | }; |
113 | 115 | |
114 | 116 | self::$compareLoose = function($a, $b) { |
115 | - if ($a == $b) |
|
116 | - return 0; |
|
117 | - elseif ($a > $b) |
|
118 | - return 1; |
|
119 | - else |
|
120 | - return -1; |
|
117 | + if ($a == $b) { |
|
118 | + return 0; |
|
119 | + } elseif ($a > $b) { |
|
120 | + return 1; |
|
121 | + } else { |
|
122 | + return -1; |
|
123 | + } |
|
121 | 124 | }; |
122 | 125 | |
123 | 126 | self::$compareLooseReversed = function($a, $b) { |
124 | - if ($a == $b) |
|
125 | - return 0; |
|
126 | - elseif ($a > $b) |
|
127 | - return -1; |
|
128 | - else |
|
129 | - return 1; |
|
127 | + if ($a == $b) { |
|
128 | + return 0; |
|
129 | + } elseif ($a > $b) { |
|
130 | + return -1; |
|
131 | + } else { |
|
132 | + return 1; |
|
133 | + } |
|
130 | 134 | }; |
131 | 135 | |
132 | 136 | self::$compareInt = function($a, $b) { |
@@ -40,8 +40,8 @@ discard block |
||
40 | 40 | public static function init() |
41 | 41 | { |
42 | 42 | self::$lambdaCache = [ |
43 | - '$v' => [ 'v,k' => Functions::$value ], |
|
44 | - '$k' => [ 'v,k' => Functions::$key ], |
|
43 | + '$v' => ['v,k' => Functions::$value], |
|
44 | + '$k' => ['v,k' => Functions::$key], |
|
45 | 45 | ]; |
46 | 46 | } |
47 | 47 | |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | $code = substr($closure, $posArrow + 3); |
147 | 147 | } |
148 | 148 | else { |
149 | - $args = '$' . str_replace(',', '=null,$', $closureArgs) . '=null'; |
|
149 | + $args = '$'.str_replace(',', '=null,$', $closureArgs).'=null'; |
|
150 | 150 | $code = $closure; |
151 | 151 | } |
152 | 152 | $code = trim($code, " \r\n\t"); |
@@ -56,16 +56,20 @@ discard block |
||
56 | 56 | public static function createLambda($closure, string $closureArgs, $default = null) |
57 | 57 | { |
58 | 58 | if ($closure === null) { |
59 | - if ($default === null) |
|
60 | - throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
59 | + if ($default === null) { |
|
60 | + throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
61 | + } |
|
61 | 62 | return $default; |
62 | 63 | } |
63 | - if ($closure instanceof \Closure) |
|
64 | - return $closure; |
|
65 | - if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) |
|
66 | - return $function; |
|
67 | - if (is_callable($closure)) |
|
68 | - return $closure; |
|
64 | + if ($closure instanceof \Closure) { |
|
65 | + return $closure; |
|
66 | + } |
|
67 | + if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) { |
|
68 | + return $function; |
|
69 | + } |
|
70 | + if (is_callable($closure)) { |
|
71 | + return $closure; |
|
72 | + } |
|
69 | 73 | throw new \InvalidArgumentException(self::ERROR_CLOSURE_NOT_CALLABLE); |
70 | 74 | } |
71 | 75 | |
@@ -83,8 +87,7 @@ discard block |
||
83 | 87 | if ($closure === null) { |
84 | 88 | $isReversed = false; |
85 | 89 | return $sortOrder === SORT_DESC ? Functions::$compareStrictReversed : Functions::$compareStrict; |
86 | - } |
|
87 | - elseif (is_int($closure)) { |
|
90 | + } elseif (is_int($closure)) { |
|
88 | 91 | switch ($closure) { |
89 | 92 | case SORT_REGULAR: |
90 | 93 | return Functions::$compareStrict; |
@@ -116,14 +119,16 @@ discard block |
||
116 | 119 | */ |
117 | 120 | public static function lambdaToSortFlagsAndOrder($closure, &$sortOrder) |
118 | 121 | { |
119 | - if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) |
|
120 | - $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
121 | - if (is_int($closure)) |
|
122 | - return $closure; |
|
123 | - elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) |
|
124 | - return self::$compareFunctionToSortFlags[$closure]; |
|
125 | - else |
|
126 | - return null; |
|
122 | + if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) { |
|
123 | + $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
124 | + } |
|
125 | + if (is_int($closure)) { |
|
126 | + return $closure; |
|
127 | + } elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) { |
|
128 | + return self::$compareFunctionToSortFlags[$closure]; |
|
129 | + } else { |
|
130 | + return null; |
|
131 | + } |
|
127 | 132 | } |
128 | 133 | |
129 | 134 | /** |
@@ -137,24 +142,26 @@ discard block |
||
137 | 142 | { |
138 | 143 | $posDollar = strpos($closure, '$'); |
139 | 144 | if ($posDollar !== false) { |
140 | - if (isset(self::$lambdaCache[$closure][$closureArgs])) |
|
141 | - return self::$lambdaCache[$closure][$closureArgs]; |
|
145 | + if (isset(self::$lambdaCache[$closure][$closureArgs])) { |
|
146 | + return self::$lambdaCache[$closure][$closureArgs]; |
|
147 | + } |
|
142 | 148 | $posArrow = strpos($closure, '==>', $posDollar); |
143 | 149 | if ($posArrow !== false) { |
144 | 150 | $args = trim(substr($closure, 0, $posArrow), "() \r\n\t"); |
145 | 151 | $code = substr($closure, $posArrow + 3); |
146 | - } |
|
147 | - else { |
|
152 | + } else { |
|
148 | 153 | $args = '$' . str_replace(',', '=null,$', $closureArgs) . '=null'; |
149 | 154 | $code = $closure; |
150 | 155 | } |
151 | 156 | $code = trim($code, " \r\n\t"); |
152 | - if (strlen($code) > 0 && $code[0] != '{') |
|
153 | - $code = "return {$code};"; |
|
157 | + if (strlen($code) > 0 && $code[0] != '{') { |
|
158 | + $code = "return {$code};"; |
|
159 | + } |
|
154 | 160 | $fun = @create_function($args, $code); |
155 | 161 | // @codeCoverageIgnoreStart |
156 | - if (!$fun) |
|
157 | - throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
162 | + if (!$fun) { |
|
163 | + throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
164 | + } |
|
158 | 165 | // @codeCoverageIgnoreEnd |
159 | 166 | self::$lambdaCache[$closure][$closureArgs] = $fun; |
160 | 167 | return $fun; |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | { |
207 | 207 | /** @var $order OrderedEnumerable */ |
208 | 208 | foreach ($this->source as $k => $v) |
209 | - $enum[] = [ $k, $v ]; |
|
209 | + $enum[] = [$k, $v]; |
|
210 | 210 | |
211 | 211 | $args = []; |
212 | 212 | foreach ($orders as $order) { |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | { |
229 | 229 | /** @var $order OrderedEnumerable */ |
230 | 230 | foreach ($this->source as $k => $v) { |
231 | - $element = [ $k, $v ]; |
|
231 | + $element = [$k, $v]; |
|
232 | 232 | foreach ($orders as $order) { |
233 | 233 | $keySelector = $order->keySelector; |
234 | 234 | $element[] = $keySelector($v, $k); |
@@ -65,7 +65,7 @@ |
||
65 | 65 | * <p>Three methods are defined to extend the type OrderedEnumerable, which is the return type of this method. These three methods, namely {@link thenBy}, {@link thenByDescending} and {@link thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made. |
66 | 66 | * <p>Because OrderedEnumerable inherits from {@link Enumerable}, you can call {@link Enumerable::orderBy orderBy}, {@link Enumerable::orderByDescending orderByDescending} or {@link Enumerable::orderByDir orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering. |
67 | 67 | * <p>This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used. |
68 | - * @param int|bool $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value). |
|
68 | + * @param boolean $sortOrder A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value). |
|
69 | 69 | * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value. |
70 | 70 | * @param callable|int|null $comparer {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b. Can also be a combination of SORT_ flags. |
71 | 71 | * @return \YaLinqo\OrderedEnumerable |
@@ -54,8 +54,10 @@ discard block |
||
54 | 54 | private function getSingleComparer() |
55 | 55 | { |
56 | 56 | $comparer = $this->comparer; |
57 | - if ($this->isReversed) |
|
58 | - $comparer = function($a, $b) use ($comparer) { return -$comparer($a, $b); }; |
|
57 | + if ($this->isReversed) { |
|
58 | + $comparer = function($a, $b) use ($comparer) { return -$comparer($a, $b); |
|
59 | + } |
|
60 | + }; |
|
59 | 61 | return $comparer; |
60 | 62 | } |
61 | 63 | |
@@ -116,8 +118,9 @@ discard block |
||
116 | 118 | $array = $this->source->tryGetArrayCopy(); |
117 | 119 | |
118 | 120 | $it = $this->trySortBySingleField($array, $canMultisort); |
119 | - if ($it !== null) |
|
120 | - return $it; |
|
121 | + if ($it !== null) { |
|
122 | + return $it; |
|
123 | + } |
|
121 | 124 | |
122 | 125 | return $this->sortByMultipleFields($array, $canMultisort); |
123 | 126 | } |
@@ -126,24 +129,23 @@ discard block |
||
126 | 129 | { |
127 | 130 | if ($this->parent !== null || $array === null) { |
128 | 131 | return null; |
129 | - } |
|
130 | - elseif ($this->keySelector === Functions::$value) { |
|
131 | - if (!$canMultisort) |
|
132 | - uasort($array, $this->getSingleComparer()); |
|
133 | - elseif ($this->sortOrder == SORT_ASC) |
|
134 | - asort($array, $this->sortFlags); |
|
135 | - else |
|
136 | - arsort($array, $this->sortFlags); |
|
137 | - } |
|
138 | - elseif ($this->keySelector === Functions::$key) { |
|
139 | - if ($canMultisort) |
|
140 | - uksort($array, $this->getSingleComparer()); |
|
141 | - elseif ($this->sortOrder == SORT_ASC) |
|
142 | - ksort($array, $this->sortFlags); |
|
143 | - else |
|
144 | - krsort($array, $this->sortFlags); |
|
145 | - } |
|
146 | - else { |
|
132 | + } elseif ($this->keySelector === Functions::$value) { |
|
133 | + if (!$canMultisort) { |
|
134 | + uasort($array, $this->getSingleComparer()); |
|
135 | + } elseif ($this->sortOrder == SORT_ASC) { |
|
136 | + asort($array, $this->sortFlags); |
|
137 | + } else { |
|
138 | + arsort($array, $this->sortFlags); |
|
139 | + } |
|
140 | + } elseif ($this->keySelector === Functions::$key) { |
|
141 | + if ($canMultisort) { |
|
142 | + uksort($array, $this->getSingleComparer()); |
|
143 | + } elseif ($this->sortOrder == SORT_ASC) { |
|
144 | + ksort($array, $this->sortFlags); |
|
145 | + } else { |
|
146 | + krsort($array, $this->sortFlags); |
|
147 | + } |
|
148 | + } else { |
|
147 | 149 | return null; |
148 | 150 | } |
149 | 151 | return new \ArrayIterator($array); |
@@ -154,14 +156,16 @@ discard block |
||
154 | 156 | $orders = []; |
155 | 157 | for ($order = $this; $order !== null; $order = $order->parent) { |
156 | 158 | $orders[] = $order; |
157 | - if ($order->sortFlags === null) |
|
158 | - $canMultisort = false; |
|
159 | + if ($order->sortFlags === null) { |
|
160 | + $canMultisort = false; |
|
161 | + } |
|
159 | 162 | } |
160 | 163 | $orders = array_reverse($orders); |
161 | 164 | |
162 | 165 | $it = $this->trySortArrayWithMultisort($array, $orders, $canMultisort); |
163 | - if ($it !== null) |
|
164 | - return $it; |
|
166 | + if ($it !== null) { |
|
167 | + return $it; |
|
168 | + } |
|
165 | 169 | |
166 | 170 | return $this->sortIterator($orders, $canMultisort); |
167 | 171 | } |
@@ -169,20 +173,23 @@ discard block |
||
169 | 173 | private function sortIterator(array $orders, bool $canMultisort) |
170 | 174 | { |
171 | 175 | $enum = []; |
172 | - if ($canMultisort) |
|
173 | - $this->sortIteratorWithMultisort($enum, $orders); |
|
174 | - else |
|
175 | - $this->sortIteratorWithUsort($enum, $orders); |
|
176 | + if ($canMultisort) { |
|
177 | + $this->sortIteratorWithMultisort($enum, $orders); |
|
178 | + } else { |
|
179 | + $this->sortIteratorWithUsort($enum, $orders); |
|
180 | + } |
|
176 | 181 | |
177 | - foreach ($enum as $pair) |
|
178 | - yield $pair[0] => $pair[1]; |
|
182 | + foreach ($enum as $pair) { |
|
183 | + yield $pair[0] => $pair[1]; |
|
184 | + } |
|
179 | 185 | } |
180 | 186 | |
181 | 187 | private function trySortArrayWithMultisort($array, array $orders, bool $canMultisort) |
182 | 188 | { |
183 | 189 | /** @var $order OrderedEnumerable */ |
184 | - if ($array === null || !$canMultisort) |
|
185 | - return null; |
|
190 | + if ($array === null || !$canMultisort) { |
|
191 | + return null; |
|
192 | + } |
|
186 | 193 | |
187 | 194 | $args = []; |
188 | 195 | foreach ($orders as $order) { |
@@ -205,8 +212,9 @@ discard block |
||
205 | 212 | private function sortIteratorWithMultisort(&$enum, array $orders) |
206 | 213 | { |
207 | 214 | /** @var $order OrderedEnumerable */ |
208 | - foreach ($this->source as $k => $v) |
|
209 | - $enum[] = [ $k, $v ]; |
|
215 | + foreach ($this->source as $k => $v) { |
|
216 | + $enum[] = [ $k, $v ]; |
|
217 | + } |
|
210 | 218 | |
211 | 219 | $args = []; |
212 | 220 | foreach ($orders as $order) { |
@@ -243,8 +251,9 @@ discard block |
||
243 | 251 | $order = $orders[$i]; |
244 | 252 | $comparer = $order->comparer; |
245 | 253 | $diff = $comparer($a[$i + 2], $b[$i + 2]); |
246 | - if ($diff != 0) |
|
247 | - return $order->isReversed ? -$diff : $diff; |
|
254 | + if ($diff != 0) { |
|
255 | + return $order->isReversed ? -$diff : $diff; |
|
256 | + } |
|
248 | 257 | } |
249 | 258 | return 0; |
250 | 259 | }); |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | * <p>If source contains fewer than count elements, an empty sequence is returned. If count is less than or equal to zero, all elements of source are yielded. |
395 | 395 | * <p>The {@link take} and skip methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll->take(n) and coll->skip(n) yields the same sequence as coll. |
396 | 396 | * @param int $count The number of elements to skip before returning the remaining elements. |
397 | - * @return Enumerable A sequence that contains the elements that occur after the specified index in the input sequence. |
|
397 | + * @return EnumerablePagination A sequence that contains the elements that occur after the specified index in the input sequence. |
|
398 | 398 | * @package YaLinqo\Pagination |
399 | 399 | */ |
400 | 400 | public function skip(int $count) |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | * <p>This method tests each element of source by using predicate and skips the element if the result is true. After the predicate function returns false for an element, that element and the remaining elements in source are yielded and there are no more invocations of predicate. If predicate returns true for all elements in the sequence, an empty sequence is returned. |
418 | 418 | * <p>The {@link takeWhile} and skipWhile methods are functional complements. Given a sequence coll and a pure function p, concatenating the results of coll->takeWhile(p) and coll->skipWhile(p) yields the same sequence as coll. |
419 | 419 | * @param callable $predicate {(v, k) ==> result} A function to test each element for a condition. |
420 | - * @return Enumerable A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate. |
|
420 | + * @return EnumerablePagination A sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate. |
|
421 | 421 | * @package YaLinqo\Pagination |
422 | 422 | */ |
423 | 423 | public function skipWhile($predicate) |
@@ -441,7 +441,7 @@ discard block |
||
441 | 441 | * <p>Take enumerates source and yields elements until count elements have been yielded or source contains no more elements. If count is less than or equal to zero, source is not enumerated and an empty sequence is returned. |
442 | 442 | * <p>The take and {@link skip} methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll->take(n) and coll->skip(n) yields the same sequence as coll. |
443 | 443 | * @param int $count The number of elements to return. |
444 | - * @return Enumerable A sequence that contains the specified number of elements from the start of the input sequence. |
|
444 | + * @return EnumerablePagination A sequence that contains the specified number of elements from the start of the input sequence. |
|
445 | 445 | * @package YaLinqo\Pagination |
446 | 446 | */ |
447 | 447 | public function take(int $count) |
@@ -464,7 +464,7 @@ discard block |
||
464 | 464 | * <p>The takeWhile method tests each element of source by using predicate and yields the element if the result is true. Enumeration stops when the predicate function returns false for an element or when source contains no more elements. |
465 | 465 | * <p>The takeWhile and {@link skipWhile} methods are functional complements. Given a sequence coll and a pure function p, concatenating the results of coll->takeWhile(p) and coll->skipWhile(p) yields the same sequence as coll. |
466 | 466 | * @param callable $predicate {(v, k) ==> result} A function to test each element for a condition. |
467 | - * @return Enumerable A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes. |
|
467 | + * @return EnumerablePagination A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes. |
|
468 | 468 | * @package YaLinqo\Pagination |
469 | 469 | */ |
470 | 470 | public function takeWhile($predicate) |
@@ -32,14 +32,16 @@ discard block |
||
32 | 32 | $it = $this->getIterator(); |
33 | 33 | |
34 | 34 | if ($it instanceof \ArrayAccess) { |
35 | - if (!$it->offsetExists($key)) |
|
36 | - throw new \UnexpectedValueException(Errors::NO_KEY); |
|
35 | + if (!$it->offsetExists($key)) { |
|
36 | + throw new \UnexpectedValueException(Errors::NO_KEY); |
|
37 | + } |
|
37 | 38 | return $it->offsetGet($key); |
38 | 39 | } |
39 | 40 | |
40 | 41 | foreach ($it as $k => $v) { |
41 | - if ($k === $key) |
|
42 | - return $v; |
|
42 | + if ($k === $key) { |
|
43 | + return $v; |
|
44 | + } |
|
43 | 45 | } |
44 | 46 | throw new \UnexpectedValueException(Errors::NO_KEY); |
45 | 47 | } |
@@ -58,12 +60,14 @@ discard block |
||
58 | 60 | /** @var $it \Iterator|\ArrayAccess */ |
59 | 61 | $it = $this->getIterator(); |
60 | 62 | |
61 | - if ($it instanceof \ArrayAccess) |
|
62 | - return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
|
63 | + if ($it instanceof \ArrayAccess) { |
|
64 | + return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
|
65 | + } |
|
63 | 66 | |
64 | 67 | foreach ($it as $k => $v) { |
65 | - if ($k === $key) |
|
66 | - return $v; |
|
68 | + if ($k === $key) { |
|
69 | + return $v; |
|
70 | + } |
|
67 | 71 | } |
68 | 72 | return $default; |
69 | 73 | } |
@@ -86,8 +90,9 @@ discard block |
||
86 | 90 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
87 | 91 | |
88 | 92 | foreach ($this as $k => $v) { |
89 | - if ($predicate($v, $k)) |
|
90 | - return $v; |
|
93 | + if ($predicate($v, $k)) { |
|
94 | + return $v; |
|
95 | + } |
|
91 | 96 | } |
92 | 97 | throw new \UnexpectedValueException(Errors::NO_MATCHES); |
93 | 98 | } |
@@ -109,8 +114,9 @@ discard block |
||
109 | 114 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
110 | 115 | |
111 | 116 | foreach ($this as $k => $v) { |
112 | - if ($predicate($v, $k)) |
|
113 | - return $v; |
|
117 | + if ($predicate($v, $k)) { |
|
118 | + return $v; |
|
119 | + } |
|
114 | 120 | } |
115 | 121 | return $default; |
116 | 122 | } |
@@ -132,8 +138,9 @@ discard block |
||
132 | 138 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
133 | 139 | |
134 | 140 | foreach ($this as $k => $v) { |
135 | - if ($predicate($v, $k)) |
|
136 | - return $v; |
|
141 | + if ($predicate($v, $k)) { |
|
142 | + return $v; |
|
143 | + } |
|
137 | 144 | } |
138 | 145 | return $fallback(); |
139 | 146 | } |
@@ -163,8 +170,9 @@ discard block |
||
163 | 170 | $value = $v; |
164 | 171 | } |
165 | 172 | } |
166 | - if (!$found) |
|
167 | - throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
173 | + if (!$found) { |
|
174 | + throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
175 | + } |
|
168 | 176 | return $value; |
169 | 177 | } |
170 | 178 | |
@@ -243,14 +251,16 @@ discard block |
||
243 | 251 | $value = null; |
244 | 252 | foreach ($this as $k => $v) { |
245 | 253 | if ($predicate($v, $k)) { |
246 | - if ($found) |
|
247 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
254 | + if ($found) { |
|
255 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
256 | + } |
|
248 | 257 | $found = true; |
249 | 258 | $value = $v; |
250 | 259 | } |
251 | 260 | } |
252 | - if (!$found) |
|
253 | - throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
261 | + if (!$found) { |
|
262 | + throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
263 | + } |
|
254 | 264 | return $value; |
255 | 265 | } |
256 | 266 | |
@@ -275,8 +285,9 @@ discard block |
||
275 | 285 | $value = null; |
276 | 286 | foreach ($this as $k => $v) { |
277 | 287 | if ($predicate($v, $k)) { |
278 | - if ($found) |
|
279 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
288 | + if ($found) { |
|
289 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
290 | + } |
|
280 | 291 | $found = true; |
281 | 292 | $value = $v; |
282 | 293 | } |
@@ -305,8 +316,9 @@ discard block |
||
305 | 316 | $value = null; |
306 | 317 | foreach ($this as $k => $v) { |
307 | 318 | if ($predicate($v, $k)) { |
308 | - if ($found) |
|
309 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
319 | + if ($found) { |
|
320 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
321 | + } |
|
310 | 322 | $found = true; |
311 | 323 | $value = $v; |
312 | 324 | } |
@@ -325,12 +337,13 @@ discard block |
||
325 | 337 | public function indexOf($value) |
326 | 338 | { |
327 | 339 | $array = $this->tryGetArrayCopy(); |
328 | - if ($array !== null) |
|
329 | - return array_search($value, $array, true); |
|
330 | - else { |
|
340 | + if ($array !== null) { |
|
341 | + return array_search($value, $array, true); |
|
342 | + } else { |
|
331 | 343 | foreach ($this as $k => $v) { |
332 | - if ($v === $value) |
|
333 | - return $k; |
|
344 | + if ($v === $value) { |
|
345 | + return $k; |
|
346 | + } |
|
334 | 347 | } |
335 | 348 | return false; |
336 | 349 | } |
@@ -348,8 +361,9 @@ discard block |
||
348 | 361 | { |
349 | 362 | $key = null; |
350 | 363 | foreach ($this as $k => $v) { |
351 | - if ($v === $value) |
|
352 | - $key = $k; |
|
364 | + if ($v === $value) { |
|
365 | + $key = $k; |
|
366 | + } |
|
353 | 367 | } |
354 | 368 | return $key; // not -1 |
355 | 369 | } |
@@ -367,8 +381,9 @@ discard block |
||
367 | 381 | $predicate = Utils::createLambda($predicate, 'v,k'); |
368 | 382 | |
369 | 383 | foreach ($this as $k => $v) { |
370 | - if ($predicate($v, $k)) |
|
371 | - return $k; |
|
384 | + if ($predicate($v, $k)) { |
|
385 | + return $k; |
|
386 | + } |
|
372 | 387 | } |
373 | 388 | return null; // not -1 |
374 | 389 | } |
@@ -387,8 +402,9 @@ discard block |
||
387 | 402 | |
388 | 403 | $key = null; |
389 | 404 | foreach ($this as $k => $v) { |
390 | - if ($predicate($v, $k)) |
|
391 | - $key = $k; |
|
405 | + if ($predicate($v, $k)) { |
|
406 | + $key = $k; |
|
407 | + } |
|
392 | 408 | } |
393 | 409 | return $key; // not -1 |
394 | 410 | } |
@@ -407,8 +423,9 @@ discard block |
||
407 | 423 | return new self(function() use ($count) { |
408 | 424 | $it = $this->getIterator(); |
409 | 425 | $it->rewind(); |
410 | - for ($i = 0; $i < $count && $it->valid(); ++$i) |
|
411 | - $it->next(); |
|
426 | + for ($i = 0; $i < $count && $it->valid(); ++$i) { |
|
427 | + $it->next(); |
|
428 | + } |
|
412 | 429 | while ($it->valid()) { |
413 | 430 | yield $it->key() => $it->current(); |
414 | 431 | $it->next(); |
@@ -432,10 +449,12 @@ discard block |
||
432 | 449 | return new self(function() use ($predicate) { |
433 | 450 | $yielding = false; |
434 | 451 | foreach ($this as $k => $v) { |
435 | - if (!$yielding && !$predicate($v, $k)) |
|
436 | - $yielding = true; |
|
437 | - if ($yielding) |
|
438 | - yield $k => $v; |
|
452 | + if (!$yielding && !$predicate($v, $k)) { |
|
453 | + $yielding = true; |
|
454 | + } |
|
455 | + if ($yielding) { |
|
456 | + yield $k => $v; |
|
457 | + } |
|
439 | 458 | } |
440 | 459 | }); |
441 | 460 | } |
@@ -451,14 +470,16 @@ discard block |
||
451 | 470 | */ |
452 | 471 | public function take(int $count) |
453 | 472 | { |
454 | - if ($count <= 0) |
|
455 | - return new self(new \EmptyIterator, false); |
|
473 | + if ($count <= 0) { |
|
474 | + return new self(new \EmptyIterator, false); |
|
475 | + } |
|
456 | 476 | |
457 | 477 | return new self(function() use ($count) { |
458 | 478 | foreach ($this as $k => $v) { |
459 | 479 | yield $k => $v; |
460 | - if (--$count == 0) |
|
461 | - break; |
|
480 | + if (--$count == 0) { |
|
481 | + break; |
|
482 | + } |
|
462 | 483 | } |
463 | 484 | }); |
464 | 485 | } |
@@ -478,8 +499,9 @@ discard block |
||
478 | 499 | |
479 | 500 | return new self(function() use ($predicate) { |
480 | 501 | foreach ($this as $k => $v) { |
481 | - if (!$predicate($v, $k)) |
|
482 | - break; |
|
502 | + if (!$predicate($v, $k)) { |
|
503 | + break; |
|
504 | + } |
|
483 | 505 | yield $k => $v; |
484 | 506 | } |
485 | 507 | }); |
@@ -36,8 +36,9 @@ discard block |
||
36 | 36 | yield $v; |
37 | 37 | $isEmpty = false; |
38 | 38 | } |
39 | - if ($isEmpty) |
|
40 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
39 | + if ($isEmpty) { |
|
40 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
41 | + } |
|
41 | 42 | } |
42 | 43 | }); |
43 | 44 | } |
@@ -72,14 +73,15 @@ discard block |
||
72 | 73 | public static function from($source): Enumerable |
73 | 74 | { |
74 | 75 | $it = null; |
75 | - if ($source instanceof Enumerable) |
|
76 | - return $source; |
|
77 | - elseif (is_array($source)) |
|
78 | - $it = new \ArrayIterator($source); |
|
79 | - elseif ($source instanceof \IteratorAggregate) |
|
80 | - $it = $source->getIterator(); |
|
81 | - elseif ($source instanceof \Traversable) |
|
82 | - $it = $source; |
|
76 | + if ($source instanceof Enumerable) { |
|
77 | + return $source; |
|
78 | + } elseif (is_array($source)) { |
|
79 | + $it = new \ArrayIterator($source); |
|
80 | + } elseif ($source instanceof \IteratorAggregate) { |
|
81 | + $it = $source->getIterator(); |
|
82 | + } elseif ($source instanceof \Traversable) { |
|
83 | + $it = $source; |
|
84 | + } |
|
83 | 85 | if ($it !== null) { |
84 | 86 | return new self($it, false); |
85 | 87 | } |
@@ -128,8 +130,9 @@ discard block |
||
128 | 130 | { |
129 | 131 | return new self(function() use ($start, $step) { |
130 | 132 | $value = $start - $step; |
131 | - while (true) |
|
132 | - yield $value += $step; |
|
133 | + while (true) { |
|
134 | + yield $value += $step; |
|
135 | + } |
|
133 | 136 | }); |
134 | 137 | } |
135 | 138 | |
@@ -189,12 +192,14 @@ discard block |
||
189 | 192 | */ |
190 | 193 | public static function range(int $start, int $count, int $step = 1): Enumerable |
191 | 194 | { |
192 | - if ($count <= 0) |
|
193 | - return self::emptyEnum(); |
|
195 | + if ($count <= 0) { |
|
196 | + return self::emptyEnum(); |
|
197 | + } |
|
194 | 198 | return new self(function() use ($start, $count, $step) { |
195 | 199 | $value = $start - $step; |
196 | - while ($count-- > 0) |
|
197 | - yield $value += $step; |
|
200 | + while ($count-- > 0) { |
|
201 | + yield $value += $step; |
|
202 | + } |
|
198 | 203 | }); |
199 | 204 | } |
200 | 205 | |
@@ -228,16 +233,18 @@ discard block |
||
228 | 233 | */ |
229 | 234 | public static function rangeTo(int $start, int $end, $step = 1): Enumerable |
230 | 235 | { |
231 | - if ($step <= 0) |
|
232 | - throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
|
236 | + if ($step <= 0) { |
|
237 | + throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
|
238 | + } |
|
233 | 239 | return new self(function() use ($start, $end, $step) { |
234 | 240 | if ($start <= $end) { |
235 | - for ($i = $start; $i < $end; $i += $step) |
|
236 | - yield $i; |
|
237 | - } |
|
238 | - else { |
|
239 | - for ($i = $start; $i > $end; $i -= $step) |
|
240 | - yield $i; |
|
241 | + for ($i = $start; $i < $end; $i += $step) { |
|
242 | + yield $i; |
|
243 | + } |
|
244 | + } else { |
|
245 | + for ($i = $start; $i > $end; $i -= $step) { |
|
246 | + yield $i; |
|
247 | + } |
|
241 | 248 | } |
242 | 249 | }); |
243 | 250 | } |
@@ -257,11 +264,13 @@ discard block |
||
257 | 264 | */ |
258 | 265 | public static function repeat($element, $count = null): Enumerable |
259 | 266 | { |
260 | - if ($count < 0) |
|
261 | - throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
|
267 | + if ($count < 0) { |
|
268 | + throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
|
269 | + } |
|
262 | 270 | return new self(function() use ($element, $count) { |
263 | - for ($i = 0; $i < $count || $count === null; $i++) |
|
264 | - yield $element; |
|
271 | + for ($i = 0; $i < $count || $count === null; $i++) { |
|
272 | + yield $element; |
|
273 | + } |
|
265 | 274 | }); |
266 | 275 | } |
267 | 276 |