| @@ 6-69 (lines=64) @@ | ||
| 3 | ||
| 4 | namespace Innmind\Immutable; |
|
| 5 | ||
| 6 | class IntRange implements PrimitiveInterface, \Iterator |
|
| 7 | { |
|
| 8 | private $start; |
|
| 9 | private $end; |
|
| 10 | private $step; |
|
| 11 | private $key; |
|
| 12 | private $current; |
|
| 13 | ||
| 14 | public function __construct(int $start, int $end, int $step = 1) |
|
| 15 | { |
|
| 16 | $this->start = $start; |
|
| 17 | $this->end = $end; |
|
| 18 | $this->step = $step; |
|
| 19 | $this->key = 0; |
|
| 20 | $this->current = $start; |
|
| 21 | } |
|
| 22 | ||
| 23 | public function start(): int |
|
| 24 | { |
|
| 25 | return $this->start; |
|
| 26 | } |
|
| 27 | ||
| 28 | public function end(): int |
|
| 29 | { |
|
| 30 | return $this->end; |
|
| 31 | } |
|
| 32 | ||
| 33 | public function step(): int |
|
| 34 | { |
|
| 35 | return $this->step; |
|
| 36 | } |
|
| 37 | ||
| 38 | public function toPrimitive(): array |
|
| 39 | { |
|
| 40 | return range($this->start, $this->end, $this->step); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function current(): int |
|
| 44 | { |
|
| 45 | return $this->current; |
|
| 46 | } |
|
| 47 | ||
| 48 | public function key(): int |
|
| 49 | { |
|
| 50 | return $this->key; |
|
| 51 | } |
|
| 52 | ||
| 53 | public function next() |
|
| 54 | { |
|
| 55 | ++$this->key; |
|
| 56 | $this->current += $this->step; |
|
| 57 | } |
|
| 58 | ||
| 59 | public function rewind() |
|
| 60 | { |
|
| 61 | $this->key = 0; |
|
| 62 | $this->current = $this->start; |
|
| 63 | } |
|
| 64 | ||
| 65 | public function valid(): bool |
|
| 66 | { |
|
| 67 | return $this->current < $this->end; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 6-69 (lines=64) @@ | ||
| 3 | ||
| 4 | namespace Innmind\Immutable; |
|
| 5 | ||
| 6 | class NumericRange implements PrimitiveInterface, \Iterator |
|
| 7 | { |
|
| 8 | private $start; |
|
| 9 | private $end; |
|
| 10 | private $step; |
|
| 11 | private $key; |
|
| 12 | private $current; |
|
| 13 | ||
| 14 | public function __construct(float $start, float $end, float $step = 1) |
|
| 15 | { |
|
| 16 | $this->start = $start; |
|
| 17 | $this->end = $end; |
|
| 18 | $this->step = $step; |
|
| 19 | $this->key = 0; |
|
| 20 | $this->current = $start; |
|
| 21 | } |
|
| 22 | ||
| 23 | public function start(): float |
|
| 24 | { |
|
| 25 | return $this->start; |
|
| 26 | } |
|
| 27 | ||
| 28 | public function end(): float |
|
| 29 | { |
|
| 30 | return $this->end; |
|
| 31 | } |
|
| 32 | ||
| 33 | public function step(): float |
|
| 34 | { |
|
| 35 | return $this->step; |
|
| 36 | } |
|
| 37 | ||
| 38 | public function toPrimitive(): array |
|
| 39 | { |
|
| 40 | return range($this->start, $this->end, $this->step); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function current(): float |
|
| 44 | { |
|
| 45 | return $this->current; |
|
| 46 | } |
|
| 47 | ||
| 48 | public function key(): int |
|
| 49 | { |
|
| 50 | return $this->key; |
|
| 51 | } |
|
| 52 | ||
| 53 | public function next() |
|
| 54 | { |
|
| 55 | ++$this->key; |
|
| 56 | $this->current += $this->step; |
|
| 57 | } |
|
| 58 | ||
| 59 | public function rewind() |
|
| 60 | { |
|
| 61 | $this->key = 0; |
|
| 62 | $this->current = $this->start; |
|
| 63 | } |
|
| 64 | ||
| 65 | public function valid(): bool |
|
| 66 | { |
|
| 67 | return $this->current < $this->end; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||