Completed
Push — master ( 2ef9a2...e0a631 )
by Pol
02:35
created

Product::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Product.
9
 *
10
 * @package drupol\phpermutations\Iterators
11
 */
12
class Product extends Combinatorics implements \Iterator, \Countable {
13
14
  /**
15
   * The key.
16
   *
17
   * @var int
18
   */
19
  protected $key;
20
21
  /**
22
   * The iterators.
23
   *
24
   * @var \Iterator[]
25
   */
26
  protected $iterators = array();
27
28
  /**
29
   * Product constructor.
30
   *
31
   * @param array $datasetArray
32
   *   The array of dataset.
33
   */
34 4
  public function __construct(array $datasetArray) {
35 4
    parent::__construct($datasetArray);
36
37 4
    foreach ($datasetArray as $dataset) {
38 4
      $this->iterators[] = new \ArrayIterator($dataset);
39 4
    }
40
41 4
    $this->key = 0;
42 4
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47 4
  public function current() {
48 4
    $tuple = array();
49
50 4
    foreach ($this->iterators as $iterator) {
51 4
      $tuple[] = $iterator->current();
52 4
    }
53
54 4
    return $tuple;
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60 4
  public function next() {
61 4
    $reverseIterators = array_reverse($this->iterators);
62
63 4
    foreach ($reverseIterators as $key => $iterator) {
64 4
      $iterator->next();
65 4
      if ($iterator->valid()) {
66 4
        foreach ($this->iterators as $key2 => $iterator2) {
67 4
          if ($key >= count($this->iterators) - $key2) {
68 4
            $iterator2->rewind();
69 4
          }
70 4
        }
71 4
        break;
72
      }
73 4
    }
74
75 4
    $this->key++;
76 4
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81
  public function key() {
82
    return $this->key;
83
  }
84
85
  /**
86
   * {@inheritdoc}
87
   */
88 4
  public function valid() {
89 4
    $isUnlessOneValid = FALSE;
90
91 4
    foreach ($this->iterators as $iterator) {
92 4
      if ($iterator->valid()) {
93 4
        $isUnlessOneValid = TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
94 4
      }
95 4
    }
96
97 4
    return $isUnlessOneValid;
98
  }
99
100
  /**
101
   * {@inheritdoc}
102
   */
103 4
  public function rewind() {
104 4
    foreach ($this->iterators as $iterator) {
105 4
      $iterator->rewind();
106 4
    }
107
108 4
    $this->key = 0;
109 4
  }
110
111
  /**
112
   * {@inheritdoc}
113
   */
114 4
  public function count() {
115 4
    $product = 1;
116
117 4
    foreach ($this->getDataset() as $dataset) {
118 4
      $product *= count($dataset);
119 4
    }
120
121 4
    return $product;
122
  }
123
124
  /**
125
   * Convert the iterator into an array.
126
   *
127
   * @return array
128
   *   The elements.
129
   */
130 4 View Code Duplication
  public function toArray() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131 4
    $data = array();
132
133 4
    for ($this->rewind(); $this->valid(); $this->next()) {
134 4
      $data[] = $this->current();
135 4
    }
136
137 4
    return $data;
138
  }
139
140
}
141