@@ -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 | } |
@@ -677,12 +689,14 @@ discard block |
||
677 | 689 | { |
678 | 690 | return new self(function() use ($value, $key) { |
679 | 691 | // TODO Switch to 'yield from' when support for PHP<7.0 is dropped. |
680 | - foreach ($this as $k => $v) |
|
681 | - yield $k => $v; |
|
682 | - if ($key !== Utils::UNDEFINED) |
|
683 | - yield $key => $value; |
|
684 | - else |
|
685 | - yield $value; |
|
692 | + foreach ($this as $k => $v) { |
|
693 | + yield $k => $v; |
|
694 | + } |
|
695 | + if ($key !== Utils::UNDEFINED) { |
|
696 | + yield $key => $value; |
|
697 | + } else { |
|
698 | + yield $value; |
|
699 | + } |
|
686 | 700 | }); |
687 | 701 | } |
688 | 702 | |
@@ -700,10 +714,12 @@ discard block |
||
700 | 714 | |
701 | 715 | return new self(function() use ($other) { |
702 | 716 | // TODO Switch to 'yield from' when support for PHP<7.0 is dropped. |
703 | - foreach ($this as $k => $v) |
|
704 | - yield $k => $v; |
|
705 | - foreach ($other as $k => $v) |
|
706 | - yield $k => $v; |
|
717 | + foreach ($this as $k => $v) { |
|
718 | + yield $k => $v; |
|
719 | + } |
|
720 | + foreach ($other as $k => $v) { |
|
721 | + yield $k => $v; |
|
722 | + } |
|
707 | 723 | }); |
708 | 724 | } |
709 | 725 | |
@@ -718,8 +734,9 @@ discard block |
||
718 | 734 | public function contains($value) |
719 | 735 | { |
720 | 736 | foreach ($this as $v) { |
721 | - if ($v === $value) |
|
722 | - return true; |
|
737 | + if ($v === $value) { |
|
738 | + return true; |
|
739 | + } |
|
723 | 740 | } |
724 | 741 | return false; |
725 | 742 | } |
@@ -743,8 +760,9 @@ discard block |
||
743 | 760 | $set = []; |
744 | 761 | foreach ($this as $k => $v) { |
745 | 762 | $key = $keySelector($v, $k); |
746 | - if (isset($set[$key])) |
|
747 | - continue; |
|
763 | + if (isset($set[$key])) { |
|
764 | + continue; |
|
765 | + } |
|
748 | 766 | $set[$key] = true; |
749 | 767 | yield $k => $v; |
750 | 768 | } |
@@ -776,8 +794,9 @@ discard block |
||
776 | 794 | } |
777 | 795 | foreach ($this as $k => $v) { |
778 | 796 | $key = $keySelector($v, $k); |
779 | - if (isset($set[$key])) |
|
780 | - continue; |
|
797 | + if (isset($set[$key])) { |
|
798 | + continue; |
|
799 | + } |
|
781 | 800 | $set[$key] = true; |
782 | 801 | yield $k => $v; |
783 | 802 | } |
@@ -809,8 +828,9 @@ discard block |
||
809 | 828 | } |
810 | 829 | foreach ($this as $k => $v) { |
811 | 830 | $key = $keySelector($v, $k); |
812 | - if (!isset($set[$key])) |
|
813 | - continue; |
|
831 | + if (!isset($set[$key])) { |
|
832 | + continue; |
|
833 | + } |
|
814 | 834 | unset($set[$key]); |
815 | 835 | yield $k => $v; |
816 | 836 | } |
@@ -831,13 +851,15 @@ discard block |
||
831 | 851 | public function prepend($value, $key = Utils::UNDEFINED) |
832 | 852 | { |
833 | 853 | return new self(function() use ($value, $key) { |
834 | - if ($key !== Utils::UNDEFINED) |
|
835 | - yield $key => $value; |
|
836 | - else |
|
837 | - yield $value; |
|
854 | + if ($key !== Utils::UNDEFINED) { |
|
855 | + yield $key => $value; |
|
856 | + } else { |
|
857 | + yield $value; |
|
858 | + } |
|
838 | 859 | // TODO Switch to 'yield from' when support for PHP<7.0 is dropped. |
839 | - foreach ($this as $k => $v) |
|
840 | - yield $k => $v; |
|
860 | + foreach ($this as $k => $v) { |
|
861 | + yield $k => $v; |
|
862 | + } |
|
841 | 863 | }); |
842 | 864 | } |
843 | 865 | |
@@ -863,15 +885,17 @@ discard block |
||
863 | 885 | $set = []; |
864 | 886 | foreach ($this as $k => $v) { |
865 | 887 | $key = $keySelector($v, $k); |
866 | - if (isset($set[$key])) |
|
867 | - continue; |
|
888 | + if (isset($set[$key])) { |
|
889 | + continue; |
|
890 | + } |
|
868 | 891 | $set[$key] = true; |
869 | 892 | yield $k => $v; |
870 | 893 | } |
871 | 894 | foreach ($other as $k => $v) { |
872 | 895 | $key = $keySelector($v, $k); |
873 | - if (isset($set[$key])) |
|
874 | - continue; |
|
896 | + if (isset($set[$key])) { |
|
897 | + continue; |
|
898 | + } |
|
875 | 899 | $set[$key] = true; |
876 | 900 | yield $k => $v; |
877 | 901 | } |
@@ -895,12 +919,14 @@ discard block |
||
895 | 919 | { |
896 | 920 | /** @var $it \Iterator|\ArrayIterator */ |
897 | 921 | $it = $this->getIterator(); |
898 | - if ($it instanceof \ArrayIterator) |
|
899 | - return $it->getArrayCopy(); |
|
922 | + if ($it instanceof \ArrayIterator) { |
|
923 | + return $it->getArrayCopy(); |
|
924 | + } |
|
900 | 925 | |
901 | 926 | $array = []; |
902 | - foreach ($it as $k => $v) |
|
903 | - $array[$k] = $v; |
|
927 | + foreach ($it as $k => $v) { |
|
928 | + $array[$k] = $v; |
|
929 | + } |
|
904 | 930 | return $array; |
905 | 931 | } |
906 | 932 | |
@@ -927,8 +953,9 @@ discard block |
||
927 | 953 | protected function toArrayDeepProc($enum) |
928 | 954 | { |
929 | 955 | $array = []; |
930 | - foreach ($enum as $k => $v) |
|
931 | - $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
|
956 | + foreach ($enum as $k => $v) { |
|
957 | + $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
|
958 | + } |
|
932 | 959 | return $array; |
933 | 960 | } |
934 | 961 | |
@@ -945,12 +972,14 @@ discard block |
||
945 | 972 | { |
946 | 973 | /** @var $it \Iterator|\ArrayIterator */ |
947 | 974 | $it = $this->getIterator(); |
948 | - if ($it instanceof \ArrayIterator) |
|
949 | - return array_values($it->getArrayCopy()); |
|
975 | + if ($it instanceof \ArrayIterator) { |
|
976 | + return array_values($it->getArrayCopy()); |
|
977 | + } |
|
950 | 978 | |
951 | 979 | $array = []; |
952 | - foreach ($it as $v) |
|
953 | - $array[] = $v; |
|
980 | + foreach ($it as $v) { |
|
981 | + $array[] = $v; |
|
982 | + } |
|
954 | 983 | return $array; |
955 | 984 | } |
956 | 985 | |
@@ -977,8 +1006,9 @@ discard block |
||
977 | 1006 | protected function toListDeepProc($enum) |
978 | 1007 | { |
979 | 1008 | $array = []; |
980 | - foreach ($enum as $v) |
|
981 | - $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
|
1009 | + foreach ($enum as $v) { |
|
1010 | + $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
|
1011 | + } |
|
982 | 1012 | return $array; |
983 | 1013 | } |
984 | 1014 | |
@@ -997,8 +1027,9 @@ discard block |
||
997 | 1027 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
998 | 1028 | |
999 | 1029 | $dic = []; |
1000 | - foreach ($this as $k => $v) |
|
1001 | - $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
|
1030 | + foreach ($this as $k => $v) { |
|
1031 | + $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
|
1032 | + } |
|
1002 | 1033 | return $dic; |
1003 | 1034 | } |
1004 | 1035 | |
@@ -1031,8 +1062,9 @@ discard block |
||
1031 | 1062 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
1032 | 1063 | |
1033 | 1064 | $lookup = []; |
1034 | - foreach ($this as $k => $v) |
|
1035 | - $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
|
1065 | + foreach ($this as $k => $v) { |
|
1066 | + $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
|
1067 | + } |
|
1036 | 1068 | return $lookup; |
1037 | 1069 | } |
1038 | 1070 | |
@@ -1074,8 +1106,9 @@ discard block |
||
1074 | 1106 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
1075 | 1107 | |
1076 | 1108 | $obj = new \stdClass(); |
1077 | - foreach ($this as $k => $v) |
|
1078 | - $obj->{$propertySelector($v, $k)} = $valueSelector($v, $k); |
|
1109 | + foreach ($this as $k => $v) { |
|
1110 | + $obj->{$propertySelector($v, $k)} = $valueSelector($v, $k); |
|
1111 | + } |
|
1079 | 1112 | return $obj; |
1080 | 1113 | } |
1081 | 1114 | |
@@ -1132,8 +1165,9 @@ discard block |
||
1132 | 1165 | { |
1133 | 1166 | $action = Utils::createLambda($action, 'v,k', Functions::$blank); |
1134 | 1167 | |
1135 | - foreach ($this as $k => $v) |
|
1136 | - $action($v, $k); |
|
1168 | + foreach ($this as $k => $v) { |
|
1169 | + $action($v, $k); |
|
1170 | + } |
|
1137 | 1171 | } |
1138 | 1172 | |
1139 | 1173 | /** |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * @param array|\Iterator|\IteratorAggregate|Enumerable $source Source sequence. |
23 | 23 | * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable. |
24 | 24 | * @throws \UnexpectedValueException If source contains no elements (checked during enumeration). |
25 | - * @return Enumerable Endless list of items repeating the source sequence. |
|
25 | + * @return EnumerableGeneration Endless list of items repeating the source sequence. |
|
26 | 26 | * @package YaLinqo\Generation |
27 | 27 | */ |
28 | 28 | public static function cycle($source) |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | /** |
46 | 46 | * Returns an empty sequence. |
47 | 47 | * <p><b>Syntax</b>: emptyEnum () |
48 | - * @return Enumerable |
|
48 | + * @return EnumerableGeneration |
|
49 | 49 | * @package YaLinqo\Generation |
50 | 50 | */ |
51 | 51 | public static function emptyEnum() |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | * @param mixed $seedValue Initial state of the generator loop for values. Default: null. |
95 | 95 | * @param callable|null $funcKey {(v, k) ==> key} State update function to run on key after every iteration of the generator loop. Default: increment. |
96 | 96 | * @param mixed $seedKey Initial state of the generator loop ofr keys. Default: 0. |
97 | - * @return Enumerable |
|
97 | + * @return EnumerableGeneration |
|
98 | 98 | * @package YaLinqo\Generation |
99 | 99 | */ |
100 | 100 | public static function generate($funcValue, $seedValue = null, $funcKey = null, $seedKey = null) |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * <p><b>Syntax</b>: toInfinity ([start [, step]]) |
122 | 122 | * @param int $start The first integer in the sequence. Default: 0. |
123 | 123 | * @param int $step The difference between adjacent integers. Default: 1. |
124 | - * @return Enumerable |
|
124 | + * @return EnumerableGeneration |
|
125 | 125 | * @package YaLinqo\Generation |
126 | 126 | */ |
127 | 127 | public static function toInfinity($start = 0, $step = 1) |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | * @param string $subject The input string. |
140 | 140 | * @param string $pattern The pattern to search for, as a string. |
141 | 141 | * @param int $flags Can be a combination of the following flags: PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE. Default: PREG_SET_ORDER. |
142 | - * @return Enumerable |
|
142 | + * @return EnumerableGeneration |
|
143 | 143 | * @see preg_match_all |
144 | 144 | * @package YaLinqo\Generation |
145 | 145 | */ |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | * <p><b>Syntax</b>: toNegativeInfinity ([start [, step]]) |
157 | 157 | * @param int $start The first integer in the sequence. Default: 0. |
158 | 158 | * @param int $step The difference between adjacent integers. Default: 1. |
159 | - * @return Enumerable |
|
159 | + * @return EnumerableGeneration |
|
160 | 160 | * @package YaLinqo\Generation |
161 | 161 | */ |
162 | 162 | public static function toNegativeInfinity($start = 0, $step = 1) |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | * Returns a sequence that contains a single element with a specified value. |
169 | 169 | * <p><b>Syntax</b>: returnEnum (element) |
170 | 170 | * @param mixed $element The single element in the resulting sequence. |
171 | - * @return Enumerable Observable sequence containing the single specified element. |
|
171 | + * @return EnumerableGeneration Observable sequence containing the single specified element. |
|
172 | 172 | * @package YaLinqo\Generation |
173 | 173 | */ |
174 | 174 | public static function returnEnum($element) |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | * @param int $start The value of the first integer in the sequence. |
185 | 185 | * @param int $count The number of integers to generate. |
186 | 186 | * @param int $step The difference between adjacent integers. Default: 1. |
187 | - * @return Enumerable A sequence that contains a range of integral numbers. |
|
187 | + * @return EnumerableGeneration A sequence that contains a range of integral numbers. |
|
188 | 188 | * @package YaLinqo\Generation |
189 | 189 | */ |
190 | 190 | public static function range($start, $count, $step = 1) |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | * @param int $start The value of the first integer in the sequence. |
207 | 207 | * @param int $count The number of integers to generate. |
208 | 208 | * @param int $step The difference between adjacent integers. Default: 1. |
209 | - * @return Enumerable A sequence that contains a range of integral numbers. |
|
209 | + * @return EnumerableGeneration A sequence that contains a range of integral numbers. |
|
210 | 210 | * @package YaLinqo\Generation |
211 | 211 | */ |
212 | 212 | public static function rangeDown($start, $count, $step = 1) |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | * @param int $end The value of the last integer in the sequence (not included). |
224 | 224 | * @param int $step The difference between adjacent integers. Default: 1. |
225 | 225 | * @throws \InvalidArgumentException If step is not a positive number. |
226 | - * @return Enumerable A sequence that contains a range of integral numbers. |
|
226 | + * @return EnumerableGeneration A sequence that contains a range of integral numbers. |
|
227 | 227 | * @package YaLinqo\Generation |
228 | 228 | */ |
229 | 229 | public static function rangeTo($start, $end, $step = 1) |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | * @param int $element The value to be repeated. |
253 | 253 | * @param int $count The number of times to repeat the value in the generated sequence. Default: null. |
254 | 254 | * @throws \InvalidArgumentException If count is less than 0. |
255 | - * @return Enumerable A sequence that contains a repeated value. |
|
255 | + * @return EnumerableGeneration A sequence that contains a repeated value. |
|
256 | 256 | * @package YaLinqo\Generation |
257 | 257 | */ |
258 | 258 | public static function repeat($element, $count = null) |
@@ -271,7 +271,7 @@ discard block |
||
271 | 271 | * @param string $subject The input string. |
272 | 272 | * @param string $pattern The pattern to search for, as a string. |
273 | 273 | * @param int $flags flags can be any combination of the following flags: PREG_SPLIT_NO_EMPTY, PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE. Default: 0. |
274 | - * @return Enumerable |
|
274 | + * @return EnumerableGeneration |
|
275 | 275 | * @see preg_split |
276 | 276 | * @package YaLinqo\Generation |
277 | 277 | */ |
@@ -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) |
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($start, $count, $step = 1) |
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($start, $end, $step = 1) |
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) |
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 |
@@ -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($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($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,8 +337,9 @@ discard block |
||
325 | 337 | public function indexOf($value) |
326 | 338 | { |
327 | 339 | foreach ($this as $k => $v) { |
328 | - if ($v === $value) |
|
329 | - return $k; |
|
340 | + if ($v === $value) { |
|
341 | + return $k; |
|
342 | + } |
|
330 | 343 | } |
331 | 344 | return null; // not -1 |
332 | 345 | } |
@@ -343,8 +356,9 @@ discard block |
||
343 | 356 | { |
344 | 357 | $key = null; |
345 | 358 | foreach ($this as $k => $v) { |
346 | - if ($v === $value) |
|
347 | - $key = $k; |
|
359 | + if ($v === $value) { |
|
360 | + $key = $k; |
|
361 | + } |
|
348 | 362 | } |
349 | 363 | return $key; // not -1 |
350 | 364 | } |
@@ -362,8 +376,9 @@ discard block |
||
362 | 376 | $predicate = Utils::createLambda($predicate, 'v,k'); |
363 | 377 | |
364 | 378 | foreach ($this as $k => $v) { |
365 | - if ($predicate($v, $k)) |
|
366 | - return $k; |
|
379 | + if ($predicate($v, $k)) { |
|
380 | + return $k; |
|
381 | + } |
|
367 | 382 | } |
368 | 383 | return null; // not -1 |
369 | 384 | } |
@@ -382,8 +397,9 @@ discard block |
||
382 | 397 | |
383 | 398 | $key = null; |
384 | 399 | foreach ($this as $k => $v) { |
385 | - if ($predicate($v, $k)) |
|
386 | - $key = $k; |
|
400 | + if ($predicate($v, $k)) { |
|
401 | + $key = $k; |
|
402 | + } |
|
387 | 403 | } |
388 | 404 | return $key; // not -1 |
389 | 405 | } |
@@ -402,8 +418,9 @@ discard block |
||
402 | 418 | return new self(function() use ($count) { |
403 | 419 | $it = $this->getIterator(); |
404 | 420 | $it->rewind(); |
405 | - for ($i = 0; $i < $count && $it->valid(); ++$i) |
|
406 | - $it->next(); |
|
421 | + for ($i = 0; $i < $count && $it->valid(); ++$i) { |
|
422 | + $it->next(); |
|
423 | + } |
|
407 | 424 | while ($it->valid()) { |
408 | 425 | yield $it->key() => $it->current(); |
409 | 426 | $it->next(); |
@@ -427,10 +444,12 @@ discard block |
||
427 | 444 | return new self(function() use ($predicate) { |
428 | 445 | $yielding = false; |
429 | 446 | foreach ($this as $k => $v) { |
430 | - if (!$yielding && !$predicate($v, $k)) |
|
431 | - $yielding = true; |
|
432 | - if ($yielding) |
|
433 | - yield $k => $v; |
|
447 | + if (!$yielding && !$predicate($v, $k)) { |
|
448 | + $yielding = true; |
|
449 | + } |
|
450 | + if ($yielding) { |
|
451 | + yield $k => $v; |
|
452 | + } |
|
434 | 453 | } |
435 | 454 | }); |
436 | 455 | } |
@@ -446,14 +465,16 @@ discard block |
||
446 | 465 | */ |
447 | 466 | public function take($count) |
448 | 467 | { |
449 | - if ($count <= 0) |
|
450 | - return new self(new \EmptyIterator, false); |
|
468 | + if ($count <= 0) { |
|
469 | + return new self(new \EmptyIterator, false); |
|
470 | + } |
|
451 | 471 | |
452 | 472 | return new self(function() use ($count) { |
453 | 473 | foreach ($this as $k => $v) { |
454 | 474 | yield $k => $v; |
455 | - if (--$count == 0) |
|
456 | - break; |
|
475 | + if (--$count == 0) { |
|
476 | + break; |
|
477 | + } |
|
457 | 478 | } |
458 | 479 | }); |
459 | 480 | } |
@@ -473,8 +494,9 @@ discard block |
||
473 | 494 | |
474 | 495 | return new self(function() use ($predicate) { |
475 | 496 | foreach ($this as $k => $v) { |
476 | - if (!$predicate($v, $k)) |
|
477 | - break; |
|
497 | + if (!$predicate($v, $k)) { |
|
498 | + break; |
|
499 | + } |
|
478 | 500 | yield $k => $v; |
479 | 501 | } |
480 | 502 | }); |
@@ -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) { |
@@ -65,7 +65,7 @@ discard block |
||
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 |
@@ -122,6 +122,9 @@ discard block |
||
122 | 122 | return $this->sortByMultipleFields($array, $canMultisort); |
123 | 123 | } |
124 | 124 | |
125 | + /** |
|
126 | + * @param boolean $canMultisort |
|
127 | + */ |
|
125 | 128 | private function trySortBySingleField($array, $canMultisort) |
126 | 129 | { |
127 | 130 | if ($this->parent !== null || $array === null) { |
@@ -149,6 +152,9 @@ discard block |
||
149 | 152 | return new \ArrayIterator($array); |
150 | 153 | } |
151 | 154 | |
155 | + /** |
|
156 | + * @param boolean $canMultisort |
|
157 | + */ |
|
152 | 158 | private function sortByMultipleFields($array, $canMultisort) |
153 | 159 | { |
154 | 160 | $orders = []; |
@@ -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); |
@@ -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($orders, $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, $orders, $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, $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 | }); |
@@ -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"); |
@@ -57,16 +57,20 @@ discard block |
||
57 | 57 | public static function createLambda($closure, $closureArgs, $default = null) |
58 | 58 | { |
59 | 59 | if ($closure === null) { |
60 | - if ($default === null) |
|
61 | - throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
60 | + if ($default === null) { |
|
61 | + throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
62 | + } |
|
62 | 63 | return $default; |
63 | 64 | } |
64 | - if ($closure instanceof \Closure) |
|
65 | - return $closure; |
|
66 | - if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) |
|
67 | - return $function; |
|
68 | - if (is_callable($closure)) |
|
69 | - return $closure; |
|
65 | + if ($closure instanceof \Closure) { |
|
66 | + return $closure; |
|
67 | + } |
|
68 | + if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) { |
|
69 | + return $function; |
|
70 | + } |
|
71 | + if (is_callable($closure)) { |
|
72 | + return $closure; |
|
73 | + } |
|
70 | 74 | throw new \InvalidArgumentException(self::ERROR_CLOSURE_NOT_CALLABLE); |
71 | 75 | } |
72 | 76 | |
@@ -84,8 +88,7 @@ discard block |
||
84 | 88 | if ($closure === null) { |
85 | 89 | $isReversed = false; |
86 | 90 | return $sortOrder === SORT_DESC ? Functions::$compareStrictReversed : Functions::$compareStrict; |
87 | - } |
|
88 | - elseif (is_int($closure)) { |
|
91 | + } elseif (is_int($closure)) { |
|
89 | 92 | switch ($closure) { |
90 | 93 | case SORT_REGULAR: |
91 | 94 | return Functions::$compareStrict; |
@@ -117,14 +120,16 @@ discard block |
||
117 | 120 | */ |
118 | 121 | public static function lambdaToSortFlagsAndOrder($closure, &$sortOrder) |
119 | 122 | { |
120 | - if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) |
|
121 | - $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
122 | - if (is_int($closure)) |
|
123 | - return $closure; |
|
124 | - elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) |
|
125 | - return self::$compareFunctionToSortFlags[$closure]; |
|
126 | - else |
|
127 | - return null; |
|
123 | + if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) { |
|
124 | + $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
125 | + } |
|
126 | + if (is_int($closure)) { |
|
127 | + return $closure; |
|
128 | + } elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) { |
|
129 | + return self::$compareFunctionToSortFlags[$closure]; |
|
130 | + } else { |
|
131 | + return null; |
|
132 | + } |
|
128 | 133 | } |
129 | 134 | |
130 | 135 | /** |
@@ -138,24 +143,26 @@ discard block |
||
138 | 143 | { |
139 | 144 | $posDollar = strpos($closure, '$'); |
140 | 145 | if ($posDollar !== false) { |
141 | - if (isset(self::$lambdaCache[$closure][$closureArgs])) |
|
142 | - return self::$lambdaCache[$closure][$closureArgs]; |
|
146 | + if (isset(self::$lambdaCache[$closure][$closureArgs])) { |
|
147 | + return self::$lambdaCache[$closure][$closureArgs]; |
|
148 | + } |
|
143 | 149 | $posArrow = strpos($closure, '==>', $posDollar); |
144 | 150 | if ($posArrow !== false) { |
145 | 151 | $args = trim(substr($closure, 0, $posArrow), "() \r\n\t"); |
146 | 152 | $code = substr($closure, $posArrow + 3); |
147 | - } |
|
148 | - else { |
|
153 | + } else { |
|
149 | 154 | $args = '$' . str_replace(',', '=null,$', $closureArgs) . '=null'; |
150 | 155 | $code = $closure; |
151 | 156 | } |
152 | 157 | $code = trim($code, " \r\n\t"); |
153 | - if (strlen($code) > 0 && $code[0] != '{') |
|
154 | - $code = "return {$code};"; |
|
158 | + if (strlen($code) > 0 && $code[0] != '{') { |
|
159 | + $code = "return {$code};"; |
|
160 | + } |
|
155 | 161 | $fun = @create_function($args, $code); |
156 | 162 | // @codeCoverageIgnoreStart |
157 | - if (!$fun) |
|
158 | - throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
163 | + if (!$fun) { |
|
164 | + throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
165 | + } |
|
159 | 166 | // @codeCoverageIgnoreEnd |
160 | 167 | self::$lambdaCache[$closure][$closureArgs] = $fun; |
161 | 168 | return $fun; |