|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KoenHoeijmakers\PaperKeyGenerator; |
|
4
|
|
|
|
|
5
|
|
|
use KoenHoeijmakers\PaperKeyGenerator\WordLists\Interfaces\WordListInterface; |
|
6
|
|
|
|
|
7
|
|
|
class PaperKeyGenerator |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The word count. |
|
11
|
|
|
* |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $count = 12; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The word divider. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $divider = ' '; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The WordList implementation. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \KoenHoeijmakers\PaperKeyGenerator\WordLists\Interfaces\WordListInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $wordList; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* PaperKey constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \KoenHoeijmakers\PaperKeyGenerator\WordLists\Interfaces\WordListInterface $wordList |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(WordListInterface $wordList) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setWordList($wordList); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Make a new PaperKey string. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function make(array $options = []) |
|
47
|
|
|
{ |
|
48
|
|
|
$count = $options['count'] ?? $this->count; |
|
49
|
|
|
$divider = $options['divider'] ?? $this->divider; |
|
50
|
|
|
|
|
51
|
|
|
$words = []; |
|
52
|
|
|
|
|
53
|
|
|
for ($iterator = 0; $iterator < $count; $iterator++) { |
|
54
|
|
|
$words[] = $this->wordList->getWord( |
|
55
|
|
|
$this->getRandomInteger() |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return implode($divider, $words); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the WordList to use. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \KoenHoeijmakers\PaperKeyGenerator\WordLists\Interfaces\WordListInterface $wordList |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setWordList(WordListInterface $wordList): self |
|
69
|
|
|
{ |
|
70
|
|
|
$this->wordList = $wordList; |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Set the count. |
|
77
|
|
|
* |
|
78
|
|
|
* @param $count |
|
79
|
|
|
* @return \KoenHoeijmakers\PaperKeyGenerator\PaperKeyGenerator |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setCount($count): self |
|
82
|
|
|
{ |
|
83
|
|
|
$this->count = (int) $count; |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set the divider. |
|
90
|
|
|
* |
|
91
|
|
|
* @param $divider |
|
92
|
|
|
* @return \KoenHoeijmakers\PaperKeyGenerator\PaperKeyGenerator |
|
93
|
|
|
*/ |
|
94
|
|
|
public function setDivider($divider): self |
|
95
|
|
|
{ |
|
96
|
|
|
$this->divider = $divider; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get a cryptographically secure integer. |
|
103
|
|
|
* |
|
104
|
|
|
* @return int |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getRandomInteger() |
|
107
|
|
|
{ |
|
108
|
|
|
return random_int(0, $this->wordList->getWordsCount()); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|