1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Permutations. |
7
|
|
|
* |
8
|
|
|
* @package drupol\phpermutations |
9
|
|
|
*/ |
10
|
|
|
class Permutations implements \Countable { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var mixed |
14
|
|
|
*/ |
15
|
|
|
protected $dataset; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $length; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Permutations constructor. |
24
|
|
|
* |
25
|
|
|
* @param array $dataset |
26
|
|
|
* @param int $length |
|
|
|
|
27
|
|
|
*/ |
28
|
3 |
|
public function __construct(array $dataset = array(), $length = NULL) { |
|
|
|
|
29
|
3 |
|
$this->setDataset($dataset); |
30
|
3 |
|
$length = ($length > $this->count()) ? $this->count() : $length; |
|
|
|
|
31
|
3 |
|
$this->setLength($length); |
32
|
|
|
|
33
|
3 |
|
return $this; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return int |
38
|
|
|
*/ |
39
|
3 |
|
public function count() { |
40
|
3 |
|
return count($this->getDataset()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param int $length |
45
|
|
|
*/ |
46
|
3 |
|
public function setLength($length) { |
47
|
3 |
|
$this->length = $length; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
3 |
|
public function getLength() { |
54
|
3 |
|
return (int) $this->length; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $dataset |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
3 |
|
public function setDataset(array $dataset = array()) { |
63
|
3 |
|
$this->dataset = $dataset; |
64
|
|
|
|
65
|
3 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
3 |
|
public function getDataset() { |
72
|
3 |
|
return $this->dataset; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
3 |
|
public function generator() { |
79
|
3 |
|
return $this->permute($this->getDataset(), array()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $items |
84
|
|
|
* @param array $perms |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
3 |
|
protected function permute($items, $perms = array( )) { |
89
|
3 |
|
$result = array(); |
90
|
|
|
|
91
|
3 |
|
foreach (new Combinations($items, $this->getLength()) as $dataset) { |
92
|
3 |
|
if (empty($dataset)) { |
93
|
3 |
|
$return = array($perms); |
94
|
3 |
|
} else { |
95
|
3 |
|
$return = array(); |
96
|
3 |
|
for ($i = count($dataset) - 1; $i >= 0; --$i) { |
97
|
3 |
|
$newitems = $dataset; |
98
|
3 |
|
$newperms = $perms; |
99
|
3 |
|
list($foo) = array_splice($newitems, $i, 1); |
100
|
3 |
|
array_unshift($newperms, $foo); |
101
|
3 |
|
$return = array_merge($return, $this->permute($newitems, $newperms)); |
102
|
3 |
|
} |
103
|
|
|
} |
104
|
3 |
|
$result = array_merge($result, $return); |
105
|
3 |
|
} |
106
|
|
|
|
107
|
3 |
|
return $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
3 |
|
public function toArray() { |
114
|
3 |
|
$results = array(); |
115
|
|
|
|
116
|
3 |
|
foreach($this->generator() as $value) { |
|
|
|
|
117
|
3 |
|
$results[] = $value; |
118
|
3 |
|
} |
119
|
|
|
|
120
|
3 |
|
return $results; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.