Completed
Push — master ( 4ae196...831fe8 )
by Pol
02:12
created

Fibonacci::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 {
13
14
  /**
15
   * @var int
16
   */
17
  protected $max;
18
19
  /**
20
   * @var int
21
   */
22
  private $previous = 1;
23
24
  /**
25
   * @var int
26
   */
27
  private $current = 0;
28
29
  /**
30
   * @var int
31
   */
32
  private $key = 0;
33
34
  /**
35
   * Fibonacci constructor.
36
   */
37 2
  public function __construct() {
38 2
    $this->setMaxLimit(PHP_INT_MAX);
39 2
  }
40
41
  /**
42
   * @return int
43
   */
44 2
  public function current() {
45 2
    return $this->current;
46
  }
47
48
  /**
49
   * @return int
50
   */
51
  public function key() {
52
    return $this->key;
53
  }
54
55
  /**
56
   *
57
   */
58 2
  public function next() {
59 2
    list($this->current, $this->previous) = array($this->current + $this->previous, $this->current);
60 2
    $this->key++;
61 2
  }
62
63
  /**
64
   *
65
   */
66 2
  public function rewind() {
67 2
    $this->previous = 1;
68 2
    $this->current = 0;
69 2
    $this->key = 0;
70 2
  }
71
72
  /**
73
   * @return bool
74
   */
75 2
  public function valid() {
76 2
    return ($this->current < $this->getMaxLimit());
77
  }
78
79
  /**
80
   * @return int
81
   */
82 2
  public function count() {
83 2
    return count($this->toArray());
84
  }
85
86
  /**
87
   * @return array
88
   */
89 2
  public function toArray() {
90 2
    $data = array();
91
92 2
    for ($this->rewind(); $this->valid(); $this->next()) {
93 2
      $data[] = $this->current();
94
    }
95
96 2
    return $data;
97
  }
98
99
  /**
100
   * @param int $max
101
   */
102 2
  public function setMaxLimit($max) {
103 2
    $this->max = $max;
104 2
  }
105
106
  /**
107
   * @return int
108
   */
109 2
  public function getMaxLimit() {
110 2
    return intval($this->max);
111
  }
112
113
}
114