Passed
Push — master ( 7039ca...2ef9a2 )
by Pol
02:34
created

Permutations::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
namespace drupol\phpermutations\Generators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class Permutations.
9
 *
10
 * @package drupol\phpermutations\Generators
11
 *
12
 * Inspired by the work of Mark Wilson <[email protected]>
13
 */
14
class Permutations extends Combinatorics {
15
16
  /**
17
   * The combinations generator.
18
   *
19
   * @var \drupol\phpermutations\Generators\Combinations
20
   */
21
  public $combinations;
22
23
  /**
24
   * Combinatorics constructor.
25
   *
26
   * @param array $dataset
27
   *   The dataset.
28
   * @param int|null $length
29
   *   The length.
30
   */
31 1
  public function __construct(array $dataset = array(), $length = NULL) {
32 1
    parent::__construct($dataset, $length);
33 1
    $this->combinations = new Combinations($dataset, $this->getLength());
34 1
  }
35
36
  /**
37
   * Alias of the get() method.
38
   *
39
   * @return \Generator
40
   *   The prime generator.
41
   */
42 1
  public function generator() {
43 1
    return $this->get($this->getDataset(), $this->getLength());
44
  }
45
46
  /**
47
   * The combination generator.
48
   *
49
   * @param array $dataset
50
   *   The dataset.
51
   * @param int $length
52
   *   The length.
53
   *
54
   * @codingStandardsIgnoreStart
55
   * @return \Generator
56
   * @codingStandardsIgnoreEnd
57
   */
58 1
  protected function get(array $dataset, $length) {
59 1
    foreach ($this->combinations->generator() as $combination) {
60 1
      foreach ($this->getPermutations($combination) as $current) {
61 1
        yield $current;
62
      }
63
    }
64 1
  }
65
66
  /**
67
   * The permutations generator.
68
   *
69
   * @param array $dataset
70
   *   The dataset.
71
   *
72
   * @codingStandardsIgnoreStart
73
   * @return \Generator
74
   * @codingStandardsIgnoreEnd
75
   */
76 1
  protected function getPermutations(array $dataset) {
77 1
    foreach ($dataset as $key => $firstItem) {
78 1
      $remaining = $dataset;
79 1
      array_splice($remaining, $key, 1);
80 1
      if (count($remaining) === 0) {
81 1
        yield [$firstItem];
82 1
        continue;
83
      }
84 1
      foreach ($this->getPermutations($remaining) as $permutation) {
85 1
        array_unshift($permutation, $firstItem);
86 1
        yield $permutation;
87
      }
88
    }
89 1
  }
90
91
  /**
92
   * Convert the generator into an array.
93
   *
94
   * @return array
95
   *   The elements.
96
   */
97 1
  public function toArray() {
98 1
    $data = array();
99
100 1
    foreach ($this->generator() as $value) {
101 1
      $data[] = $value;
102
    }
103
104 1
    return $data;
105
  }
106
107
  /**
108
   * {@inheritdoc}
109
   */
110 1
  public function count() {
111 1
    return $this->combinations->count() * $this->fact($this->getLength());
112
  }
113
114
}
115