| Total Complexity | 13 |
| Total Lines | 132 |
| Duplicated Lines | 0 % |
| Coverage | 80% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 16 | class FiniteGroup extends Iterators |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The group. |
||
| 20 | * |
||
| 21 | * @var int[] |
||
| 22 | */ |
||
| 23 | protected $group; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The group size. |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $size; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Count elements of an object. |
||
| 34 | * |
||
| 35 | * @return int |
||
| 36 | * The number of element |
||
| 37 | */ |
||
| 38 | 1 | public function count() |
|
| 39 | { |
||
| 40 | 1 | return count($this->group); |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | 1 | public function current() |
|
| 47 | { |
||
| 48 | 1 | return current($this->group); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Get the group size. |
||
| 53 | * |
||
| 54 | * @return int |
||
| 55 | * The size |
||
| 56 | */ |
||
| 57 | 1 | public function getSize() |
|
| 58 | { |
||
| 59 | 1 | return (int) $this->size; |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * {@inheritdoc} |
||
| 64 | * |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | 1 | public function next() |
|
| 68 | { |
||
| 69 | 1 | ++$this->key; |
|
| 70 | 1 | next($this->group); |
|
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Get the order. |
||
| 75 | * |
||
| 76 | * @param int $generator |
||
| 77 | * The generator |
||
| 78 | * |
||
| 79 | * @return int |
||
| 80 | * The order |
||
| 81 | */ |
||
| 82 | public function order($generator) |
||
| 83 | { |
||
| 84 | $result = []; |
||
| 85 | |||
| 86 | foreach (range(1, $this->getSize()) as $number) { |
||
| 87 | $value = ($generator ** $number) % $this->getSize(); |
||
| 88 | $result[$value] = $value; |
||
| 89 | } |
||
| 90 | |||
| 91 | return count($result); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set the group size. |
||
| 96 | * |
||
| 97 | * @param int $size |
||
| 98 | * The size |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | 1 | public function setSize($size) |
|
| 103 | { |
||
| 104 | 1 | $this->size = $size; |
|
| 105 | 1 | $this->computeGroup(); |
|
| 106 | 1 | } |
|
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | * |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | 1 | public function valid() |
|
| 114 | { |
||
| 115 | 1 | return isset($this->group[$this->key()]); |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Clean out the group from unwanted values. |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | 1 | private function computeGroup() |
|
| 130 | } |
||
| 131 | } |
||
| 132 | 1 | } |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get the greater common divisor between two numbers. |
||
| 136 | * |
||
| 137 | * @param int $a |
||
| 138 | * The first number |
||
| 139 | * @param int $b |
||
| 140 | * The second number |
||
| 141 | * |
||
| 142 | * @return int |
||
| 143 | * The greater common divisor between $a and $b |
||
| 144 | */ |
||
| 145 | 1 | private function gcd($a, $b) |
|
| 148 | } |
||
| 149 | } |
||
| 150 |