@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | |
35 | 35 | /** |
36 | 36 | * @internal |
37 | - * @param \Closure|\Iterator $iterator |
|
37 | + * @param \Closure $iterator |
|
38 | 38 | * @param bool $isClosure |
39 | 39 | */ |
40 | 40 | private function __construct ($iterator, $isClosure = true) |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | * <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. |
213 | 213 | * <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. |
214 | 214 | * <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. |
215 | - * @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). |
|
215 | + * @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). |
|
216 | 216 | * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value. |
217 | 217 | * @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. |
218 | 218 | * @return OrderedEnumerable |
@@ -448,7 +448,7 @@ discard block |
||
448 | 448 | * <p>Computes the average of a sequence of numeric values that are obtained by invoking a transform function on each element of the input sequence. |
449 | 449 | * @param callable|null $selector {(v, k) ==> result} A transform function to apply to each element. Default: value. |
450 | 450 | * @throws \UnexpectedValueException If sequence contains no elements. |
451 | - * @return number The average of the sequence of values. |
|
451 | + * @return integer The average of the sequence of values. |
|
452 | 452 | * @package YaLinqo\Aggregation |
453 | 453 | */ |
454 | 454 | public function average ($selector = null) |
@@ -595,7 +595,7 @@ discard block |
||
595 | 595 | * <p>Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence. |
596 | 596 | * <p>This method returns zero if source contains no elements. |
597 | 597 | * @param callable|null $selector {(v, k) ==> result} A transform function to apply to each element. |
598 | - * @return number The sum of the values in the sequence. |
|
598 | + * @return integer The sum of the values in the sequence. |
|
599 | 599 | * @package YaLinqo\Aggregation |
600 | 600 | */ |
601 | 601 | public function sum ($selector = null) |
@@ -850,7 +850,7 @@ discard block |
||
850 | 850 | |
851 | 851 | /** |
852 | 852 | * Proc for {@link toArrayDeep}. |
853 | - * @param $enum \Traversable Source sequence. |
|
853 | + * @param Enumerable $enum \Traversable Source sequence. |
|
854 | 854 | * @return array An array that contains the elements from the input sequence. |
855 | 855 | * @package YaLinqo\Conversion |
856 | 856 | */ |
@@ -900,7 +900,7 @@ discard block |
||
900 | 900 | |
901 | 901 | /** |
902 | 902 | * Proc for {@link toListDeep}. |
903 | - * @param $enum \Traversable Source sequence. |
|
903 | + * @param Enumerable $enum \Traversable Source sequence. |
|
904 | 904 | * @return array An array that contains the elements from the input sequence. |
905 | 905 | * @package YaLinqo\Conversion |
906 | 906 | */ |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | * @param \Closure|\Iterator $iterator |
38 | 38 | * @param bool $isClosure |
39 | 39 | */ |
40 | - private function __construct ($iterator, $isClosure = true) |
|
40 | + private function __construct($iterator, $isClosure = true) |
|
41 | 41 | { |
42 | 42 | $this->iterator = $isClosure ? $iterator() : $iterator; |
43 | 43 | } |
@@ -47,12 +47,12 @@ discard block |
||
47 | 47 | * {@inheritdoc} |
48 | 48 | * @return \Iterator |
49 | 49 | */ |
50 | - public function getIterator () |
|
50 | + public function getIterator() |
|
51 | 51 | { |
52 | 52 | return $this->iterator; |
53 | 53 | } |
54 | 54 | |
55 | - protected function tryGetArrayCopy () |
|
55 | + protected function tryGetArrayCopy() |
|
56 | 56 | { |
57 | 57 | /** @var $it \Iterator|\ArrayIterator */ |
58 | 58 | $it = $this->iterator; |
@@ -70,26 +70,26 @@ discard block |
||
70 | 70 | * @link http://php.net/manual/language.types.type-juggling.php Type Juggling |
71 | 71 | * @package YaLinqo\Projection and filtering |
72 | 72 | */ |
73 | - public function cast ($type) |
|
73 | + public function cast($type) |
|
74 | 74 | { |
75 | 75 | switch ($type) { |
76 | 76 | case 'array': |
77 | - return $this->select(function ($v) { return (array)$v; }); |
|
77 | + return $this->select(function($v) { return (array) $v; }); |
|
78 | 78 | case 'int': |
79 | 79 | case 'integer': |
80 | 80 | case 'long': |
81 | - return $this->select(function ($v) { return (int)$v; }); |
|
81 | + return $this->select(function($v) { return (int) $v; }); |
|
82 | 82 | case 'float': |
83 | 83 | case 'real': |
84 | 84 | case 'double': |
85 | - return $this->select(function ($v) { return (float)$v; }); |
|
85 | + return $this->select(function($v) { return (float) $v; }); |
|
86 | 86 | case 'null': |
87 | 87 | case 'unset': |
88 | - return $this->select(function ($v) { return (unset)$v; }); |
|
88 | + return $this->select(function($v) { return (unset) $v; }); |
|
89 | 89 | case 'object': |
90 | - return $this->select(function ($v) { return (object)$v; }); |
|
90 | + return $this->select(function($v) { return (object) $v; }); |
|
91 | 91 | case 'string': |
92 | - return $this->select(function ($v) { return (string)$v; }); |
|
92 | + return $this->select(function($v) { return (string) $v; }); |
|
93 | 93 | default: |
94 | 94 | throw new \InvalidArgumentException(Errors::UNSUPPORTED_BUILTIN_TYPE); |
95 | 95 | } |
@@ -103,34 +103,34 @@ discard block |
||
103 | 103 | * @return Enumerable A sequence that contains elements from the input sequence of the specified type. |
104 | 104 | * @package YaLinqo\Projection and filtering |
105 | 105 | */ |
106 | - public function ofType ($type) |
|
106 | + public function ofType($type) |
|
107 | 107 | { |
108 | 108 | switch ($type) { |
109 | 109 | case 'array': |
110 | - return $this->where(function ($v) { return is_array($v); }); |
|
110 | + return $this->where(function($v) { return is_array($v); }); |
|
111 | 111 | case 'int': |
112 | 112 | case 'integer': |
113 | 113 | case 'long': |
114 | - return $this->where(function ($v) { return is_int($v); }); |
|
114 | + return $this->where(function($v) { return is_int($v); }); |
|
115 | 115 | case 'callable': |
116 | 116 | case 'callback': |
117 | - return $this->where(function ($v) { return is_callable($v); }); |
|
117 | + return $this->where(function($v) { return is_callable($v); }); |
|
118 | 118 | case 'float': |
119 | 119 | case 'real': |
120 | 120 | case 'double': |
121 | - return $this->where(function ($v) { return is_float($v); }); |
|
121 | + return $this->where(function($v) { return is_float($v); }); |
|
122 | 122 | case 'null': |
123 | - return $this->where(function ($v) { return is_null($v); }); |
|
123 | + return $this->where(function($v) { return is_null($v); }); |
|
124 | 124 | case 'numeric': |
125 | - return $this->where(function ($v) { return is_numeric($v); }); |
|
125 | + return $this->where(function($v) { return is_numeric($v); }); |
|
126 | 126 | case 'object': |
127 | - return $this->where(function ($v) { return is_object($v); }); |
|
127 | + return $this->where(function($v) { return is_object($v); }); |
|
128 | 128 | case 'scalar': |
129 | - return $this->where(function ($v) { return is_scalar($v); }); |
|
129 | + return $this->where(function($v) { return is_scalar($v); }); |
|
130 | 130 | case 'string': |
131 | - return $this->where(function ($v) { return is_string($v); }); |
|
131 | + return $this->where(function($v) { return is_string($v); }); |
|
132 | 132 | default: |
133 | - return $this->where(function ($v) use ($type) { return is_object($v) && get_class($v) === $type; }); |
|
133 | + return $this->where(function($v) use ($type) { return is_object($v) && get_class($v) === $type; }); |
|
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
@@ -143,12 +143,12 @@ discard block |
||
143 | 143 | * @return Enumerable A sequence whose elements are the result of invoking the transform functions on each element of source. |
144 | 144 | * @package YaLinqo\Projection and filtering |
145 | 145 | */ |
146 | - public function select ($selectorValue, $selectorKey = null) |
|
146 | + public function select($selectorValue, $selectorKey = null) |
|
147 | 147 | { |
148 | 148 | $selectorValue = Utils::createLambda($selectorValue, 'v,k'); |
149 | 149 | $selectorKey = Utils::createLambda($selectorKey, 'v,k', Functions::$key); |
150 | 150 | |
151 | - return new self(function () use ($selectorValue, $selectorKey) { |
|
151 | + return new self(function() use ($selectorValue, $selectorKey) { |
|
152 | 152 | foreach ($this as $k => $v) |
153 | 153 | yield $selectorKey($v, $k) => $selectorValue($v, $k); |
154 | 154 | }); |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | * @return Enumerable A sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence. |
170 | 170 | * @package YaLinqo\Projection and filtering |
171 | 171 | */ |
172 | - public function selectMany ($collectionSelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
172 | + public function selectMany($collectionSelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
173 | 173 | { |
174 | 174 | $collectionSelector = Utils::createLambda($collectionSelector, 'v,k', Functions::$value); |
175 | 175 | $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,k1,k2', Functions::$value); |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | if ($resultSelectorKey === false) |
178 | 178 | $resultSelectorKey = Functions::increment(); |
179 | 179 | |
180 | - return new self(function () use ($collectionSelector, $resultSelectorValue, $resultSelectorKey) { |
|
180 | + return new self(function() use ($collectionSelector, $resultSelectorValue, $resultSelectorKey) { |
|
181 | 181 | foreach ($this as $ok => $ov) |
182 | 182 | foreach ($collectionSelector($ov, $ok) as $ik => $iv) |
183 | 183 | yield $resultSelectorKey($iv, $ok, $ik) => $resultSelectorValue($iv, $ok, $ik); |
@@ -191,11 +191,11 @@ discard block |
||
191 | 191 | * @return Enumerable A sequence that contains elements from the input sequence that satisfy the condition. |
192 | 192 | * @package YaLinqo\Projection and filtering |
193 | 193 | */ |
194 | - public function where ($predicate) |
|
194 | + public function where($predicate) |
|
195 | 195 | { |
196 | 196 | $predicate = Utils::createLambda($predicate, 'v,k'); |
197 | 197 | |
198 | - return new self(function () use ($predicate) { |
|
198 | + return new self(function() use ($predicate) { |
|
199 | 199 | foreach ($this as $k => $v) |
200 | 200 | if ($predicate($v, $k)) |
201 | 201 | yield $k => $v; |
@@ -218,7 +218,7 @@ discard block |
||
218 | 218 | * @return OrderedEnumerable |
219 | 219 | * @package YaLinqo\Ordering |
220 | 220 | */ |
221 | - public function orderByDir ($sortOrder, $keySelector = null, $comparer = null) |
|
221 | + public function orderByDir($sortOrder, $keySelector = null, $comparer = null) |
|
222 | 222 | { |
223 | 223 | $sortFlags = Utils::lambdaToSortFlagsAndOrder($comparer, $sortOrder); |
224 | 224 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value); |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | * @return OrderedEnumerable |
239 | 239 | * @package YaLinqo\Ordering |
240 | 240 | */ |
241 | - public function orderBy ($keySelector = null, $comparer = null) |
|
241 | + public function orderBy($keySelector = null, $comparer = null) |
|
242 | 242 | { |
243 | 243 | return $this->orderByDir(false, $keySelector, $comparer); |
244 | 244 | } |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | * @return OrderedEnumerable |
255 | 255 | * @package YaLinqo\Ordering |
256 | 256 | */ |
257 | - public function orderByDescending ($keySelector = null, $comparer = null) |
|
257 | + public function orderByDescending($keySelector = null, $comparer = null) |
|
258 | 258 | { |
259 | 259 | return $this->orderByDir(true, $keySelector, $comparer); |
260 | 260 | } |
@@ -277,17 +277,17 @@ discard block |
||
277 | 277 | * @return Enumerable A sequence that contains elements that are obtained by performing a grouped join on two sequences. |
278 | 278 | * @package YaLinqo\Joining and grouping |
279 | 279 | */ |
280 | - public function groupJoin ($inner, $outerKeySelector = null, $innerKeySelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
280 | + public function groupJoin($inner, $outerKeySelector = null, $innerKeySelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
281 | 281 | { |
282 | 282 | $inner = self::from($inner); |
283 | 283 | $outerKeySelector = Utils::createLambda($outerKeySelector, 'v,k', Functions::$key); |
284 | 284 | $innerKeySelector = Utils::createLambda($innerKeySelector, 'v,k', Functions::$key); |
285 | 285 | /** @noinspection PhpUnusedParameterInspection */ |
286 | - $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,e,k', function ($v, $e, $k) { return [ $v, $e ]; }); |
|
286 | + $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,e,k', function($v, $e, $k) { return [$v, $e]; }); |
|
287 | 287 | /** @noinspection PhpUnusedParameterInspection */ |
288 | - $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v,e,k', function ($v, $e, $k) { return $k; }); |
|
288 | + $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v,e,k', function($v, $e, $k) { return $k; }); |
|
289 | 289 | |
290 | - return new self(function () use ($inner, $outerKeySelector, $innerKeySelector, $resultSelectorValue, $resultSelectorKey) { |
|
290 | + return new self(function() use ($inner, $outerKeySelector, $innerKeySelector, $resultSelectorValue, $resultSelectorKey) { |
|
291 | 291 | $lookup = $inner->toLookup($innerKeySelector); |
292 | 292 | foreach ($this as $k => $v) { |
293 | 293 | $key = $outerKeySelector($v, $k); |
@@ -311,17 +311,17 @@ discard block |
||
311 | 311 | * @return Enumerable |
312 | 312 | * @package YaLinqo\Joining and grouping |
313 | 313 | */ |
314 | - public function join ($inner, $outerKeySelector = null, $innerKeySelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
314 | + public function join($inner, $outerKeySelector = null, $innerKeySelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
315 | 315 | { |
316 | 316 | $inner = self::from($inner); |
317 | 317 | $outerKeySelector = Utils::createLambda($outerKeySelector, 'v,k', Functions::$key); |
318 | 318 | $innerKeySelector = Utils::createLambda($innerKeySelector, 'v,k', Functions::$key); |
319 | 319 | /** @noinspection PhpUnusedParameterInspection */ |
320 | - $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v1,v2,k', function ($v1, $v2, $k) { return [ $v1, $v2 ]; }); |
|
320 | + $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v1,v2,k', function($v1, $v2, $k) { return [$v1, $v2]; }); |
|
321 | 321 | /** @noinspection PhpUnusedParameterInspection */ |
322 | - $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v1,v2,k', function ($v1, $v2, $k) { return $k; }); |
|
322 | + $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v1,v2,k', function($v1, $v2, $k) { return $k; }); |
|
323 | 323 | |
324 | - return new self(function () use ($inner, $outerKeySelector, $innerKeySelector, $resultSelectorValue, $resultSelectorKey) { |
|
324 | + return new self(function() use ($inner, $outerKeySelector, $innerKeySelector, $resultSelectorValue, $resultSelectorKey) { |
|
325 | 325 | $lookup = $inner->toLookup($innerKeySelector); |
326 | 326 | foreach ($this as $ok => $ov) { |
327 | 327 | $key = $outerKeySelector($ov, $ok); |
@@ -350,7 +350,7 @@ discard block |
||
350 | 350 | * @return Enumerable A sequence of sequences indexed by a key. |
351 | 351 | * @package YaLinqo\Joining and grouping |
352 | 352 | */ |
353 | - public function groupBy ($keySelector = null, $valueSelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
353 | + public function groupBy($keySelector = null, $valueSelector = null, $resultSelectorValue = null, $resultSelectorKey = null) |
|
354 | 354 | { |
355 | 355 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$key); |
356 | 356 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
@@ -376,7 +376,7 @@ discard block |
||
376 | 376 | * @return mixed The final accumulator value. |
377 | 377 | * @package YaLinqo\Aggregation |
378 | 378 | */ |
379 | - public function aggregate ($func, $seed = null) |
|
379 | + public function aggregate($func, $seed = null) |
|
380 | 380 | { |
381 | 381 | $func = Utils::createLambda($func, 'a,v,k'); |
382 | 382 | |
@@ -414,7 +414,7 @@ discard block |
||
414 | 414 | * @return mixed The final accumulator value, or default if sequence is empty. |
415 | 415 | * @package YaLinqo\Aggregation |
416 | 416 | */ |
417 | - public function aggregateOrDefault ($func, $seed = null, $default = null) |
|
417 | + public function aggregateOrDefault($func, $seed = null, $default = null) |
|
418 | 418 | { |
419 | 419 | $func = Utils::createLambda($func, 'a,v,k'); |
420 | 420 | $result = $seed; |
@@ -451,7 +451,7 @@ discard block |
||
451 | 451 | * @return number The average of the sequence of values. |
452 | 452 | * @package YaLinqo\Aggregation |
453 | 453 | */ |
454 | - public function average ($selector = null) |
|
454 | + public function average($selector = null) |
|
455 | 455 | { |
456 | 456 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
457 | 457 | $sum = $count = 0; |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | * @return int The number of elements in the input sequence. |
476 | 476 | * @package YaLinqo\Aggregation |
477 | 477 | */ |
478 | - public function count ($predicate = null) |
|
478 | + public function count($predicate = null) |
|
479 | 479 | { |
480 | 480 | $it = $this->getIterator(); |
481 | 481 | |
@@ -502,7 +502,7 @@ discard block |
||
502 | 502 | * @return number The maximum value in the sequence. |
503 | 503 | * @package YaLinqo\Aggregation |
504 | 504 | */ |
505 | - public function max ($selector = null) |
|
505 | + public function max($selector = null) |
|
506 | 506 | { |
507 | 507 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
508 | 508 | |
@@ -529,14 +529,14 @@ discard block |
||
529 | 529 | * @return number The maximum value in the sequence. |
530 | 530 | * @package YaLinqo\Aggregation |
531 | 531 | */ |
532 | - public function maxBy ($comparer, $selector = null) |
|
532 | + public function maxBy($comparer, $selector = null) |
|
533 | 533 | { |
534 | 534 | $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict); |
535 | 535 | $enum = $this; |
536 | 536 | |
537 | 537 | if ($selector !== null) |
538 | 538 | $enum = $enum->select($selector); |
539 | - return $enum->aggregate(function ($a, $b) use ($comparer) { return $comparer($a, $b) > 0 ? $a : $b; }); |
|
539 | + return $enum->aggregate(function($a, $b) use ($comparer) { return $comparer($a, $b) > 0 ? $a : $b; }); |
|
540 | 540 | } |
541 | 541 | |
542 | 542 | /** |
@@ -550,7 +550,7 @@ discard block |
||
550 | 550 | * @return number The minimum value in the sequence. |
551 | 551 | * @package YaLinqo\Aggregation |
552 | 552 | */ |
553 | - public function min ($selector = null) |
|
553 | + public function min($selector = null) |
|
554 | 554 | { |
555 | 555 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
556 | 556 | |
@@ -577,14 +577,14 @@ discard block |
||
577 | 577 | * @return number The minimum value in the sequence. |
578 | 578 | * @package YaLinqo\Aggregation |
579 | 579 | */ |
580 | - public function minBy ($comparer, $selector = null) |
|
580 | + public function minBy($comparer, $selector = null) |
|
581 | 581 | { |
582 | 582 | $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict); |
583 | 583 | $enum = $this; |
584 | 584 | |
585 | 585 | if ($selector !== null) |
586 | 586 | $enum = $enum->select($selector); |
587 | - return $enum->aggregate(function ($a, $b) use ($comparer) { return $comparer($a, $b) < 0 ? $a : $b; }); |
|
587 | + return $enum->aggregate(function($a, $b) use ($comparer) { return $comparer($a, $b) < 0 ? $a : $b; }); |
|
588 | 588 | } |
589 | 589 | |
590 | 590 | /** |
@@ -598,7 +598,7 @@ discard block |
||
598 | 598 | * @return number The sum of the values in the sequence. |
599 | 599 | * @package YaLinqo\Aggregation |
600 | 600 | */ |
601 | - public function sum ($selector = null) |
|
601 | + public function sum($selector = null) |
|
602 | 602 | { |
603 | 603 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
604 | 604 | |
@@ -620,7 +620,7 @@ discard block |
||
620 | 620 | * @return bool true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false. |
621 | 621 | * @package YaLinqo\Sets |
622 | 622 | */ |
623 | - public function all ($predicate) |
|
623 | + public function all($predicate) |
|
624 | 624 | { |
625 | 625 | $predicate = Utils::createLambda($predicate, 'v,k'); |
626 | 626 | |
@@ -641,7 +641,7 @@ discard block |
||
641 | 641 | * @return bool If predicate is null: true if the source sequence contains any elements; otherwise, false. If predicate is not null: true if any elements in the source sequence pass the test in the specified predicate; otherwise, false. |
642 | 642 | * @package YaLinqo\Sets |
643 | 643 | */ |
644 | - public function any ($predicate = null) |
|
644 | + public function any($predicate = null) |
|
645 | 645 | { |
646 | 646 | $predicate = Utils::createLambda($predicate, 'v,k', false); |
647 | 647 | |
@@ -669,7 +669,7 @@ discard block |
||
669 | 669 | * @return bool true if the source sequence contains an element that has the specified value; otherwise, false. |
670 | 670 | * @package YaLinqo\Sets |
671 | 671 | */ |
672 | - public function contains ($value) |
|
672 | + public function contains($value) |
|
673 | 673 | { |
674 | 674 | foreach ($this as $v) { |
675 | 675 | if ($v === $value) |
@@ -689,12 +689,12 @@ discard block |
||
689 | 689 | * @return Enumerable A sequence that contains distinct elements of the input sequence. |
690 | 690 | * @package YaLinqo\Sets |
691 | 691 | */ |
692 | - public function distinct ($keySelector = null) |
|
692 | + public function distinct($keySelector = null) |
|
693 | 693 | { |
694 | 694 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value); |
695 | 695 | |
696 | - return new self(function () use ($keySelector) { |
|
697 | - $set = [ ]; |
|
696 | + return new self(function() use ($keySelector) { |
|
697 | + $set = []; |
|
698 | 698 | foreach ($this as $k => $v) { |
699 | 699 | $key = $keySelector($v, $k); |
700 | 700 | if (isset($set[$key])) |
@@ -717,13 +717,13 @@ discard block |
||
717 | 717 | * @return Enumerable A sequence that contains the set difference of the elements of two sequences. |
718 | 718 | * @package YaLinqo\Sets |
719 | 719 | */ |
720 | - public function except ($other, $keySelector = null) |
|
720 | + public function except($other, $keySelector = null) |
|
721 | 721 | { |
722 | 722 | $other = self::from($other); |
723 | 723 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value); |
724 | 724 | |
725 | - return new self(function () use ($other, $keySelector) { |
|
726 | - $set = [ ]; |
|
725 | + return new self(function() use ($other, $keySelector) { |
|
726 | + $set = []; |
|
727 | 727 | foreach ($other as $k => $v) { |
728 | 728 | $key = $keySelector($v, $k); |
729 | 729 | $set[$key] = true; |
@@ -750,13 +750,13 @@ discard block |
||
750 | 750 | * @return Enumerable A sequence that contains the elements that form the set intersection of two sequences. |
751 | 751 | * @package YaLinqo\Sets |
752 | 752 | */ |
753 | - public function intersect ($other, $keySelector = null) |
|
753 | + public function intersect($other, $keySelector = null) |
|
754 | 754 | { |
755 | 755 | $other = self::from($other); |
756 | 756 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value); |
757 | 757 | |
758 | - return new self(function () use ($other, $keySelector) { |
|
759 | - $set = [ ]; |
|
758 | + return new self(function() use ($other, $keySelector) { |
|
759 | + $set = []; |
|
760 | 760 | foreach ($other as $k => $v) { |
761 | 761 | $key = $keySelector($v, $k); |
762 | 762 | $set[$key] = true; |
@@ -784,13 +784,13 @@ discard block |
||
784 | 784 | * @return Enumerable A sequence that contains the elements from both input sequences, excluding duplicates. |
785 | 785 | * @package YaLinqo\Sets |
786 | 786 | */ |
787 | - public function union ($other, $keySelector = null) |
|
787 | + public function union($other, $keySelector = null) |
|
788 | 788 | { |
789 | 789 | $other = self::from($other); |
790 | 790 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value); |
791 | 791 | |
792 | - return new self(function () use ($other, $keySelector) { |
|
793 | - $set = [ ]; |
|
792 | + return new self(function() use ($other, $keySelector) { |
|
793 | + $set = []; |
|
794 | 794 | foreach ($this as $k => $v) { |
795 | 795 | $key = $keySelector($v, $k); |
796 | 796 | if (isset($set[$key])) |
@@ -821,14 +821,14 @@ discard block |
||
821 | 821 | * @return array An array that contains the elements from the input sequence. |
822 | 822 | * @package YaLinqo\Conversion |
823 | 823 | */ |
824 | - public function toArray () |
|
824 | + public function toArray() |
|
825 | 825 | { |
826 | 826 | /** @var $it \Iterator|\ArrayIterator */ |
827 | 827 | $it = $this->getIterator(); |
828 | 828 | if ($it instanceof \ArrayIterator) |
829 | 829 | return $it->getArrayCopy(); |
830 | 830 | |
831 | - $array = [ ]; |
|
831 | + $array = []; |
|
832 | 832 | foreach ($it as $k => $v) |
833 | 833 | $array[$k] = $v; |
834 | 834 | return $array; |
@@ -843,7 +843,7 @@ discard block |
||
843 | 843 | * @return array An array that contains the elements from the input sequence. |
844 | 844 | * @package YaLinqo\Conversion |
845 | 845 | */ |
846 | - public function toArrayDeep () |
|
846 | + public function toArrayDeep() |
|
847 | 847 | { |
848 | 848 | return $this->toArrayDeepProc($this); |
849 | 849 | } |
@@ -854,9 +854,9 @@ discard block |
||
854 | 854 | * @return array An array that contains the elements from the input sequence. |
855 | 855 | * @package YaLinqo\Conversion |
856 | 856 | */ |
857 | - protected function toArrayDeepProc ($enum) |
|
857 | + protected function toArrayDeepProc($enum) |
|
858 | 858 | { |
859 | - $array = [ ]; |
|
859 | + $array = []; |
|
860 | 860 | foreach ($enum as $k => $v) |
861 | 861 | $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
862 | 862 | return $array; |
@@ -871,14 +871,14 @@ discard block |
||
871 | 871 | * @return array An array that contains the elements from the input sequence. |
872 | 872 | * @package YaLinqo\Conversion |
873 | 873 | */ |
874 | - public function toList () |
|
874 | + public function toList() |
|
875 | 875 | { |
876 | 876 | /** @var $it \Iterator|\ArrayIterator */ |
877 | 877 | $it = $this->getIterator(); |
878 | 878 | if ($it instanceof \ArrayIterator) |
879 | 879 | return array_values($it->getArrayCopy()); |
880 | 880 | |
881 | - $array = [ ]; |
|
881 | + $array = []; |
|
882 | 882 | foreach ($it as $v) |
883 | 883 | $array[] = $v; |
884 | 884 | return $array; |
@@ -893,7 +893,7 @@ discard block |
||
893 | 893 | * @return array An array that contains the elements from the input sequence. |
894 | 894 | * @package YaLinqo\Conversion |
895 | 895 | */ |
896 | - public function toListDeep () |
|
896 | + public function toListDeep() |
|
897 | 897 | { |
898 | 898 | return $this->toListDeepProc($this); |
899 | 899 | } |
@@ -904,9 +904,9 @@ discard block |
||
904 | 904 | * @return array An array that contains the elements from the input sequence. |
905 | 905 | * @package YaLinqo\Conversion |
906 | 906 | */ |
907 | - protected function toListDeepProc ($enum) |
|
907 | + protected function toListDeepProc($enum) |
|
908 | 908 | { |
909 | - $array = [ ]; |
|
909 | + $array = []; |
|
910 | 910 | foreach ($enum as $v) |
911 | 911 | $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
912 | 912 | return $array; |
@@ -921,12 +921,12 @@ discard block |
||
921 | 921 | * @return array An array that contains keys and values selected from the input sequence. |
922 | 922 | * @package YaLinqo\Conversion |
923 | 923 | */ |
924 | - public function toDictionary ($keySelector = null, $valueSelector = null) |
|
924 | + public function toDictionary($keySelector = null, $valueSelector = null) |
|
925 | 925 | { |
926 | 926 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$key); |
927 | 927 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
928 | 928 | |
929 | - $dic = [ ]; |
|
929 | + $dic = []; |
|
930 | 930 | foreach ($this as $k => $v) |
931 | 931 | $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
932 | 932 | return $dic; |
@@ -941,7 +941,7 @@ discard block |
||
941 | 941 | * @see json_encode |
942 | 942 | * @package YaLinqo\Conversion |
943 | 943 | */ |
944 | - public function toJSON ($options = 0) |
|
944 | + public function toJSON($options = 0) |
|
945 | 945 | { |
946 | 946 | return json_encode($this->toArrayDeep(), $options); |
947 | 947 | } |
@@ -955,12 +955,12 @@ discard block |
||
955 | 955 | * @return array An array that contains keys and value arrays selected from the input sequence. |
956 | 956 | * @package YaLinqo\Conversion |
957 | 957 | */ |
958 | - public function toLookup ($keySelector = null, $valueSelector = null) |
|
958 | + public function toLookup($keySelector = null, $valueSelector = null) |
|
959 | 959 | { |
960 | 960 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$key); |
961 | 961 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
962 | 962 | |
963 | - $lookup = [ ]; |
|
963 | + $lookup = []; |
|
964 | 964 | foreach ($this as $k => $v) |
965 | 965 | $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
966 | 966 | return $lookup; |
@@ -973,7 +973,7 @@ discard block |
||
973 | 973 | * @see array_keys |
974 | 974 | * @package YaLinqo\Conversion |
975 | 975 | */ |
976 | - public function toKeys () |
|
976 | + public function toKeys() |
|
977 | 977 | { |
978 | 978 | return $this->select(Functions::$key, Functions::increment()); |
979 | 979 | } |
@@ -985,7 +985,7 @@ discard block |
||
985 | 985 | * @see array_values |
986 | 986 | * @package YaLinqo\Conversion |
987 | 987 | */ |
988 | - public function toValues () |
|
988 | + public function toValues() |
|
989 | 989 | { |
990 | 990 | return $this->select(Functions::$value, Functions::increment()); |
991 | 991 | } |
@@ -998,7 +998,7 @@ discard block |
||
998 | 998 | * @return \stdClass |
999 | 999 | * @package YaLinqo\Conversion |
1000 | 1000 | */ |
1001 | - public function toObject ($propertySelector = null, $valueSelector = null) |
|
1001 | + public function toObject($propertySelector = null, $valueSelector = null) |
|
1002 | 1002 | { |
1003 | 1003 | $propertySelector = Utils::createLambda($propertySelector, 'v,k', Functions::$key); |
1004 | 1004 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
@@ -1018,7 +1018,7 @@ discard block |
||
1018 | 1018 | * @see implode |
1019 | 1019 | * @package YaLinqo\Conversion |
1020 | 1020 | */ |
1021 | - public function toString ($separator = '', $valueSelector = null) |
|
1021 | + public function toString($separator = '', $valueSelector = null) |
|
1022 | 1022 | { |
1023 | 1023 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', false); |
1024 | 1024 | $array = $valueSelector ? $this->select($valueSelector)->toArray() : $this->toArray(); |
@@ -1038,11 +1038,11 @@ discard block |
||
1038 | 1038 | * @return Enumerable The source sequence with the side-effecting behavior applied. |
1039 | 1039 | * @package YaLinqo\Actions |
1040 | 1040 | */ |
1041 | - public function call ($action) |
|
1041 | + public function call($action) |
|
1042 | 1042 | { |
1043 | 1043 | $action = Utils::createLambda($action, 'v,k'); |
1044 | 1044 | |
1045 | - return new self(function () use ($action) { |
|
1045 | + return new self(function() use ($action) { |
|
1046 | 1046 | foreach ($this as $k => $v) { |
1047 | 1047 | $action($v, $k); |
1048 | 1048 | yield $k => $v; |
@@ -1058,7 +1058,7 @@ discard block |
||
1058 | 1058 | * @param callable $action {(v, k) ==> void} The action to invoke for each element in the sequence. |
1059 | 1059 | * @package YaLinqo\Actions |
1060 | 1060 | */ |
1061 | - public function each ($action = null) |
|
1061 | + public function each($action = null) |
|
1062 | 1062 | { |
1063 | 1063 | $action = Utils::createLambda($action, 'v,k', Functions::$blank); |
1064 | 1064 | |
@@ -1074,7 +1074,7 @@ discard block |
||
1074 | 1074 | * @see implode, echo |
1075 | 1075 | * @package YaLinqo\Actions |
1076 | 1076 | */ |
1077 | - public function write ($separator = '', $selector = null) |
|
1077 | + public function write($separator = '', $selector = null) |
|
1078 | 1078 | { |
1079 | 1079 | echo $this->toString($separator, $selector); |
1080 | 1080 | } |
@@ -1087,7 +1087,7 @@ discard block |
||
1087 | 1087 | * @see echo, PHP_EOL |
1088 | 1088 | * @package YaLinqo\Actions |
1089 | 1089 | */ |
1090 | - public function writeLine ($selector = null) |
|
1090 | + public function writeLine($selector = null) |
|
1091 | 1091 | { |
1092 | 1092 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
1093 | 1093 |
@@ -149,8 +149,9 @@ discard block |
||
149 | 149 | $selectorKey = Utils::createLambda($selectorKey, 'v,k', Functions::$key); |
150 | 150 | |
151 | 151 | return new self(function () use ($selectorValue, $selectorKey) { |
152 | - foreach ($this as $k => $v) |
|
153 | - yield $selectorKey($v, $k) => $selectorValue($v, $k); |
|
152 | + foreach ($this as $k => $v) { |
|
153 | + yield $selectorKey($v, $k) => $selectorValue($v, $k); |
|
154 | + } |
|
154 | 155 | }); |
155 | 156 | } |
156 | 157 | |
@@ -174,13 +175,15 @@ discard block |
||
174 | 175 | $collectionSelector = Utils::createLambda($collectionSelector, 'v,k', Functions::$value); |
175 | 176 | $resultSelectorValue = Utils::createLambda($resultSelectorValue, 'v,k1,k2', Functions::$value); |
176 | 177 | $resultSelectorKey = Utils::createLambda($resultSelectorKey, 'v,k1,k2', false); |
177 | - if ($resultSelectorKey === false) |
|
178 | - $resultSelectorKey = Functions::increment(); |
|
178 | + if ($resultSelectorKey === false) { |
|
179 | + $resultSelectorKey = Functions::increment(); |
|
180 | + } |
|
179 | 181 | |
180 | 182 | return new self(function () use ($collectionSelector, $resultSelectorValue, $resultSelectorKey) { |
181 | - foreach ($this as $ok => $ov) |
|
182 | - foreach ($collectionSelector($ov, $ok) as $ik => $iv) |
|
183 | + foreach ($this as $ok => $ov) { |
|
184 | + foreach ($collectionSelector($ov, $ok) as $ik => $iv) |
|
183 | 185 | yield $resultSelectorKey($iv, $ok, $ik) => $resultSelectorValue($iv, $ok, $ik); |
186 | + } |
|
184 | 187 | }); |
185 | 188 | } |
186 | 189 | |
@@ -196,9 +199,10 @@ discard block |
||
196 | 199 | $predicate = Utils::createLambda($predicate, 'v,k'); |
197 | 200 | |
198 | 201 | return new self(function () use ($predicate) { |
199 | - foreach ($this as $k => $v) |
|
200 | - if ($predicate($v, $k)) |
|
202 | + foreach ($this as $k => $v) { |
|
203 | + if ($predicate($v, $k)) |
|
201 | 204 | yield $k => $v; |
205 | + } |
|
202 | 206 | }); |
203 | 207 | } |
204 | 208 | |
@@ -325,9 +329,10 @@ discard block |
||
325 | 329 | $lookup = $inner->toLookup($innerKeySelector); |
326 | 330 | foreach ($this as $ok => $ov) { |
327 | 331 | $key = $outerKeySelector($ov, $ok); |
328 | - if (isset($lookup[$key])) |
|
329 | - foreach ($lookup[$key] as $iv) |
|
332 | + if (isset($lookup[$key])) { |
|
333 | + foreach ($lookup[$key] as $iv) |
|
330 | 334 | yield $resultSelectorKey($ov, $iv, $key) => $resultSelectorValue($ov, $iv, $key); |
335 | + } |
|
331 | 336 | } |
332 | 337 | }); |
333 | 338 | } |
@@ -385,20 +390,19 @@ discard block |
||
385 | 390 | foreach ($this as $k => $v) { |
386 | 391 | $result = $func($result, $v, $k); |
387 | 392 | } |
388 | - } |
|
389 | - else { |
|
393 | + } else { |
|
390 | 394 | $assigned = false; |
391 | 395 | foreach ($this as $k => $v) { |
392 | 396 | if ($assigned) { |
393 | 397 | $result = $func($result, $v, $k); |
394 | - } |
|
395 | - else { |
|
398 | + } else { |
|
396 | 399 | $result = $v; |
397 | 400 | $assigned = true; |
398 | 401 | } |
399 | 402 | } |
400 | - if (!$assigned) |
|
401 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
403 | + if (!$assigned) { |
|
404 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
405 | + } |
|
402 | 406 | } |
403 | 407 | return $result; |
404 | 408 | } |
@@ -425,13 +429,11 @@ discard block |
||
425 | 429 | $result = $func($result, $v, $k); |
426 | 430 | $assigned = true; |
427 | 431 | } |
428 | - } |
|
429 | - else { |
|
432 | + } else { |
|
430 | 433 | foreach ($this as $k => $v) { |
431 | 434 | if ($assigned) { |
432 | 435 | $result = $func($result, $v, $k); |
433 | - } |
|
434 | - else { |
|
436 | + } else { |
|
435 | 437 | $result = $v; |
436 | 438 | $assigned = true; |
437 | 439 | } |
@@ -460,8 +462,9 @@ discard block |
||
460 | 462 | $sum += $selector($v, $k); |
461 | 463 | $count++; |
462 | 464 | } |
463 | - if ($count === 0) |
|
464 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
465 | + if ($count === 0) { |
|
466 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
467 | + } |
|
465 | 468 | return $sum / $count; |
466 | 469 | } |
467 | 470 | |
@@ -479,15 +482,17 @@ discard block |
||
479 | 482 | { |
480 | 483 | $it = $this->getIterator(); |
481 | 484 | |
482 | - if ($it instanceof \Countable && $predicate === null) |
|
483 | - return count($it); |
|
485 | + if ($it instanceof \Countable && $predicate === null) { |
|
486 | + return count($it); |
|
487 | + } |
|
484 | 488 | |
485 | 489 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$value); |
486 | 490 | $count = 0; |
487 | 491 | |
488 | - foreach ($it as $k => $v) |
|
489 | - if ($predicate($v, $k)) |
|
492 | + foreach ($it as $k => $v) { |
|
493 | + if ($predicate($v, $k)) |
|
490 | 494 | $count++; |
495 | + } |
|
491 | 496 | return $count; |
492 | 497 | } |
493 | 498 | |
@@ -512,8 +517,9 @@ discard block |
||
512 | 517 | $max = max($max, $selector($v, $k)); |
513 | 518 | $assigned = true; |
514 | 519 | } |
515 | - if (!$assigned) |
|
516 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
520 | + if (!$assigned) { |
|
521 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
522 | + } |
|
517 | 523 | return $max; |
518 | 524 | } |
519 | 525 | |
@@ -534,8 +540,9 @@ discard block |
||
534 | 540 | $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict); |
535 | 541 | $enum = $this; |
536 | 542 | |
537 | - if ($selector !== null) |
|
538 | - $enum = $enum->select($selector); |
|
543 | + if ($selector !== null) { |
|
544 | + $enum = $enum->select($selector); |
|
545 | + } |
|
539 | 546 | return $enum->aggregate(function ($a, $b) use ($comparer) { return $comparer($a, $b) > 0 ? $a : $b; }); |
540 | 547 | } |
541 | 548 | |
@@ -560,8 +567,9 @@ discard block |
||
560 | 567 | $min = min($min, $selector($v, $k)); |
561 | 568 | $assigned = true; |
562 | 569 | } |
563 | - if (!$assigned) |
|
564 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
570 | + if (!$assigned) { |
|
571 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
572 | + } |
|
565 | 573 | return $min; |
566 | 574 | } |
567 | 575 | |
@@ -582,8 +590,9 @@ discard block |
||
582 | 590 | $comparer = Utils::createLambda($comparer, 'a,b', Functions::$compareStrict); |
583 | 591 | $enum = $this; |
584 | 592 | |
585 | - if ($selector !== null) |
|
586 | - $enum = $enum->select($selector); |
|
593 | + if ($selector !== null) { |
|
594 | + $enum = $enum->select($selector); |
|
595 | + } |
|
587 | 596 | return $enum->aggregate(function ($a, $b) use ($comparer) { return $comparer($a, $b) < 0 ? $a : $b; }); |
588 | 597 | } |
589 | 598 | |
@@ -603,8 +612,9 @@ discard block |
||
603 | 612 | $selector = Utils::createLambda($selector, 'v,k', Functions::$value); |
604 | 613 | |
605 | 614 | $sum = 0; |
606 | - foreach ($this as $k => $v) |
|
607 | - $sum += $selector($v, $k); |
|
615 | + foreach ($this as $k => $v) { |
|
616 | + $sum += $selector($v, $k); |
|
617 | + } |
|
608 | 618 | return $sum; |
609 | 619 | } |
610 | 620 | |
@@ -625,8 +635,9 @@ discard block |
||
625 | 635 | $predicate = Utils::createLambda($predicate, 'v,k'); |
626 | 636 | |
627 | 637 | foreach ($this as $k => $v) { |
628 | - if (!$predicate($v, $k)) |
|
629 | - return false; |
|
638 | + if (!$predicate($v, $k)) { |
|
639 | + return false; |
|
640 | + } |
|
630 | 641 | } |
631 | 642 | return true; |
632 | 643 | } |
@@ -647,15 +658,16 @@ discard block |
||
647 | 658 | |
648 | 659 | if ($predicate) { |
649 | 660 | foreach ($this as $k => $v) { |
650 | - if ($predicate($v, $k)) |
|
651 | - return true; |
|
661 | + if ($predicate($v, $k)) { |
|
662 | + return true; |
|
663 | + } |
|
652 | 664 | } |
653 | 665 | return false; |
654 | - } |
|
655 | - else { |
|
666 | + } else { |
|
656 | 667 | $it = $this->getIterator(); |
657 | - if ($it instanceof \Countable) |
|
658 | - return count($it) > 0; |
|
668 | + if ($it instanceof \Countable) { |
|
669 | + return count($it) > 0; |
|
670 | + } |
|
659 | 671 | $it->rewind(); |
660 | 672 | return $it->valid(); |
661 | 673 | } |
@@ -672,8 +684,9 @@ discard block |
||
672 | 684 | public function contains ($value) |
673 | 685 | { |
674 | 686 | foreach ($this as $v) { |
675 | - if ($v === $value) |
|
676 | - return true; |
|
687 | + if ($v === $value) { |
|
688 | + return true; |
|
689 | + } |
|
677 | 690 | } |
678 | 691 | return false; |
679 | 692 | } |
@@ -697,8 +710,9 @@ discard block |
||
697 | 710 | $set = [ ]; |
698 | 711 | foreach ($this as $k => $v) { |
699 | 712 | $key = $keySelector($v, $k); |
700 | - if (isset($set[$key])) |
|
701 | - continue; |
|
713 | + if (isset($set[$key])) { |
|
714 | + continue; |
|
715 | + } |
|
702 | 716 | $set[$key] = true; |
703 | 717 | yield $k => $v; |
704 | 718 | } |
@@ -730,8 +744,9 @@ discard block |
||
730 | 744 | } |
731 | 745 | foreach ($this as $k => $v) { |
732 | 746 | $key = $keySelector($v, $k); |
733 | - if (isset($set[$key])) |
|
734 | - continue; |
|
747 | + if (isset($set[$key])) { |
|
748 | + continue; |
|
749 | + } |
|
735 | 750 | $set[$key] = true; |
736 | 751 | yield $k => $v; |
737 | 752 | } |
@@ -763,8 +778,9 @@ discard block |
||
763 | 778 | } |
764 | 779 | foreach ($this as $k => $v) { |
765 | 780 | $key = $keySelector($v, $k); |
766 | - if (!isset($set[$key])) |
|
767 | - continue; |
|
781 | + if (!isset($set[$key])) { |
|
782 | + continue; |
|
783 | + } |
|
768 | 784 | unset($set[$key]); |
769 | 785 | yield $k => $v; |
770 | 786 | } |
@@ -793,15 +809,17 @@ discard block |
||
793 | 809 | $set = [ ]; |
794 | 810 | foreach ($this as $k => $v) { |
795 | 811 | $key = $keySelector($v, $k); |
796 | - if (isset($set[$key])) |
|
797 | - continue; |
|
812 | + if (isset($set[$key])) { |
|
813 | + continue; |
|
814 | + } |
|
798 | 815 | $set[$key] = true; |
799 | 816 | yield $k => $v; |
800 | 817 | } |
801 | 818 | foreach ($other as $k => $v) { |
802 | 819 | $key = $keySelector($v, $k); |
803 | - if (isset($set[$key])) |
|
804 | - continue; |
|
820 | + if (isset($set[$key])) { |
|
821 | + continue; |
|
822 | + } |
|
805 | 823 | $set[$key] = true; |
806 | 824 | yield $k => $v; |
807 | 825 | } |
@@ -825,12 +843,14 @@ discard block |
||
825 | 843 | { |
826 | 844 | /** @var $it \Iterator|\ArrayIterator */ |
827 | 845 | $it = $this->getIterator(); |
828 | - if ($it instanceof \ArrayIterator) |
|
829 | - return $it->getArrayCopy(); |
|
846 | + if ($it instanceof \ArrayIterator) { |
|
847 | + return $it->getArrayCopy(); |
|
848 | + } |
|
830 | 849 | |
831 | 850 | $array = [ ]; |
832 | - foreach ($it as $k => $v) |
|
833 | - $array[$k] = $v; |
|
851 | + foreach ($it as $k => $v) { |
|
852 | + $array[$k] = $v; |
|
853 | + } |
|
834 | 854 | return $array; |
835 | 855 | } |
836 | 856 | |
@@ -857,8 +877,9 @@ discard block |
||
857 | 877 | protected function toArrayDeepProc ($enum) |
858 | 878 | { |
859 | 879 | $array = [ ]; |
860 | - foreach ($enum as $k => $v) |
|
861 | - $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
|
880 | + foreach ($enum as $k => $v) { |
|
881 | + $array[$k] = $v instanceof \Traversable || is_array($v) ? $this->toArrayDeepProc($v) : $v; |
|
882 | + } |
|
862 | 883 | return $array; |
863 | 884 | } |
864 | 885 | |
@@ -875,12 +896,14 @@ discard block |
||
875 | 896 | { |
876 | 897 | /** @var $it \Iterator|\ArrayIterator */ |
877 | 898 | $it = $this->getIterator(); |
878 | - if ($it instanceof \ArrayIterator) |
|
879 | - return array_values($it->getArrayCopy()); |
|
899 | + if ($it instanceof \ArrayIterator) { |
|
900 | + return array_values($it->getArrayCopy()); |
|
901 | + } |
|
880 | 902 | |
881 | 903 | $array = [ ]; |
882 | - foreach ($it as $v) |
|
883 | - $array[] = $v; |
|
904 | + foreach ($it as $v) { |
|
905 | + $array[] = $v; |
|
906 | + } |
|
884 | 907 | return $array; |
885 | 908 | } |
886 | 909 | |
@@ -907,8 +930,9 @@ discard block |
||
907 | 930 | protected function toListDeepProc ($enum) |
908 | 931 | { |
909 | 932 | $array = [ ]; |
910 | - foreach ($enum as $v) |
|
911 | - $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
|
933 | + foreach ($enum as $v) { |
|
934 | + $array[] = $v instanceof \Traversable || is_array($v) ? $this->toListDeepProc($v) : $v; |
|
935 | + } |
|
912 | 936 | return $array; |
913 | 937 | } |
914 | 938 | |
@@ -927,8 +951,9 @@ discard block |
||
927 | 951 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
928 | 952 | |
929 | 953 | $dic = [ ]; |
930 | - foreach ($this as $k => $v) |
|
931 | - $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
|
954 | + foreach ($this as $k => $v) { |
|
955 | + $dic[$keySelector($v, $k)] = $valueSelector($v, $k); |
|
956 | + } |
|
932 | 957 | return $dic; |
933 | 958 | } |
934 | 959 | |
@@ -961,8 +986,9 @@ discard block |
||
961 | 986 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
962 | 987 | |
963 | 988 | $lookup = [ ]; |
964 | - foreach ($this as $k => $v) |
|
965 | - $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
|
989 | + foreach ($this as $k => $v) { |
|
990 | + $lookup[$keySelector($v, $k)][] = $valueSelector($v, $k); |
|
991 | + } |
|
966 | 992 | return $lookup; |
967 | 993 | } |
968 | 994 | |
@@ -1004,8 +1030,9 @@ discard block |
||
1004 | 1030 | $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value); |
1005 | 1031 | |
1006 | 1032 | $obj = new \stdClass(); |
1007 | - foreach ($this as $k => $v) |
|
1008 | - $obj->{$propertySelector($v, $k)} = $valueSelector($v, $k); |
|
1033 | + foreach ($this as $k => $v) { |
|
1034 | + $obj->{$propertySelector($v, $k)} = $valueSelector($v, $k); |
|
1035 | + } |
|
1009 | 1036 | return $obj; |
1010 | 1037 | } |
1011 | 1038 | |
@@ -1062,8 +1089,9 @@ discard block |
||
1062 | 1089 | { |
1063 | 1090 | $action = Utils::createLambda($action, 'v,k', Functions::$blank); |
1064 | 1091 | |
1065 | - foreach ($this as $k => $v) |
|
1066 | - $action($v, $k); |
|
1092 | + foreach ($this as $k => $v) { |
|
1093 | + $action($v, $k); |
|
1094 | + } |
|
1067 | 1095 | } |
1068 | 1096 | |
1069 | 1097 | /** |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | * <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. |
386 | 386 | * <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. |
387 | 387 | * @param int $count The number of elements to skip before returning the remaining elements. |
388 | - * @return Enumerable A sequence that contains the elements that occur after the specified index in the input sequence. |
|
388 | + * @return EnumerablePagination A sequence that contains the elements that occur after the specified index in the input sequence. |
|
389 | 389 | * @package YaLinqo\Pagination |
390 | 390 | */ |
391 | 391 | public function skip ($count) |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | * <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. |
409 | 409 | * <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. |
410 | 410 | * @param callable $predicate {(v, k) ==> result} A function to test each element for a condition. |
411 | - * @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. |
|
411 | + * @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. |
|
412 | 412 | * @package YaLinqo\Pagination |
413 | 413 | */ |
414 | 414 | public function skipWhile ($predicate) |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | * <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. |
433 | 433 | * <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. |
434 | 434 | * @param int $count The number of elements to return. |
435 | - * @return Enumerable A sequence that contains the specified number of elements from the start of the input sequence. |
|
435 | + * @return EnumerablePagination A sequence that contains the specified number of elements from the start of the input sequence. |
|
436 | 436 | * @package YaLinqo\Pagination |
437 | 437 | */ |
438 | 438 | public function take ($count) |
@@ -457,7 +457,7 @@ discard block |
||
457 | 457 | * <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. |
458 | 458 | * <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. |
459 | 459 | * @param callable $predicate {(v, k) ==> result} A function to test each element for a condition. |
460 | - * @return Enumerable A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes. |
|
460 | + * @return EnumerablePagination A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes. |
|
461 | 461 | * @package YaLinqo\Pagination |
462 | 462 | */ |
463 | 463 | public function takeWhile ($predicate) |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * @return mixed The value at the key in the source sequence. |
18 | 18 | * @package YaLinqo\Pagination |
19 | 19 | */ |
20 | - public function elementAt ($key) |
|
20 | + public function elementAt($key) |
|
21 | 21 | { |
22 | 22 | /** @var $it \Iterator|\ArrayAccess */ |
23 | 23 | $it = $this->getIterator(); |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | * @return mixed default value if the key is not found in the source sequence; otherwise, the value at the specified key in the source sequence. |
45 | 45 | * @package YaLinqo\Pagination |
46 | 46 | */ |
47 | - public function elementAtOrDefault ($key, $default = null) |
|
47 | + public function elementAtOrDefault($key, $default = null) |
|
48 | 48 | { |
49 | 49 | /** @var $it \Iterator|\ArrayAccess */ |
50 | 50 | $it = $this->getIterator(); |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * @return mixed If predicate is null: the first element in the specified sequence. If predicate is not null: The first element in the sequence that passes the test in the specified predicate function. |
73 | 73 | * @package YaLinqo\Pagination |
74 | 74 | */ |
75 | - public function first ($predicate = null) |
|
75 | + public function first($predicate = null) |
|
76 | 76 | { |
77 | 77 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
78 | 78 | |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @return mixed If predicate is null: default value if source is empty; otherwise, the first element in source. If predicate is not null: default value if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate. |
96 | 96 | * @package YaLinqo\Pagination |
97 | 97 | */ |
98 | - public function firstOrDefault ($default = null, $predicate = null) |
|
98 | + public function firstOrDefault($default = null, $predicate = null) |
|
99 | 99 | { |
100 | 100 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
101 | 101 | |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @return mixed If predicate is null: the result of calling a fallback function if source is empty; otherwise, the first element in source. If predicate is not null: the result of calling a fallback function if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate. |
119 | 119 | * @package YaLinqo\Pagination |
120 | 120 | */ |
121 | - public function firstOrFallback ($fallback, $predicate = null) |
|
121 | + public function firstOrFallback($fallback, $predicate = null) |
|
122 | 122 | { |
123 | 123 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
124 | 124 | |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | * @return mixed If predicate is null: the last element in the specified sequence. If predicate is not null: The last element in the sequence that passes the test in the specified predicate function. |
143 | 143 | * @package YaLinqo\Pagination |
144 | 144 | */ |
145 | - public function last ($predicate = null) |
|
145 | + public function last($predicate = null) |
|
146 | 146 | { |
147 | 147 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
148 | 148 | |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | * @return mixed If predicate is null: default value if source is empty; otherwise, the last element in source. If predicate is not null: default value if source is empty or if no element passes the test specified by predicate; otherwise, the last element in source that passes the test specified by predicate. |
172 | 172 | * @package YaLinqo\Pagination |
173 | 173 | */ |
174 | - public function lastOrDefault ($default = null, $predicate = null) |
|
174 | + public function lastOrDefault($default = null, $predicate = null) |
|
175 | 175 | { |
176 | 176 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
177 | 177 | |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | * @return mixed If predicate is null: the result of calling a fallback function if source is empty; otherwise, the last element in source. If predicate is not null: the result of calling a fallback function if source is empty or if no element passes the test specified by predicate; otherwise, the last element in source that passes the test specified by predicate. |
199 | 199 | * @package YaLinqo\Pagination |
200 | 200 | */ |
201 | - public function lastOrFallback ($fallback, $predicate = null) |
|
201 | + public function lastOrFallback($fallback, $predicate = null) |
|
202 | 202 | { |
203 | 203 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
204 | 204 | |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | * @return mixed If predicate is null: the single element of the input sequence. If predicate is not null: The single element of the sequence that passes the test in the specified predicate function. |
227 | 227 | * @package YaLinqo\Pagination |
228 | 228 | */ |
229 | - public function single ($predicate = null) |
|
229 | + public function single($predicate = null) |
|
230 | 230 | { |
231 | 231 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
232 | 232 | |
@@ -258,7 +258,7 @@ discard block |
||
258 | 258 | * @return mixed If predicate is null: default value if source is empty; otherwise, the single element of the source. If predicate is not null: default value if source is empty or if no element passes the test specified by predicate; otherwise, the single element of the source that passes the test specified by predicate. |
259 | 259 | * @package YaLinqo\Pagination |
260 | 260 | */ |
261 | - public function singleOrDefault ($default = null, $predicate = null) |
|
261 | + public function singleOrDefault($default = null, $predicate = null) |
|
262 | 262 | { |
263 | 263 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
264 | 264 | |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | * @return mixed If predicate is null: the result of calling a fallback function if source is empty; otherwise, the single element of the source. If predicate is not null: the result of calling a fallback function if source is empty or if no element passes the test specified by predicate; otherwise, the single element of the source that passes the test specified by predicate. |
289 | 289 | * @package YaLinqo\Pagination |
290 | 290 | */ |
291 | - public function singleOrFallback ($fallback, $predicate = null) |
|
291 | + public function singleOrFallback($fallback, $predicate = null) |
|
292 | 292 | { |
293 | 293 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
294 | 294 | |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | * @return mixed The key of the first occurrence of value, if found; otherwise, null. |
314 | 314 | * @package YaLinqo\Pagination |
315 | 315 | */ |
316 | - public function indexOf ($value) |
|
316 | + public function indexOf($value) |
|
317 | 317 | { |
318 | 318 | foreach ($this as $k => $v) { |
319 | 319 | if ($v === $value) |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | * @return mixed The key of the last occurrence of value, if found; otherwise, null. |
331 | 331 | * @package YaLinqo\Pagination |
332 | 332 | */ |
333 | - public function lastIndexOf ($value) |
|
333 | + public function lastIndexOf($value) |
|
334 | 334 | { |
335 | 335 | $key = null; |
336 | 336 | foreach ($this as $k => $v) { |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | * @return mixed The key of the first occurrence of an element that matches the conditions defined by predicate, if found; otherwise, null. |
349 | 349 | * @package YaLinqo\Pagination |
350 | 350 | */ |
351 | - public function findIndex ($predicate) |
|
351 | + public function findIndex($predicate) |
|
352 | 352 | { |
353 | 353 | $predicate = Utils::createLambda($predicate, 'v,k'); |
354 | 354 | |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | * @return mixed The key of the last occurrence of an element that matches the conditions defined by predicate, if found; otherwise, null. |
368 | 368 | * @package YaLinqo\Pagination |
369 | 369 | */ |
370 | - public function findLastIndex ($predicate) |
|
370 | + public function findLastIndex($predicate) |
|
371 | 371 | { |
372 | 372 | $predicate = Utils::createLambda($predicate, 'v,k'); |
373 | 373 | |
@@ -388,9 +388,9 @@ discard block |
||
388 | 388 | * @return Enumerable A sequence that contains the elements that occur after the specified index in the input sequence. |
389 | 389 | * @package YaLinqo\Pagination |
390 | 390 | */ |
391 | - public function skip ($count) |
|
391 | + public function skip($count) |
|
392 | 392 | { |
393 | - return new self(function () use ($count) { |
|
393 | + return new self(function() use ($count) { |
|
394 | 394 | $it = $this->getIterator(); |
395 | 395 | $it->rewind(); |
396 | 396 | for ($i = 0; $i < $count && $it->valid(); ++$i) |
@@ -411,11 +411,11 @@ discard block |
||
411 | 411 | * @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. |
412 | 412 | * @package YaLinqo\Pagination |
413 | 413 | */ |
414 | - public function skipWhile ($predicate) |
|
414 | + public function skipWhile($predicate) |
|
415 | 415 | { |
416 | 416 | $predicate = Utils::createLambda($predicate, 'v,k'); |
417 | 417 | |
418 | - return new self(function () use ($predicate) { |
|
418 | + return new self(function() use ($predicate) { |
|
419 | 419 | $yielding = false; |
420 | 420 | foreach ($this as $k => $v) { |
421 | 421 | if (!$yielding && !$predicate($v, $k)) |
@@ -435,12 +435,12 @@ discard block |
||
435 | 435 | * @return Enumerable A sequence that contains the specified number of elements from the start of the input sequence. |
436 | 436 | * @package YaLinqo\Pagination |
437 | 437 | */ |
438 | - public function take ($count) |
|
438 | + public function take($count) |
|
439 | 439 | { |
440 | 440 | if ($count <= 0) |
441 | 441 | return new self(new \EmptyIterator, false); |
442 | 442 | |
443 | - return new self(function () use ($count) { |
|
443 | + return new self(function() use ($count) { |
|
444 | 444 | if ($count <= 0) |
445 | 445 | return; |
446 | 446 | foreach ($this as $k => $v) { |
@@ -460,11 +460,11 @@ discard block |
||
460 | 460 | * @return Enumerable A sequence that contains the elements from the input sequence that occur before the element at which the test no longer passes. |
461 | 461 | * @package YaLinqo\Pagination |
462 | 462 | */ |
463 | - public function takeWhile ($predicate) |
|
463 | + public function takeWhile($predicate) |
|
464 | 464 | { |
465 | 465 | $predicate = Utils::createLambda($predicate, 'v,k'); |
466 | 466 | |
467 | - return new self(function () use ($predicate) { |
|
467 | + return new self(function() use ($predicate) { |
|
468 | 468 | foreach ($this as $k => $v) { |
469 | 469 | if (!$predicate($v, $k)) |
470 | 470 | break; |
@@ -476,5 +476,5 @@ discard block |
||
476 | 476 | /** |
477 | 477 | * @return \Iterator |
478 | 478 | */ |
479 | - public abstract function getIterator (); |
|
479 | + public abstract function getIterator(); |
|
480 | 480 | } |
481 | 481 | \ No newline at end of file |
@@ -23,14 +23,16 @@ discard block |
||
23 | 23 | $it = $this->getIterator(); |
24 | 24 | |
25 | 25 | if ($it instanceof \ArrayAccess) { |
26 | - if (!$it->offsetExists($key)) |
|
27 | - throw new \UnexpectedValueException(Errors::NO_KEY); |
|
26 | + if (!$it->offsetExists($key)) { |
|
27 | + throw new \UnexpectedValueException(Errors::NO_KEY); |
|
28 | + } |
|
28 | 29 | return $it->offsetGet($key); |
29 | 30 | } |
30 | 31 | |
31 | 32 | foreach ($it as $k => $v) { |
32 | - if ($k === $key) |
|
33 | - return $v; |
|
33 | + if ($k === $key) { |
|
34 | + return $v; |
|
35 | + } |
|
34 | 36 | } |
35 | 37 | throw new \UnexpectedValueException(Errors::NO_KEY); |
36 | 38 | } |
@@ -49,12 +51,14 @@ discard block |
||
49 | 51 | /** @var $it \Iterator|\ArrayAccess */ |
50 | 52 | $it = $this->getIterator(); |
51 | 53 | |
52 | - if ($it instanceof \ArrayAccess) |
|
53 | - return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
|
54 | + if ($it instanceof \ArrayAccess) { |
|
55 | + return $it->offsetExists($key) ? $it->offsetGet($key) : $default; |
|
56 | + } |
|
54 | 57 | |
55 | 58 | foreach ($it as $k => $v) { |
56 | - if ($k === $key) |
|
57 | - return $v; |
|
59 | + if ($k === $key) { |
|
60 | + return $v; |
|
61 | + } |
|
58 | 62 | } |
59 | 63 | return $default; |
60 | 64 | } |
@@ -77,8 +81,9 @@ discard block |
||
77 | 81 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
78 | 82 | |
79 | 83 | foreach ($this as $k => $v) { |
80 | - if ($predicate($v, $k)) |
|
81 | - return $v; |
|
84 | + if ($predicate($v, $k)) { |
|
85 | + return $v; |
|
86 | + } |
|
82 | 87 | } |
83 | 88 | throw new \UnexpectedValueException(Errors::NO_MATCHES); |
84 | 89 | } |
@@ -100,8 +105,9 @@ discard block |
||
100 | 105 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
101 | 106 | |
102 | 107 | foreach ($this as $k => $v) { |
103 | - if ($predicate($v, $k)) |
|
104 | - return $v; |
|
108 | + if ($predicate($v, $k)) { |
|
109 | + return $v; |
|
110 | + } |
|
105 | 111 | } |
106 | 112 | return $default; |
107 | 113 | } |
@@ -123,8 +129,9 @@ discard block |
||
123 | 129 | $predicate = Utils::createLambda($predicate, 'v,k', Functions::$true); |
124 | 130 | |
125 | 131 | foreach ($this as $k => $v) { |
126 | - if ($predicate($v, $k)) |
|
127 | - return $v; |
|
132 | + if ($predicate($v, $k)) { |
|
133 | + return $v; |
|
134 | + } |
|
128 | 135 | } |
129 | 136 | return $fallback(); |
130 | 137 | } |
@@ -154,8 +161,9 @@ discard block |
||
154 | 161 | $value = $v; |
155 | 162 | } |
156 | 163 | } |
157 | - if (!$found) |
|
158 | - throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
164 | + if (!$found) { |
|
165 | + throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
166 | + } |
|
159 | 167 | return $value; |
160 | 168 | } |
161 | 169 | |
@@ -234,14 +242,16 @@ discard block |
||
234 | 242 | $value = null; |
235 | 243 | foreach ($this as $k => $v) { |
236 | 244 | if ($predicate($v, $k)) { |
237 | - if ($found) |
|
238 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
245 | + if ($found) { |
|
246 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
247 | + } |
|
239 | 248 | $found = true; |
240 | 249 | $value = $v; |
241 | 250 | } |
242 | 251 | } |
243 | - if (!$found) |
|
244 | - throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
252 | + if (!$found) { |
|
253 | + throw new \UnexpectedValueException(Errors::NO_MATCHES); |
|
254 | + } |
|
245 | 255 | return $value; |
246 | 256 | } |
247 | 257 | |
@@ -266,8 +276,9 @@ discard block |
||
266 | 276 | $value = null; |
267 | 277 | foreach ($this as $k => $v) { |
268 | 278 | if ($predicate($v, $k)) { |
269 | - if ($found) |
|
270 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
279 | + if ($found) { |
|
280 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
281 | + } |
|
271 | 282 | $found = true; |
272 | 283 | $value = $v; |
273 | 284 | } |
@@ -296,8 +307,9 @@ discard block |
||
296 | 307 | $value = null; |
297 | 308 | foreach ($this as $k => $v) { |
298 | 309 | if ($predicate($v, $k)) { |
299 | - if ($found) |
|
300 | - throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
310 | + if ($found) { |
|
311 | + throw new \UnexpectedValueException(Errors::MANY_MATCHES); |
|
312 | + } |
|
301 | 313 | $found = true; |
302 | 314 | $value = $v; |
303 | 315 | } |
@@ -316,8 +328,9 @@ discard block |
||
316 | 328 | public function indexOf ($value) |
317 | 329 | { |
318 | 330 | foreach ($this as $k => $v) { |
319 | - if ($v === $value) |
|
320 | - return $k; |
|
331 | + if ($v === $value) { |
|
332 | + return $k; |
|
333 | + } |
|
321 | 334 | } |
322 | 335 | return null; // not -1 |
323 | 336 | } |
@@ -334,8 +347,9 @@ discard block |
||
334 | 347 | { |
335 | 348 | $key = null; |
336 | 349 | foreach ($this as $k => $v) { |
337 | - if ($v === $value) |
|
338 | - $key = $k; |
|
350 | + if ($v === $value) { |
|
351 | + $key = $k; |
|
352 | + } |
|
339 | 353 | } |
340 | 354 | return $key; // not -1 |
341 | 355 | } |
@@ -353,8 +367,9 @@ discard block |
||
353 | 367 | $predicate = Utils::createLambda($predicate, 'v,k'); |
354 | 368 | |
355 | 369 | foreach ($this as $k => $v) { |
356 | - if ($predicate($v, $k)) |
|
357 | - return $k; |
|
370 | + if ($predicate($v, $k)) { |
|
371 | + return $k; |
|
372 | + } |
|
358 | 373 | } |
359 | 374 | return null; // not -1 |
360 | 375 | } |
@@ -373,8 +388,9 @@ discard block |
||
373 | 388 | |
374 | 389 | $key = null; |
375 | 390 | foreach ($this as $k => $v) { |
376 | - if ($predicate($v, $k)) |
|
377 | - $key = $k; |
|
391 | + if ($predicate($v, $k)) { |
|
392 | + $key = $k; |
|
393 | + } |
|
378 | 394 | } |
379 | 395 | return $key; // not -1 |
380 | 396 | } |
@@ -393,8 +409,9 @@ discard block |
||
393 | 409 | return new self(function () use ($count) { |
394 | 410 | $it = $this->getIterator(); |
395 | 411 | $it->rewind(); |
396 | - for ($i = 0; $i < $count && $it->valid(); ++$i) |
|
397 | - $it->next(); |
|
412 | + for ($i = 0; $i < $count && $it->valid(); ++$i) { |
|
413 | + $it->next(); |
|
414 | + } |
|
398 | 415 | while ($it->valid()) { |
399 | 416 | yield $it->key() => $it->current(); |
400 | 417 | $it->next(); |
@@ -418,10 +435,12 @@ discard block |
||
418 | 435 | return new self(function () use ($predicate) { |
419 | 436 | $yielding = false; |
420 | 437 | foreach ($this as $k => $v) { |
421 | - if (!$yielding && !$predicate($v, $k)) |
|
422 | - $yielding = true; |
|
423 | - if ($yielding) |
|
424 | - yield $k => $v; |
|
438 | + if (!$yielding && !$predicate($v, $k)) { |
|
439 | + $yielding = true; |
|
440 | + } |
|
441 | + if ($yielding) { |
|
442 | + yield $k => $v; |
|
443 | + } |
|
425 | 444 | } |
426 | 445 | }); |
427 | 446 | } |
@@ -437,16 +456,19 @@ discard block |
||
437 | 456 | */ |
438 | 457 | public function take ($count) |
439 | 458 | { |
440 | - if ($count <= 0) |
|
441 | - return new self(new \EmptyIterator, false); |
|
459 | + if ($count <= 0) { |
|
460 | + return new self(new \EmptyIterator, false); |
|
461 | + } |
|
442 | 462 | |
443 | 463 | return new self(function () use ($count) { |
444 | - if ($count <= 0) |
|
445 | - return; |
|
464 | + if ($count <= 0) { |
|
465 | + return; |
|
466 | + } |
|
446 | 467 | foreach ($this as $k => $v) { |
447 | 468 | yield $k => $v; |
448 | - if (--$count == 0) |
|
449 | - break; |
|
469 | + if (--$count == 0) { |
|
470 | + break; |
|
471 | + } |
|
450 | 472 | } |
451 | 473 | }); |
452 | 474 | } |
@@ -466,8 +488,9 @@ discard block |
||
466 | 488 | |
467 | 489 | return new self(function () use ($predicate) { |
468 | 490 | foreach ($this as $k => $v) { |
469 | - if (!$predicate($v, $k)) |
|
470 | - break; |
|
491 | + if (!$predicate($v, $k)) { |
|
492 | + break; |
|
493 | + } |
|
471 | 494 | yield $k => $v; |
472 | 495 | } |
473 | 496 | }); |
@@ -142,7 +142,7 @@ |
||
142 | 142 | |
143 | 143 | /** |
144 | 144 | * Increment function: returns incremental integers starting from 0. |
145 | - * @return callable |
|
145 | + * @return \Closure |
|
146 | 146 | */ |
147 | 147 | public static function increment () |
148 | 148 | { |
@@ -79,23 +79,23 @@ discard block |
||
79 | 79 | public static $compareIntReversed; |
80 | 80 | |
81 | 81 | /** @internal */ |
82 | - public static function init () |
|
82 | + public static function init() |
|
83 | 83 | { |
84 | - self::$identity = function ($x) { return $x; }; |
|
84 | + self::$identity = function($x) { return $x; }; |
|
85 | 85 | |
86 | 86 | /** @noinspection PhpUnusedParameterInspection */ |
87 | - self::$key = function ($v, $k) { return $k; }; |
|
87 | + self::$key = function($v, $k) { return $k; }; |
|
88 | 88 | |
89 | 89 | /** @noinspection PhpUnusedParameterInspection */ |
90 | - self::$value = function ($v, $k) { return $v; }; |
|
90 | + self::$value = function($v, $k) { return $v; }; |
|
91 | 91 | |
92 | - self::$true = function () { return true; }; |
|
92 | + self::$true = function() { return true; }; |
|
93 | 93 | |
94 | - self::$false = function () { return false; }; |
|
94 | + self::$false = function() { return false; }; |
|
95 | 95 | |
96 | - self::$blank = function () { }; |
|
96 | + self::$blank = function() { }; |
|
97 | 97 | |
98 | - self::$compareStrict = function ($a, $b) { |
|
98 | + self::$compareStrict = function($a, $b) { |
|
99 | 99 | if ($a === $b) |
100 | 100 | return 0; |
101 | 101 | elseif ($a > $b) |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | return -1; |
105 | 105 | }; |
106 | 106 | |
107 | - self::$compareStrictReversed = function ($a, $b) { |
|
107 | + self::$compareStrictReversed = function($a, $b) { |
|
108 | 108 | if ($a === $b) |
109 | 109 | return 0; |
110 | 110 | elseif ($a > $b) |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | return 1; |
114 | 114 | }; |
115 | 115 | |
116 | - self::$compareLoose = function ($a, $b) { |
|
116 | + self::$compareLoose = function($a, $b) { |
|
117 | 117 | if ($a == $b) |
118 | 118 | return 0; |
119 | 119 | elseif ($a > $b) |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | return -1; |
123 | 123 | }; |
124 | 124 | |
125 | - self::$compareLooseReversed = function ($a, $b) { |
|
125 | + self::$compareLooseReversed = function($a, $b) { |
|
126 | 126 | if ($a == $b) |
127 | 127 | return 0; |
128 | 128 | elseif ($a > $b) |
@@ -131,11 +131,11 @@ discard block |
||
131 | 131 | return 1; |
132 | 132 | }; |
133 | 133 | |
134 | - self::$compareInt = function ($a, $b) { |
|
134 | + self::$compareInt = function($a, $b) { |
|
135 | 135 | return $a - $b; |
136 | 136 | }; |
137 | 137 | |
138 | - self::$compareIntReversed = function ($a, $b) { |
|
138 | + self::$compareIntReversed = function($a, $b) { |
|
139 | 139 | return $b - $a; |
140 | 140 | }; |
141 | 141 | } |
@@ -144,9 +144,9 @@ discard block |
||
144 | 144 | * Increment function: returns incremental integers starting from 0. |
145 | 145 | * @return callable |
146 | 146 | */ |
147 | - public static function increment () |
|
147 | + public static function increment() |
|
148 | 148 | { |
149 | 149 | $i = 0; |
150 | - return function () use (&$i) { return $i++; }; |
|
150 | + return function() use (&$i) { return $i++; }; |
|
151 | 151 | } |
152 | 152 | } |
@@ -96,39 +96,43 @@ |
||
96 | 96 | self::$blank = function () { }; |
97 | 97 | |
98 | 98 | self::$compareStrict = function ($a, $b) { |
99 | - if ($a === $b) |
|
100 | - return 0; |
|
101 | - elseif ($a > $b) |
|
102 | - return 1; |
|
103 | - else |
|
104 | - return -1; |
|
99 | + if ($a === $b) { |
|
100 | + return 0; |
|
101 | + } elseif ($a > $b) { |
|
102 | + return 1; |
|
103 | + } else { |
|
104 | + return -1; |
|
105 | + } |
|
105 | 106 | }; |
106 | 107 | |
107 | 108 | self::$compareStrictReversed = function ($a, $b) { |
108 | - if ($a === $b) |
|
109 | - return 0; |
|
110 | - elseif ($a > $b) |
|
111 | - return -1; |
|
112 | - else |
|
113 | - return 1; |
|
109 | + if ($a === $b) { |
|
110 | + return 0; |
|
111 | + } elseif ($a > $b) { |
|
112 | + return -1; |
|
113 | + } else { |
|
114 | + return 1; |
|
115 | + } |
|
114 | 116 | }; |
115 | 117 | |
116 | 118 | self::$compareLoose = function ($a, $b) { |
117 | - if ($a == $b) |
|
118 | - return 0; |
|
119 | - elseif ($a > $b) |
|
120 | - return 1; |
|
121 | - else |
|
122 | - return -1; |
|
119 | + if ($a == $b) { |
|
120 | + return 0; |
|
121 | + } elseif ($a > $b) { |
|
122 | + return 1; |
|
123 | + } else { |
|
124 | + return -1; |
|
125 | + } |
|
123 | 126 | }; |
124 | 127 | |
125 | 128 | self::$compareLooseReversed = function ($a, $b) { |
126 | - if ($a == $b) |
|
127 | - return 0; |
|
128 | - elseif ($a > $b) |
|
129 | - return -1; |
|
130 | - else |
|
131 | - return 1; |
|
129 | + if ($a == $b) { |
|
130 | + return 0; |
|
131 | + } elseif ($a > $b) { |
|
132 | + return -1; |
|
133 | + } else { |
|
134 | + return 1; |
|
135 | + } |
|
132 | 136 | }; |
133 | 137 | |
134 | 138 | self::$compareInt = function ($a, $b) { |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * <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. |
68 | 68 | * <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. |
69 | 69 | * <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. |
70 | - * @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). |
|
70 | + * @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). |
|
71 | 71 | * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from an element. Default: value. |
72 | 72 | * @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. |
73 | 73 | * @return \YaLinqo\OrderedEnumerable |
@@ -124,6 +124,9 @@ discard block |
||
124 | 124 | return $this->sortByMultipleFields($array, $canMultisort); |
125 | 125 | } |
126 | 126 | |
127 | + /** |
|
128 | + * @param boolean $canMultisort |
|
129 | + */ |
|
127 | 130 | private function trySortBySingleField ($array, $canMultisort) |
128 | 131 | { |
129 | 132 | if ($this->parent !== null || $array === null) { |
@@ -151,6 +154,9 @@ discard block |
||
151 | 154 | return new \ArrayIterator($array); |
152 | 155 | } |
153 | 156 | |
157 | + /** |
|
158 | + * @param boolean $canMultisort |
|
159 | + */ |
|
154 | 160 | private function sortByMultipleFields ($array, $canMultisort) |
155 | 161 | { |
156 | 162 | $orders = [ ]; |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | * @param callable $comparer {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b |
43 | 43 | * @param \YaLinqo\OrderedEnumerable $parent |
44 | 44 | */ |
45 | - public function __construct ($source, $sortOrder, $sortFlags, $isReversed, $keySelector, $comparer, $parent = null) |
|
45 | + public function __construct($source, $sortOrder, $sortFlags, $isReversed, $keySelector, $comparer, $parent = null) |
|
46 | 46 | { |
47 | 47 | $this->source = $source; |
48 | 48 | $this->sortOrder = $sortOrder; |
@@ -53,11 +53,11 @@ discard block |
||
53 | 53 | $this->parent = $parent; |
54 | 54 | } |
55 | 55 | |
56 | - private function getSingleComparer () |
|
56 | + private function getSingleComparer() |
|
57 | 57 | { |
58 | 58 | $comparer = $this->comparer; |
59 | 59 | if ($this->isReversed) |
60 | - $comparer = function ($a, $b) use ($comparer) { return -$comparer($a, $b); }; |
|
60 | + $comparer = function($a, $b) use ($comparer) { return -$comparer($a, $b); }; |
|
61 | 61 | return $comparer; |
62 | 62 | } |
63 | 63 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | * @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. |
73 | 73 | * @return \YaLinqo\OrderedEnumerable |
74 | 74 | */ |
75 | - public function thenByDir ($sortOrder, $keySelector = null, $comparer = null) |
|
75 | + public function thenByDir($sortOrder, $keySelector = null, $comparer = null) |
|
76 | 76 | { |
77 | 77 | $sortFlags = Utils::lambdaToSortFlagsAndOrder($comparer, $sortOrder); |
78 | 78 | $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value); |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @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. |
92 | 92 | * @return \YaLinqo\OrderedEnumerable |
93 | 93 | */ |
94 | - public function thenBy ($keySelector = null, $comparer = null) |
|
94 | + public function thenBy($keySelector = null, $comparer = null) |
|
95 | 95 | { |
96 | 96 | return $this->thenByDir(false, $keySelector, $comparer); |
97 | 97 | } |
@@ -106,13 +106,13 @@ discard block |
||
106 | 106 | * @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. |
107 | 107 | * @return \YaLinqo\OrderedEnumerable |
108 | 108 | */ |
109 | - public function thenByDescending ($keySelector = null, $comparer = null) |
|
109 | + public function thenByDescending($keySelector = null, $comparer = null) |
|
110 | 110 | { |
111 | 111 | return $this->thenByDir(true, $keySelector, $comparer); |
112 | 112 | } |
113 | 113 | |
114 | 114 | /** {@inheritdoc} */ |
115 | - public function getIterator () |
|
115 | + public function getIterator() |
|
116 | 116 | { |
117 | 117 | $canMultisort = $this->sortFlags !== null; |
118 | 118 | $array = $this->source->tryGetArrayCopy(); |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | return $this->sortByMultipleFields($array, $canMultisort); |
125 | 125 | } |
126 | 126 | |
127 | - private function trySortBySingleField ($array, $canMultisort) |
|
127 | + private function trySortBySingleField($array, $canMultisort) |
|
128 | 128 | { |
129 | 129 | if ($this->parent !== null || $array === null) { |
130 | 130 | return null; |
@@ -151,9 +151,9 @@ discard block |
||
151 | 151 | return new \ArrayIterator($array); |
152 | 152 | } |
153 | 153 | |
154 | - private function sortByMultipleFields ($array, $canMultisort) |
|
154 | + private function sortByMultipleFields($array, $canMultisort) |
|
155 | 155 | { |
156 | - $orders = [ ]; |
|
156 | + $orders = []; |
|
157 | 157 | for ($order = $this; $order !== null; $order = $order->parent) { |
158 | 158 | $orders[] = $order; |
159 | 159 | if ($order->sortFlags === null) |
@@ -168,9 +168,9 @@ discard block |
||
168 | 168 | return $this->sortIterator($orders, $canMultisort); |
169 | 169 | } |
170 | 170 | |
171 | - private function sortIterator ($orders, $canMultisort) |
|
171 | + private function sortIterator($orders, $canMultisort) |
|
172 | 172 | { |
173 | - $enum = [ ]; |
|
173 | + $enum = []; |
|
174 | 174 | if ($canMultisort) |
175 | 175 | $this->sortIteratorWithMultisort($enum, $orders); |
176 | 176 | else |
@@ -180,15 +180,15 @@ discard block |
||
180 | 180 | yield $pair[0] => $pair[1]; |
181 | 181 | } |
182 | 182 | |
183 | - private function trySortArrayWithMultisort ($array, $orders, $canMultisort) |
|
183 | + private function trySortArrayWithMultisort($array, $orders, $canMultisort) |
|
184 | 184 | { |
185 | 185 | /** @var $order OrderedEnumerable */ |
186 | 186 | if ($array === null || !$canMultisort) |
187 | 187 | return null; |
188 | 188 | |
189 | - $args = [ ]; |
|
189 | + $args = []; |
|
190 | 190 | foreach ($orders as $order) { |
191 | - $column = [ ]; |
|
191 | + $column = []; |
|
192 | 192 | foreach ($array as $k => $v) { |
193 | 193 | $keySelector = $order->keySelector; |
194 | 194 | $column[$k] = $keySelector($v, $k); |
@@ -204,15 +204,15 @@ discard block |
||
204 | 204 | return new \ArrayIterator($array); |
205 | 205 | } |
206 | 206 | |
207 | - private function sortIteratorWithMultisort (&$enum, $orders) |
|
207 | + private function sortIteratorWithMultisort(&$enum, $orders) |
|
208 | 208 | { |
209 | 209 | /** @var $order OrderedEnumerable */ |
210 | 210 | foreach ($this->source as $k => $v) |
211 | - $enum[] = [ $k, $v ]; |
|
211 | + $enum[] = [$k, $v]; |
|
212 | 212 | |
213 | - $args = [ ]; |
|
213 | + $args = []; |
|
214 | 214 | foreach ($orders as $order) { |
215 | - $column = [ ]; |
|
215 | + $column = []; |
|
216 | 216 | foreach ($enum as $k => $pair) { |
217 | 217 | $keySelector = $order->keySelector; |
218 | 218 | $column[$k] = $keySelector($pair[1], $pair[0]); |
@@ -226,11 +226,11 @@ discard block |
||
226 | 226 | call_user_func_array('array_multisort', $args); |
227 | 227 | } |
228 | 228 | |
229 | - private function sortIteratorWithUsort (&$enum, $orders) |
|
229 | + private function sortIteratorWithUsort(&$enum, $orders) |
|
230 | 230 | { |
231 | 231 | /** @var $order OrderedEnumerable */ |
232 | 232 | foreach ($this->source as $k => $v) { |
233 | - $element = [ $k, $v ]; |
|
233 | + $element = [$k, $v]; |
|
234 | 234 | foreach ($orders as $order) { |
235 | 235 | $keySelector = $order->keySelector; |
236 | 236 | $element[] = $keySelector($v, $k); |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | $enum[] = $element; |
239 | 239 | } |
240 | 240 | |
241 | - usort($enum, function ($a, $b) use ($orders) { |
|
241 | + usort($enum, function($a, $b) use ($orders) { |
|
242 | 242 | /** @var $order OrderedEnumerable */ |
243 | 243 | $count = count($orders); |
244 | 244 | for ($i = 0; $i < $count; $i++) { |
@@ -56,8 +56,10 @@ discard block |
||
56 | 56 | private function getSingleComparer () |
57 | 57 | { |
58 | 58 | $comparer = $this->comparer; |
59 | - if ($this->isReversed) |
|
60 | - $comparer = function ($a, $b) use ($comparer) { return -$comparer($a, $b); }; |
|
59 | + if ($this->isReversed) { |
|
60 | + $comparer = function ($a, $b) use ($comparer) { return -$comparer($a, $b); |
|
61 | + } |
|
62 | + }; |
|
61 | 63 | return $comparer; |
62 | 64 | } |
63 | 65 | |
@@ -118,8 +120,9 @@ discard block |
||
118 | 120 | $array = $this->source->tryGetArrayCopy(); |
119 | 121 | |
120 | 122 | $it = $this->trySortBySingleField($array, $canMultisort); |
121 | - if ($it !== null) |
|
122 | - return $it; |
|
123 | + if ($it !== null) { |
|
124 | + return $it; |
|
125 | + } |
|
123 | 126 | |
124 | 127 | return $this->sortByMultipleFields($array, $canMultisort); |
125 | 128 | } |
@@ -128,24 +131,23 @@ discard block |
||
128 | 131 | { |
129 | 132 | if ($this->parent !== null || $array === null) { |
130 | 133 | return null; |
131 | - } |
|
132 | - else if ($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 { |
|
134 | + } else if ($this->keySelector === Functions::$value) { |
|
135 | + if (!$canMultisort) { |
|
136 | + uasort($array, $this->getSingleComparer()); |
|
137 | + } elseif ($this->sortOrder == SORT_ASC) { |
|
138 | + asort($array, $this->sortFlags); |
|
139 | + } else { |
|
140 | + arsort($array, $this->sortFlags); |
|
141 | + } |
|
142 | + } elseif ($this->keySelector === Functions::$key) { |
|
143 | + if ($canMultisort) { |
|
144 | + uksort($array, $this->getSingleComparer()); |
|
145 | + } elseif ($this->sortOrder == SORT_ASC) { |
|
146 | + ksort($array, $this->sortFlags); |
|
147 | + } else { |
|
148 | + krsort($array, $this->sortFlags); |
|
149 | + } |
|
150 | + } else { |
|
149 | 151 | return null; |
150 | 152 | } |
151 | 153 | return new \ArrayIterator($array); |
@@ -156,14 +158,16 @@ discard block |
||
156 | 158 | $orders = [ ]; |
157 | 159 | for ($order = $this; $order !== null; $order = $order->parent) { |
158 | 160 | $orders[] = $order; |
159 | - if ($order->sortFlags === null) |
|
160 | - $canMultisort = false; |
|
161 | + if ($order->sortFlags === null) { |
|
162 | + $canMultisort = false; |
|
163 | + } |
|
161 | 164 | } |
162 | 165 | $orders = array_reverse($orders); |
163 | 166 | |
164 | 167 | $it = $this->trySortArrayWithMultisort($array, $orders, $canMultisort); |
165 | - if ($it !== null) |
|
166 | - return $it; |
|
168 | + if ($it !== null) { |
|
169 | + return $it; |
|
170 | + } |
|
167 | 171 | |
168 | 172 | return $this->sortIterator($orders, $canMultisort); |
169 | 173 | } |
@@ -171,20 +175,23 @@ discard block |
||
171 | 175 | private function sortIterator ($orders, $canMultisort) |
172 | 176 | { |
173 | 177 | $enum = [ ]; |
174 | - if ($canMultisort) |
|
175 | - $this->sortIteratorWithMultisort($enum, $orders); |
|
176 | - else |
|
177 | - $this->sortIteratorWithUsort($enum, $orders); |
|
178 | + if ($canMultisort) { |
|
179 | + $this->sortIteratorWithMultisort($enum, $orders); |
|
180 | + } else { |
|
181 | + $this->sortIteratorWithUsort($enum, $orders); |
|
182 | + } |
|
178 | 183 | |
179 | - foreach ($enum as $pair) |
|
180 | - yield $pair[0] => $pair[1]; |
|
184 | + foreach ($enum as $pair) { |
|
185 | + yield $pair[0] => $pair[1]; |
|
186 | + } |
|
181 | 187 | } |
182 | 188 | |
183 | 189 | private function trySortArrayWithMultisort ($array, $orders, $canMultisort) |
184 | 190 | { |
185 | 191 | /** @var $order OrderedEnumerable */ |
186 | - if ($array === null || !$canMultisort) |
|
187 | - return null; |
|
192 | + if ($array === null || !$canMultisort) { |
|
193 | + return null; |
|
194 | + } |
|
188 | 195 | |
189 | 196 | $args = [ ]; |
190 | 197 | foreach ($orders as $order) { |
@@ -207,8 +214,9 @@ discard block |
||
207 | 214 | private function sortIteratorWithMultisort (&$enum, $orders) |
208 | 215 | { |
209 | 216 | /** @var $order OrderedEnumerable */ |
210 | - foreach ($this->source as $k => $v) |
|
211 | - $enum[] = [ $k, $v ]; |
|
217 | + foreach ($this->source as $k => $v) { |
|
218 | + $enum[] = [ $k, $v ]; |
|
219 | + } |
|
212 | 220 | |
213 | 221 | $args = [ ]; |
214 | 222 | foreach ($orders as $order) { |
@@ -245,8 +253,9 @@ discard block |
||
245 | 253 | $order = $orders[$i]; |
246 | 254 | $comparer = $order->comparer; |
247 | 255 | $diff = $comparer($a[$i + 2], $b[$i + 2]); |
248 | - if ($diff != 0) |
|
249 | - return $order->isReversed ? -$diff : $diff; |
|
256 | + if ($diff != 0) { |
|
257 | + return $order->isReversed ? -$diff : $diff; |
|
258 | + } |
|
250 | 259 | } |
251 | 260 | return 0; |
252 | 261 | }); |
@@ -35,11 +35,11 @@ discard block |
||
35 | 35 | ]; |
36 | 36 | |
37 | 37 | /** @internal */ |
38 | - public static function init () |
|
38 | + public static function init() |
|
39 | 39 | { |
40 | 40 | self::$lambdaCache = [ |
41 | - '$v' => [ 'v,k' => Functions::$value ], |
|
42 | - '$k' => [ 'v,k' => Functions::$key ], |
|
41 | + '$v' => ['v,k' => Functions::$value], |
|
42 | + '$k' => ['v,k' => Functions::$key], |
|
43 | 43 | ]; |
44 | 44 | } |
45 | 45 | |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * @throws \InvalidArgumentException Incorrect lambda syntax. |
53 | 53 | * @return callable|null |
54 | 54 | */ |
55 | - public static function createLambda ($closure, $closureArgs, $default = null) |
|
55 | + public static function createLambda($closure, $closureArgs, $default = null) |
|
56 | 56 | { |
57 | 57 | if ($closure === null) { |
58 | 58 | if ($default === null) |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | * @throws \InvalidArgumentException Incorrect lambda syntax. |
78 | 78 | * @throws \InvalidArgumentException Incorrect SORT_ flags. |
79 | 79 | */ |
80 | - public static function createComparer ($closure, $sortOrder, &$isReversed) |
|
80 | + public static function createComparer($closure, $sortOrder, &$isReversed) |
|
81 | 81 | { |
82 | 82 | if ($closure === null) { |
83 | 83 | $isReversed = false; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | * @param int|bool $sortOrder |
114 | 114 | * @return callable|string|int|null |
115 | 115 | */ |
116 | - public static function lambdaToSortFlagsAndOrder ($closure, &$sortOrder) |
|
116 | + public static function lambdaToSortFlagsAndOrder($closure, &$sortOrder) |
|
117 | 117 | { |
118 | 118 | if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) |
119 | 119 | $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | * @throws \InvalidArgumentException Incorrect lambda syntax. |
133 | 133 | * @return string|null |
134 | 134 | */ |
135 | - private static function createLambdaFromString ($closure, $closureArgs) |
|
135 | + private static function createLambdaFromString($closure, $closureArgs) |
|
136 | 136 | { |
137 | 137 | $posDollar = strpos($closure, '$'); |
138 | 138 | if ($posDollar !== false) { |
@@ -55,16 +55,20 @@ discard block |
||
55 | 55 | public static function createLambda ($closure, $closureArgs, $default = null) |
56 | 56 | { |
57 | 57 | if ($closure === null) { |
58 | - if ($default === null) |
|
59 | - throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
58 | + if ($default === null) { |
|
59 | + throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL); |
|
60 | + } |
|
60 | 61 | return $default; |
61 | 62 | } |
62 | - if ($closure instanceof \Closure) |
|
63 | - return $closure; |
|
64 | - if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) |
|
65 | - return $function; |
|
66 | - if (is_callable($closure)) |
|
67 | - return $closure; |
|
63 | + if ($closure instanceof \Closure) { |
|
64 | + return $closure; |
|
65 | + } |
|
66 | + if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) { |
|
67 | + return $function; |
|
68 | + } |
|
69 | + if (is_callable($closure)) { |
|
70 | + return $closure; |
|
71 | + } |
|
68 | 72 | throw new \InvalidArgumentException(self::ERROR_CLOSURE_NOT_CALLABLE); |
69 | 73 | } |
70 | 74 | |
@@ -82,8 +86,7 @@ discard block |
||
82 | 86 | if ($closure === null) { |
83 | 87 | $isReversed = false; |
84 | 88 | return $sortOrder === SORT_DESC ? Functions::$compareStrictReversed : Functions::$compareStrict; |
85 | - } |
|
86 | - elseif (is_int($closure)) { |
|
89 | + } elseif (is_int($closure)) { |
|
87 | 90 | switch ($closure) { |
88 | 91 | case SORT_REGULAR: |
89 | 92 | return Functions::$compareStrict; |
@@ -115,14 +118,16 @@ discard block |
||
115 | 118 | */ |
116 | 119 | public static function lambdaToSortFlagsAndOrder ($closure, &$sortOrder) |
117 | 120 | { |
118 | - if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) |
|
119 | - $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
120 | - if (is_int($closure)) |
|
121 | - return $closure; |
|
122 | - elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) |
|
123 | - return self::$compareFunctionToSortFlags[$closure]; |
|
124 | - else |
|
125 | - return null; |
|
121 | + if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) { |
|
122 | + $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC; |
|
123 | + } |
|
124 | + if (is_int($closure)) { |
|
125 | + return $closure; |
|
126 | + } elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) { |
|
127 | + return self::$compareFunctionToSortFlags[$closure]; |
|
128 | + } else { |
|
129 | + return null; |
|
130 | + } |
|
126 | 131 | } |
127 | 132 | |
128 | 133 | /** |
@@ -136,23 +141,25 @@ discard block |
||
136 | 141 | { |
137 | 142 | $posDollar = strpos($closure, '$'); |
138 | 143 | if ($posDollar !== false) { |
139 | - if (isset(self::$lambdaCache[$closure][$closureArgs])) |
|
140 | - return self::$lambdaCache[$closure][$closureArgs]; |
|
144 | + if (isset(self::$lambdaCache[$closure][$closureArgs])) { |
|
145 | + return self::$lambdaCache[$closure][$closureArgs]; |
|
146 | + } |
|
141 | 147 | $posArrow = strpos($closure, '==>', $posDollar); |
142 | 148 | if ($posArrow !== false) { |
143 | 149 | $args = trim(substr($closure, 0, $posArrow), "() \r\n\t"); |
144 | 150 | $code = substr($closure, $posArrow + 3); |
145 | - } |
|
146 | - else { |
|
151 | + } else { |
|
147 | 152 | $args = '$' . str_replace(',', '=null,$', $closureArgs) . '=null'; |
148 | 153 | $code = $closure; |
149 | 154 | } |
150 | 155 | $code = trim($code, " \r\n\t"); |
151 | - if (strlen($code) > 0 && $code[0] != '{') |
|
152 | - $code = "return {$code};"; |
|
156 | + if (strlen($code) > 0 && $code[0] != '{') { |
|
157 | + $code = "return {$code};"; |
|
158 | + } |
|
153 | 159 | $fun = create_function($args, $code); |
154 | - if (!$fun) |
|
155 | - throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
160 | + if (!$fun) { |
|
161 | + throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA); |
|
162 | + } |
|
156 | 163 | self::$lambdaCache[$closure][$closureArgs] = $fun; |
157 | 164 | return $fun; |
158 | 165 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | * @return \YaLinqo\Enumerable |
19 | 19 | * @see \YaLinqo\Enumerable::from |
20 | 20 | */ |
21 | - function from ($source) |
|
21 | + function from($source) |
|
22 | 22 | { |
23 | 23 | return \YaLinqo\Enumerable::from($source); |
24 | 24 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * @param array|\Iterator|\IteratorAggregate|Enumerable $source Source sequence. |
14 | 14 | * @throws \InvalidArgumentException If source is not array or Traversible or Enumerable. |
15 | 15 | * @throws \UnexpectedValueException If source contains no elements (checked during enumeration). |
16 | - * @return Enumerable Endless list of items repeating the source sequence. |
|
16 | + * @return EnumerableGeneration Endless list of items repeating the source sequence. |
|
17 | 17 | * @package YaLinqo\Generation |
18 | 18 | */ |
19 | 19 | public static function cycle ($source) |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | /** |
37 | 37 | * Returns an empty sequence. |
38 | 38 | * <p><b>Syntax</b>: emptyEnum () |
39 | - * @return Enumerable |
|
39 | + * @return EnumerableGeneration |
|
40 | 40 | * @package YaLinqo\Generation |
41 | 41 | */ |
42 | 42 | public static function emptyEnum () |
@@ -79,6 +79,9 @@ discard block |
||
79 | 79 | throw new \InvalidArgumentException('source must be array or Traversable.'); |
80 | 80 | } |
81 | 81 | |
82 | + /** |
|
83 | + * @param \Traversable $source |
|
84 | + */ |
|
82 | 85 | private static function fromTraversable ($source) |
83 | 86 | { |
84 | 87 | foreach ($source as $k => $v) |
@@ -93,7 +96,7 @@ discard block |
||
93 | 96 | * @param mixed $seedValue Initial state of the generator loop for values. Default: null. |
94 | 97 | * @param callable|null $funcKey {(v, k) ==> key} State update function to run on key after every iteration of the generator loop. Default: increment. |
95 | 98 | * @param mixed $seedKey Initial state of the generator loop ofr keys. Default: 0. |
96 | - * @return Enumerable |
|
99 | + * @return EnumerableGeneration |
|
97 | 100 | * @package YaLinqo\Generation |
98 | 101 | */ |
99 | 102 | public static function generate ($funcValue, $seedValue = null, $funcKey = null, $seedKey = null) |
@@ -120,7 +123,7 @@ discard block |
||
120 | 123 | * <p><b>Syntax</b>: toInfinity ([start [, step]]) |
121 | 124 | * @param int $start The first integer in the sequence. Default: 0. |
122 | 125 | * @param int $step The difference between adjacent integers. Default: 1. |
123 | - * @return Enumerable |
|
126 | + * @return EnumerableGeneration |
|
124 | 127 | * @package YaLinqo\Generation |
125 | 128 | */ |
126 | 129 | public static function toInfinity ($start = 0, $step = 1) |
@@ -138,7 +141,7 @@ discard block |
||
138 | 141 | * @param string $subject The input string. |
139 | 142 | * @param string $pattern The pattern to search for, as a string. |
140 | 143 | * @param int $flags Can be a combination of the following flags: PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE. Default: PREG_SET_ORDER. |
141 | - * @return Enumerable |
|
144 | + * @return EnumerableGeneration |
|
142 | 145 | * @see preg_match_all |
143 | 146 | * @package YaLinqo\Generation |
144 | 147 | */ |
@@ -155,7 +158,7 @@ discard block |
||
155 | 158 | * <p><b>Syntax</b>: toNegativeInfinity ([start [, step]]) |
156 | 159 | * @param int $start The first integer in the sequence. Default: 0. |
157 | 160 | * @param int $step The difference between adjacent integers. Default: 1. |
158 | - * @return Enumerable |
|
161 | + * @return EnumerableGeneration |
|
159 | 162 | * @package YaLinqo\Generation |
160 | 163 | */ |
161 | 164 | public static function toNegativeInfinity ($start = 0, $step = 1) |
@@ -167,7 +170,7 @@ discard block |
||
167 | 170 | * Returns a sequence that contains a single element with a specified value. |
168 | 171 | * <p><b>Syntax</b>: returnEnum (element) |
169 | 172 | * @param mixed $element The single element in the resulting sequence. |
170 | - * @return Enumerable Observable sequence containing the single specified element. |
|
173 | + * @return EnumerableGeneration Observable sequence containing the single specified element. |
|
171 | 174 | * @package YaLinqo\Generation |
172 | 175 | */ |
173 | 176 | public static function returnEnum ($element) |
@@ -183,7 +186,7 @@ discard block |
||
183 | 186 | * @param int $start The value of the first integer in the sequence. |
184 | 187 | * @param int $count The number of integers to generate. |
185 | 188 | * @param int $step The difference between adjacent integers. Default: 1. |
186 | - * @return Enumerable A sequence that contains a range of integral numbers. |
|
189 | + * @return EnumerableGeneration A sequence that contains a range of integral numbers. |
|
187 | 190 | * @package YaLinqo\Generation |
188 | 191 | */ |
189 | 192 | public static function range ($start, $count, $step = 1) |
@@ -205,7 +208,7 @@ discard block |
||
205 | 208 | * @param int $start The value of the first integer in the sequence. |
206 | 209 | * @param int $count The number of integers to generate. |
207 | 210 | * @param int $step The difference between adjacent integers. Default: 1. |
208 | - * @return Enumerable A sequence that contains a range of integral numbers. |
|
211 | + * @return EnumerableGeneration A sequence that contains a range of integral numbers. |
|
209 | 212 | * @package YaLinqo\Generation |
210 | 213 | */ |
211 | 214 | public static function rangeDown ($start, $count, $step = 1) |
@@ -222,7 +225,7 @@ discard block |
||
222 | 225 | * @param int $end The value of the last integer in the sequence (not included). |
223 | 226 | * @param int $step The difference between adjacent integers. Default: 1. |
224 | 227 | * @throws \InvalidArgumentException If step is not a positive number. |
225 | - * @return Enumerable A sequence that contains a range of integral numbers. |
|
228 | + * @return EnumerableGeneration A sequence that contains a range of integral numbers. |
|
226 | 229 | * @package YaLinqo\Generation |
227 | 230 | */ |
228 | 231 | public static function rangeTo ($start, $end, $step = 1) |
@@ -251,7 +254,7 @@ discard block |
||
251 | 254 | * @param int $element The value to be repeated. |
252 | 255 | * @param int $count The number of times to repeat the value in the generated sequence. Default: null. |
253 | 256 | * @throws \InvalidArgumentException If count is less than 0. |
254 | - * @return Enumerable A sequence that contains a repeated value. |
|
257 | + * @return EnumerableGeneration A sequence that contains a repeated value. |
|
255 | 258 | * @package YaLinqo\Generation |
256 | 259 | */ |
257 | 260 | public static function repeat ($element, $count = null) |
@@ -270,7 +273,7 @@ discard block |
||
270 | 273 | * @param string $subject The input string. |
271 | 274 | * @param string $pattern The pattern to search for, as a string. |
272 | 275 | * @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. |
273 | - * @return Enumerable |
|
276 | + * @return EnumerableGeneration |
|
274 | 277 | * @see preg_split |
275 | 278 | * @package YaLinqo\Generation |
276 | 279 | */ |
@@ -16,11 +16,11 @@ discard block |
||
16 | 16 | * @return Enumerable Endless list of items repeating the source sequence. |
17 | 17 | * @package YaLinqo\Generation |
18 | 18 | */ |
19 | - public static function cycle ($source) |
|
19 | + public static function cycle($source) |
|
20 | 20 | { |
21 | 21 | $source = self::from($source); |
22 | 22 | |
23 | - return new self(function () use ($source) { |
|
23 | + return new self(function() use ($source) { |
|
24 | 24 | $isEmpty = true; |
25 | 25 | while (true) { |
26 | 26 | foreach ($source as $v) { |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | * @return Enumerable |
40 | 40 | * @package YaLinqo\Generation |
41 | 41 | */ |
42 | - public static function emptyEnum () |
|
42 | + public static function emptyEnum() |
|
43 | 43 | { |
44 | 44 | return new self(new \EmptyIterator, false); |
45 | 45 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | * @return Enumerable |
61 | 61 | * @package YaLinqo\Generation |
62 | 62 | */ |
63 | - public static function from ($source) |
|
63 | + public static function from($source) |
|
64 | 64 | { |
65 | 65 | $it = null; |
66 | 66 | if ($source instanceof Enumerable) |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | throw new \InvalidArgumentException('source must be array or Traversable.'); |
80 | 80 | } |
81 | 81 | |
82 | - private static function fromTraversable ($source) |
|
82 | + private static function fromTraversable($source) |
|
83 | 83 | { |
84 | 84 | foreach ($source as $k => $v) |
85 | 85 | yield $k => $v; |
@@ -96,12 +96,12 @@ discard block |
||
96 | 96 | * @return Enumerable |
97 | 97 | * @package YaLinqo\Generation |
98 | 98 | */ |
99 | - public static function generate ($funcValue, $seedValue = null, $funcKey = null, $seedKey = null) |
|
99 | + public static function generate($funcValue, $seedValue = null, $funcKey = null, $seedKey = null) |
|
100 | 100 | { |
101 | 101 | $funcValue = Utils::createLambda($funcValue, 'v,k'); |
102 | 102 | $funcKey = Utils::createLambda($funcKey, 'v,k', false); |
103 | 103 | |
104 | - return new self(function () use ($funcValue, $funcKey, $seedValue, $seedKey) { |
|
104 | + return new self(function() use ($funcValue, $funcKey, $seedValue, $seedKey) { |
|
105 | 105 | $key = $seedKey === null ? ($funcKey ? $funcKey($seedValue, $seedKey) : 0) : $seedKey; |
106 | 106 | $value = $seedValue === null ? $funcValue($seedValue, $seedKey) : $seedValue; |
107 | 107 | yield $key => $value; |
@@ -123,9 +123,9 @@ discard block |
||
123 | 123 | * @return Enumerable |
124 | 124 | * @package YaLinqo\Generation |
125 | 125 | */ |
126 | - public static function toInfinity ($start = 0, $step = 1) |
|
126 | + public static function toInfinity($start = 0, $step = 1) |
|
127 | 127 | { |
128 | - return new self(function () use ($start, $step) { |
|
128 | + return new self(function() use ($start, $step) { |
|
129 | 129 | $value = $start - $step; |
130 | 130 | while (true) |
131 | 131 | yield $value += $step; |
@@ -142,9 +142,9 @@ discard block |
||
142 | 142 | * @see preg_match_all |
143 | 143 | * @package YaLinqo\Generation |
144 | 144 | */ |
145 | - public static function matches ($subject, $pattern, $flags = PREG_SET_ORDER) |
|
145 | + public static function matches($subject, $pattern, $flags = PREG_SET_ORDER) |
|
146 | 146 | { |
147 | - return new self(function () use ($subject, $pattern, $flags) { |
|
147 | + return new self(function() use ($subject, $pattern, $flags) { |
|
148 | 148 | preg_match_all($pattern, $subject, $matches, $flags); |
149 | 149 | return $matches !== false ? self::from($matches)->getIterator() : self::emptyEnum(); |
150 | 150 | }); |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | * @return Enumerable |
159 | 159 | * @package YaLinqo\Generation |
160 | 160 | */ |
161 | - public static function toNegativeInfinity ($start = 0, $step = 1) |
|
161 | + public static function toNegativeInfinity($start = 0, $step = 1) |
|
162 | 162 | { |
163 | 163 | return self::toInfinity($start, -$step); |
164 | 164 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | * @return Enumerable Observable sequence containing the single specified element. |
171 | 171 | * @package YaLinqo\Generation |
172 | 172 | */ |
173 | - public static function returnEnum ($element) |
|
173 | + public static function returnEnum($element) |
|
174 | 174 | { |
175 | 175 | return self::repeat($element, 1); |
176 | 176 | } |
@@ -186,11 +186,11 @@ discard block |
||
186 | 186 | * @return Enumerable A sequence that contains a range of integral numbers. |
187 | 187 | * @package YaLinqo\Generation |
188 | 188 | */ |
189 | - public static function range ($start, $count, $step = 1) |
|
189 | + public static function range($start, $count, $step = 1) |
|
190 | 190 | { |
191 | 191 | if ($count <= 0) |
192 | 192 | return self::emptyEnum(); |
193 | - return new self(function () use ($start, $count, $step) { |
|
193 | + return new self(function() use ($start, $count, $step) { |
|
194 | 194 | $value = $start - $step; |
195 | 195 | while ($count-- > 0) |
196 | 196 | yield $value += $step; |
@@ -208,7 +208,7 @@ discard block |
||
208 | 208 | * @return Enumerable A sequence that contains a range of integral numbers. |
209 | 209 | * @package YaLinqo\Generation |
210 | 210 | */ |
211 | - public static function rangeDown ($start, $count, $step = 1) |
|
211 | + public static function rangeDown($start, $count, $step = 1) |
|
212 | 212 | { |
213 | 213 | return self::range($start, $count, -$step); |
214 | 214 | } |
@@ -225,11 +225,11 @@ discard block |
||
225 | 225 | * @return Enumerable A sequence that contains a range of integral numbers. |
226 | 226 | * @package YaLinqo\Generation |
227 | 227 | */ |
228 | - public static function rangeTo ($start, $end, $step = 1) |
|
228 | + public static function rangeTo($start, $end, $step = 1) |
|
229 | 229 | { |
230 | 230 | if ($step <= 0) |
231 | 231 | throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
232 | - return new self(function () use ($start, $end, $step) { |
|
232 | + return new self(function() use ($start, $end, $step) { |
|
233 | 233 | if ($start <= $end) { |
234 | 234 | for ($i = $start; $i < $end; $i += $step) |
235 | 235 | yield $i; |
@@ -254,11 +254,11 @@ discard block |
||
254 | 254 | * @return Enumerable A sequence that contains a repeated value. |
255 | 255 | * @package YaLinqo\Generation |
256 | 256 | */ |
257 | - public static function repeat ($element, $count = null) |
|
257 | + public static function repeat($element, $count = null) |
|
258 | 258 | { |
259 | 259 | if ($count < 0) |
260 | 260 | throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
261 | - return new self(function () use ($element, $count) { |
|
261 | + return new self(function() use ($element, $count) { |
|
262 | 262 | for ($i = 0; $i < $count || $count === null; $i++) |
263 | 263 | yield $element; |
264 | 264 | }); |
@@ -274,7 +274,7 @@ discard block |
||
274 | 274 | * @see preg_split |
275 | 275 | * @package YaLinqo\Generation |
276 | 276 | */ |
277 | - public static function split ($subject, $pattern, $flags = 0) |
|
277 | + public static function split($subject, $pattern, $flags = 0) |
|
278 | 278 | { |
279 | 279 | return new self( |
280 | 280 | new \ArrayIterator(preg_split($pattern, $subject, -1, $flags)), |
@@ -27,8 +27,9 @@ discard block |
||
27 | 27 | yield $v; |
28 | 28 | $isEmpty = false; |
29 | 29 | } |
30 | - if ($isEmpty) |
|
31 | - throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
30 | + if ($isEmpty) { |
|
31 | + throw new \UnexpectedValueException(Errors::NO_ELEMENTS); |
|
32 | + } |
|
32 | 33 | } |
33 | 34 | }); |
34 | 35 | } |
@@ -63,16 +64,17 @@ discard block |
||
63 | 64 | public static function from ($source) |
64 | 65 | { |
65 | 66 | $it = null; |
66 | - if ($source instanceof Enumerable) |
|
67 | - return $source; |
|
68 | - else if (is_array($source)) |
|
69 | - $it = new \ArrayIterator($source); |
|
70 | - elseif ($source instanceof \Iterator) |
|
71 | - $it = $source; |
|
72 | - elseif ($source instanceof \IteratorAggregate) |
|
73 | - $it = $source->getIterator(); |
|
74 | - elseif ($source instanceof \Traversable) |
|
75 | - $it = self::fromTraversable($source); |
|
67 | + if ($source instanceof Enumerable) { |
|
68 | + return $source; |
|
69 | + } else if (is_array($source)) { |
|
70 | + $it = new \ArrayIterator($source); |
|
71 | + } elseif ($source instanceof \Iterator) { |
|
72 | + $it = $source; |
|
73 | + } elseif ($source instanceof \IteratorAggregate) { |
|
74 | + $it = $source->getIterator(); |
|
75 | + } elseif ($source instanceof \Traversable) { |
|
76 | + $it = self::fromTraversable($source); |
|
77 | + } |
|
76 | 78 | if ($it !== null) { |
77 | 79 | return new self($it, false); |
78 | 80 | } |
@@ -81,8 +83,9 @@ discard block |
||
81 | 83 | |
82 | 84 | private static function fromTraversable ($source) |
83 | 85 | { |
84 | - foreach ($source as $k => $v) |
|
85 | - yield $k => $v; |
|
86 | + foreach ($source as $k => $v) { |
|
87 | + yield $k => $v; |
|
88 | + } |
|
86 | 89 | } |
87 | 90 | |
88 | 91 | /** |
@@ -127,8 +130,9 @@ discard block |
||
127 | 130 | { |
128 | 131 | return new self(function () use ($start, $step) { |
129 | 132 | $value = $start - $step; |
130 | - while (true) |
|
131 | - yield $value += $step; |
|
133 | + while (true) { |
|
134 | + yield $value += $step; |
|
135 | + } |
|
132 | 136 | }); |
133 | 137 | } |
134 | 138 | |
@@ -188,12 +192,14 @@ discard block |
||
188 | 192 | */ |
189 | 193 | public static function range ($start, $count, $step = 1) |
190 | 194 | { |
191 | - if ($count <= 0) |
|
192 | - return self::emptyEnum(); |
|
195 | + if ($count <= 0) { |
|
196 | + return self::emptyEnum(); |
|
197 | + } |
|
193 | 198 | return new self(function () use ($start, $count, $step) { |
194 | 199 | $value = $start - $step; |
195 | - while ($count-- > 0) |
|
196 | - yield $value += $step; |
|
200 | + while ($count-- > 0) { |
|
201 | + yield $value += $step; |
|
202 | + } |
|
197 | 203 | }); |
198 | 204 | } |
199 | 205 | |
@@ -227,16 +233,18 @@ discard block |
||
227 | 233 | */ |
228 | 234 | public static function rangeTo ($start, $end, $step = 1) |
229 | 235 | { |
230 | - if ($step <= 0) |
|
231 | - throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
|
236 | + if ($step <= 0) { |
|
237 | + throw new \InvalidArgumentException(Errors::STEP_NEGATIVE); |
|
238 | + } |
|
232 | 239 | return new self(function () use ($start, $end, $step) { |
233 | 240 | if ($start <= $end) { |
234 | - for ($i = $start; $i < $end; $i += $step) |
|
235 | - yield $i; |
|
236 | - } |
|
237 | - else { |
|
238 | - for ($i = $start; $i > $end; $i -= $step) |
|
239 | - 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 | + } |
|
240 | 248 | } |
241 | 249 | }); |
242 | 250 | } |
@@ -256,11 +264,13 @@ discard block |
||
256 | 264 | */ |
257 | 265 | public static function repeat ($element, $count = null) |
258 | 266 | { |
259 | - if ($count < 0) |
|
260 | - throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
|
267 | + if ($count < 0) { |
|
268 | + throw new \InvalidArgumentException(Errors::COUNT_LESS_THAN_ZERO); |
|
269 | + } |
|
261 | 270 | return new self(function () use ($element, $count) { |
262 | - for ($i = 0; $i < $count || $count === null; $i++) |
|
263 | - yield $element; |
|
271 | + for ($i = 0; $i < $count || $count === null; $i++) { |
|
272 | + yield $element; |
|
273 | + } |
|
264 | 274 | }); |
265 | 275 | } |
266 | 276 |