| 1 | <?php |
||
| 12 | class Fibonacci extends Combinatorics implements \Iterator, \Countable |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * The maximum limit. |
||
| 16 | * |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | protected $max; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The previous element. |
||
| 23 | * |
||
| 24 | * @var int |
||
| 25 | */ |
||
| 26 | private $previous = 1; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The current element. |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | private $current = 0; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The current key. |
||
| 37 | * |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $key = 0; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Fibonacci constructor. |
||
| 44 | */ |
||
| 45 | 2 | public function __construct() |
|
| 46 | { |
||
| 47 | 2 | $this->setMaxLimit(PHP_INT_MAX); |
|
| 48 | 2 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | 2 | public function current() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function key() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 2 | public function next() |
|
| 70 | { |
||
| 71 | 2 | list($this->current, $this->previous) = [$this->current + $this->previous, $this->current]; |
|
| 72 | 2 | $this->key++; |
|
| 73 | 2 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 2 | public function rewind() |
|
| 79 | { |
||
| 80 | 2 | $this->previous = 1; |
|
| 81 | 2 | $this->current = 0; |
|
| 82 | 2 | $this->key = 0; |
|
| 83 | 2 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | 2 | public function valid() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | 2 | public function count() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Convert the iterator into an array. |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | * The elements. |
||
| 106 | */ |
||
| 107 | 2 | public function toArray() |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Set the maximum limit. |
||
| 120 | * |
||
| 121 | * @param int $max |
||
| 122 | * The limit. |
||
| 123 | */ |
||
| 124 | 2 | public function setMaxLimit($max) |
|
| 125 | { |
||
| 126 | 2 | $this->max = $max; |
|
| 127 | 2 | } |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Get the maximum limit. |
||
| 131 | * |
||
| 132 | * @return int |
||
| 133 | * The limit. |
||
| 134 | */ |
||
| 135 | 2 | public function getMaxLimit() |
|
| 139 | } |
||
| 140 |