|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BenTools\StringCombinations; |
|
4
|
|
|
|
|
5
|
|
|
use IteratorAggregate; |
|
6
|
|
|
|
|
7
|
|
|
final class NoDuplicateLettersStringCombinations implements IteratorAggregate, StringCombinationsInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var StringCombinations |
|
11
|
|
|
*/ |
|
12
|
|
|
private $stringCombinations; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* UniqueStringCombinations constructor. |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(StringCombinations $stringCombinations) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->stringCombinations = $stringCombinations; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getIterator() |
|
26
|
|
|
{ |
|
27
|
|
|
for ($i = $this->stringCombinations->min; $i <= $this->stringCombinations->max; $i++) { |
|
|
|
|
|
|
28
|
|
|
foreach ($this->permute($this->stringCombinations->charset, $i) as $combination) { |
|
|
|
|
|
|
29
|
|
|
yield implode($this->stringCombinations->glue, $combination); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritDoc |
|
36
|
|
|
*/ |
|
37
|
|
|
public function count() |
|
38
|
|
|
{ |
|
39
|
|
|
$arr = []; |
|
40
|
|
|
|
|
41
|
|
|
for ($pos = $this->stringCombinations->max, $i = 0; $pos >= $this->stringCombinations->min; $pos--, $i++) { |
|
|
|
|
|
|
42
|
|
|
if (0 === $i) { |
|
43
|
|
|
$arr[$i] = [$pos]; |
|
44
|
|
|
} else { |
|
45
|
|
|
$arr[$i] = array_merge($arr[$i - 1], [$pos]); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return array_sum(array_map('array_product', $arr)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function permute(array $charset, $length = null) |
|
53
|
|
|
{ |
|
54
|
|
|
$n = count($charset); |
|
55
|
|
|
|
|
56
|
|
|
if (null === $length) { |
|
57
|
|
|
$length = $n; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($length > $n) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$indices = range(0, $n - 1); |
|
65
|
|
|
$cycles = range($n, $n - $length + 1, -1); |
|
66
|
|
|
|
|
67
|
|
|
yield array_slice($charset, 0, $length); |
|
68
|
|
|
|
|
69
|
|
|
if ($n <= 0) { |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
while (true) { |
|
74
|
|
|
$exitEarly = false; |
|
75
|
|
|
for ($i = $length; $i--;) { |
|
76
|
|
|
$cycles[$i]-= 1; |
|
77
|
|
|
if ($cycles[$i] == 0) { |
|
78
|
|
|
if ($i < count($indices)) { |
|
79
|
|
|
$removed = array_splice($indices, $i, 1); |
|
80
|
|
|
$indices[] = $removed[0]; |
|
81
|
|
|
} |
|
82
|
|
|
$cycles[$i] = $n - $i; |
|
83
|
|
|
} else { |
|
84
|
|
|
$j = $cycles[$i]; |
|
85
|
|
|
$value = $indices[$i]; |
|
86
|
|
|
$negative = $indices[count($indices) - $j]; |
|
87
|
|
|
$indices[$i] = $negative; |
|
88
|
|
|
$indices[count($indices) - $j] = $value; |
|
89
|
|
|
$result = []; |
|
90
|
|
|
$counter = 0; |
|
91
|
|
|
foreach ($indices as $index) { |
|
92
|
|
|
$result[] = $charset[$index]; |
|
93
|
|
|
$counter++; |
|
94
|
|
|
if ($counter == $length) { |
|
95
|
|
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
yield $result; |
|
99
|
|
|
$exitEarly = true; |
|
100
|
|
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
if (!$exitEarly) { |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritDoc |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getRandomString() |
|
113
|
|
|
{ |
|
114
|
|
|
$charset = $this->stringCombinations->charset; |
|
|
|
|
|
|
115
|
|
|
$string = []; |
|
116
|
|
|
|
|
117
|
|
|
$length = random_int($this->stringCombinations->min, $this->stringCombinations->max); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
for ($pos = 1; $pos <= $length; $pos++) { |
|
120
|
|
|
shuffle($charset); |
|
121
|
|
|
|
|
122
|
|
|
$string[] = array_shift($charset); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return implode($this->stringCombinations->glue, $string); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritDoc |
|
130
|
|
|
*/ |
|
131
|
|
|
public function asArray() |
|
132
|
|
|
{ |
|
133
|
|
|
return iterator_to_array($this); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.