Completed
Push — master ( fa4893...deeb78 )
by Pol
06:37
created

Fibonacci::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Fibonacci.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Fibonacci extends Combinatorics implements \Iterator, \Countable
13
{
14
15
  /**
16
   * The maximum limit.
17
   *
18
   * @var int
19
   */
20
    protected $max;
21
22
  /**
23
   * The previous element.
24
   *
25
   * @var int
26
   */
27
    private $previous = 1;
28
29
  /**
30
   * The current element.
31
   *
32
   * @var int
33
   */
34
    private $current = 0;
35
36
  /**
37
   * The current key.
38
   *
39
   * @var int
40
   */
41
    private $key = 0;
42
43
  /**
44
   * Fibonacci constructor.
45
   */
46 2
    public function __construct()
47
    {
48 2
        $this->setMaxLimit(PHP_INT_MAX);
49 2
    }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54 2
    public function current()
55
    {
56 2
        return $this->current;
57
    }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
    public function key()
63
    {
64
        return $this->key;
65
    }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70 2
    public function next()
71
    {
72 2
        list($this->current, $this->previous) = array($this->current + $this->previous, $this->current);
73 2
        $this->key++;
74 2
    }
75
76
  /**
77
   * {@inheritdoc}
78
   */
79 2
    public function rewind()
80
    {
81 2
        $this->previous = 1;
82 2
        $this->current = 0;
83 2
        $this->key = 0;
84 2
    }
85
86
  /**
87
   * {@inheritdoc}
88
   */
89 2
    public function valid()
90
    {
91 2
        return ($this->current < $this->getMaxLimit());
92
    }
93
94
  /**
95
   * {@inheritdoc}
96
   */
97 2
    public function count()
98
    {
99 2
        return count($this->toArray());
100
    }
101
102
  /**
103
   * Convert the iterator into an array.
104
   *
105
   * @return array
106
   *   The elements.
107
   */
108 2
    public function toArray()
109
    {
110 2
        $data = array();
111
112 2
        for ($this->rewind(); $this->valid(); $this->next()) {
113 2
            $data[] = $this->current();
114
        }
115
116 2
        return $data;
117
    }
118
119
  /**
120
   * Set the maximum limit.
121
   *
122
   * @param int $max
123
   *   The limit.
124
   */
125 2
    public function setMaxLimit($max)
126
    {
127 2
        $this->max = $max;
128 2
    }
129
130
  /**
131
   * Get the maximum limit.
132
   *
133
   * @return int
134
   *   The limit.
135
   */
136 2
    public function getMaxLimit()
137
    {
138 2
        return intval($this->max);
139
    }
140
}
141