Completed
Push — master ( 9b6b65...1498b8 )
by Pol
02:35
created

FiniteGroup::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class FiniteGroup.
9
 *
10
 * The finite group is an abelian finite cyclic group.
11
 *
12
 * @package drupol\phpermutations\Iterators
13
 */
14
class FiniteGroup extends Combinatorics implements \Iterator, \Countable {
15
16
  /**
17
   * The group size.
18
   *
19
   * @var int
20
   */
21
  protected $size;
22
23
  /**
24
   * The group.
25
   *
26
   * @var int[]
27
   */
28
  protected $group;
29
30
  /**
31
   * The key.
32
   *
33
   * @var int
34
   */
35
  protected $key;
36
37
  /**
38
   * {@inheritdoc}
39
   */
40 2
  public function current() {
41 2
    return current($this->group);
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47 2
  public function next() {
48 2
    $this->key++;
49 2
    next($this->group);
50 2
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55 2
  public function key() {
56 2
    return $this->key;
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62 2
  public function valid() {
63 2
    return isset($this->group[$this->key()]);
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69 2
  public function rewind() {
70 2
    $this->key = 0;
71 2
  }
72
73
  /**
74
   * Count elements of an object.
75
   *
76
   * @return int
77
   *   The number of element.
78
   */
79 2
  public function count() {
80 2
    return count($this->group);
81
  }
82
83
  /**
84
   * Convert the iterator into an array.
85
   *
86
   * @return array
87
   *   The elements.
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 2
    }
95
96 2
    return $data;
97
  }
98
99
  /**
100
   * Set the group size.
101
   *
102
   * @param int $size
103
   *   The size.
104
   */
105 2
  public function setSize($size) {
106 2
    $this->size = $size;
107 2
    $this->computeGroup();
108 2
  }
109
110
  /**
111
   * Get the group size.
112
   *
113
   * @return int
114
   *   The size.
115
   */
116 2
  public function getSize() {
117 2
    return intval($this->size);
118
  }
119
120
  /**
121
   * Clean out the group from unwanted values.
122
   */
123 2
  private function computeGroup() {
124 2
    $this->group = array();
125
126 2
    foreach (range(1, $this->getSize() - 1) as $key => $number) {
127 2
      if ($this->gcd($number, $this->getSize() - 1) == 1) {
128 2
        $this->group[] = $number;
129 2
      }
130 2
    }
131 2
  }
132
133
  /**
134
   * Get the greater common divisor between two numbers.
135
   *
136
   * @param int $a
137
   *   The first number.
138
   * @param int $b
139
   *   The second number.
140
   *
141
   * @return int
142
   *   The greater common divisor between $a and $b.
143
   */
144 2
  private function gcd($a, $b) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $b. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
145 2
    return $b ? $this->gcd($b, $a % $b) : $a;
146
  }
147
148
  public function order($generator) {
149
150
    //foreach($this->)
151
152
  }
153
154
}
155