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