Completed
Push — master ( cba614...f7e6da )
by Pol
07:09
created

Combinations   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
cbo 1
dl 0
loc 56
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generator() 0 3 1
A get() 0 16 4
A toArray() 0 9 2
1
<?php
2
3
namespace drupol\phpermutations\Generators;
4
5
use drupol\phpermutations\Iterators\Combinations as CombinationsIterator;
6
7
/**
8
 * Class Combinations.
9
 *
10
 * @package drupol\phpermutations\Generators
11
 *
12
 * @author  Mark Wilson <[email protected]>
13
 */
14
class Combinations extends CombinationsIterator {
15
16
  /**
17
   * Alias of the get() method.
18
   *
19
   * @return \Generator
20
   *   The prime generator.
21
   */
22 2
  public function generator() {
23 2
    return $this->get($this->getDataset(), $this->getLength());
24
  }
25
26
  /**
27
   * The generator.
28
   *
29
   * @param array $dataset
30
   *   The dataset.
31
   *
32
   * @codingStandardsIgnoreStart
33
   * @return \Generator
34
   * @codingStandardsIgnoreEnd
35
   */
36 2
  protected function get(array $dataset, $length) {
37 2
    $originalLength  = count($dataset);
38 2
    $remainingLength = $originalLength - $length + 1;
39 2
    for ($i = 0; $i < $remainingLength; $i++) {
40 2
      $current = $dataset[$i];
41 2
      if ($length === 1) {
42 2
        yield [$current];
43 2
      } else {
44 2
        $remaining = array_slice($dataset, $i + 1);
45 2
        foreach ($this->get($remaining, $length - 1) as $permutation) {
46 2
          array_unshift($permutation, $current);
47 2
          yield $permutation;
48 2
        }
49
      }
50 2
    }
51 2
  }
52
53
  /**
54
   * Convert the generator into an array.
55
   *
56
   * @return array
57
   *   The elements.
58
   */
59 2
  public function toArray() {
60 2
    $data = array();
61
62 2
    foreach ($this->generator() as $value) {
63 2
      $data[] = $value;
64 2
    }
65
66 2
    return $data;
67
  }
68
69
}
70