1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of string format generation for generator services. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\Randomness; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Traits\Randomness\RandomnessTrait as RandomnessSpecification; |
10
|
|
|
use \CryptoManana\Core\StringBuilder as StringBuilder; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait StringOutputTrait - Reusable implementation of `StringOutputInterface`. |
14
|
|
|
* |
15
|
|
|
* @see \CryptoManana\Core\Interfaces\Randomness\StringOutputInterface The abstract specification. |
16
|
|
|
* |
17
|
|
|
* @package CryptoManana\Core\Traits\Randomness |
18
|
|
|
* |
19
|
|
|
* @mixin RandomnessSpecification |
20
|
|
|
*/ |
21
|
|
|
trait StringOutputTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Forcing the implementation of the software abstract randomness. |
25
|
|
|
* |
26
|
|
|
* {@internal Forcing the implementation of `AbstractRandomness`. }} |
27
|
|
|
*/ |
28
|
|
|
use RandomnessSpecification; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Generate a random digit character. |
32
|
|
|
* |
33
|
|
|
* @param bool $includeZero Flag for including the zero digit (default => true). |
34
|
|
|
* |
35
|
|
|
* @return string Randomly generated digit character. |
36
|
|
|
* @throws \Exception Validation errors. |
37
|
|
|
*/ |
38
|
9 |
|
public function getDigit($includeZero = true) |
39
|
|
|
{ |
40
|
9 |
|
return ($includeZero) ? (string)$this->getInt(0, 9) : (string)$this->getInt(1, 9); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Generate a random english letter character. |
45
|
|
|
* |
46
|
|
|
* @param bool $caseSensitive Flag for enabling case sensitive generation (default => true). |
47
|
|
|
* |
48
|
|
|
* @return string Randomly generated english letter character. |
49
|
|
|
* @throws \Exception Validation errors. |
50
|
|
|
*/ |
51
|
6 |
|
public function getLetter($caseSensitive = true) |
52
|
|
|
{ |
53
|
6 |
|
if ($caseSensitive) { |
54
|
6 |
|
$upper = $this->getBool(); |
55
|
|
|
|
56
|
6 |
|
$letterCode = $upper ? $this->getInt(65, 90) : $this->getInt(97, 122); |
57
|
|
|
} else { |
58
|
3 |
|
$letterCode = $this->getInt(97, 122); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
return StringBuilder::getChr($letterCode); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Generate a random alphanumeric string. |
66
|
|
|
* |
67
|
|
|
* @param int $length The output string length (default => 1). |
68
|
|
|
* @param bool $caseSensitive Flag for enabling case sensitive generation (default => true). |
69
|
|
|
* |
70
|
|
|
* @return string Randomly generated alphanumeric string. |
71
|
|
|
* @throws \Exception Validation errors. |
72
|
|
|
*/ |
73
|
6 |
|
public function getAlphaNumeric($length = 1, $caseSensitive = true) |
74
|
|
|
{ |
75
|
6 |
|
$this->validatePositiveInteger($length); |
76
|
|
|
|
77
|
6 |
|
$id = ''; |
78
|
|
|
|
79
|
6 |
|
for ($i = 1; $i <= $length; $i++) { |
80
|
6 |
|
if ($this->getBool()) { |
81
|
6 |
|
$id .= $this->getLetter($caseSensitive); |
82
|
|
|
} else { |
83
|
6 |
|
$id .= $this->getDigit(true); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
return $id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Generate a random ASCII (American Standard Code) string containing only printable characters. |
92
|
|
|
* |
93
|
|
|
* @param int $length The output string length (default => 1). |
94
|
|
|
* @param bool|int $includeSpace Flag for including the space character (default => true). |
95
|
|
|
* |
96
|
|
|
* @return string Randomly generated ASCII string. |
97
|
|
|
* @throws \Exception Validation errors. |
98
|
|
|
*/ |
99
|
3 |
|
public function getAscii($length = 1, $includeSpace = false) |
100
|
|
|
{ |
101
|
3 |
|
$this->validatePositiveInteger($length); |
102
|
|
|
|
103
|
3 |
|
$asciiString = ''; |
104
|
3 |
|
$startFrom = ($includeSpace == true) ? 32 : 33; |
105
|
|
|
|
106
|
3 |
|
for ($i = 1; $i <= $length; $i++) { |
107
|
3 |
|
$asciiString .= StringBuilder::getChr($this->getInt($startFrom, 126)); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
return $asciiString; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Generate a random string with custom characters. |
115
|
|
|
* |
116
|
|
|
* @param int $length The output string length (default => 1). |
117
|
|
|
* @param array $characters The character map for the string generation (default => ASCII). |
118
|
|
|
* |
119
|
|
|
* @return string Randomly generated string using a custom character map. |
120
|
|
|
* @throws \Exception Validation errors. |
121
|
|
|
*/ |
122
|
12 |
|
public function getString($length = 1, array $characters = []) |
123
|
|
|
{ |
124
|
12 |
|
if (empty($characters)) { |
125
|
3 |
|
return $this->getAscii($length, true); |
126
|
|
|
} else { |
127
|
12 |
|
foreach ($characters as $char) { |
128
|
12 |
|
if (!is_string($char)) { |
129
|
3 |
|
throw new \InvalidArgumentException( |
130
|
3 |
|
'The provided symbol map must contain only elements of type string.' |
131
|
|
|
); |
132
|
12 |
|
} elseif (StringBuilder::stringLength($char) != 1) { |
133
|
3 |
|
throw new \LengthException( |
134
|
3 |
|
'The provided symbol map\'s values must only be of 1 character length.' |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
if (count($characters) < 2) { |
140
|
3 |
|
throw new \LengthException( |
141
|
3 |
|
'You must supply a set of at least 2 characters for the output string generation.' |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
$this->validatePositiveInteger($length); |
146
|
|
|
|
147
|
3 |
|
$passwordString = ''; |
148
|
3 |
|
$lastIndex = count($characters) - 1; |
149
|
|
|
|
150
|
3 |
|
for ($i = 1; $i <= $length; $i++) { |
151
|
3 |
|
$passwordString .= $characters[$this->getInt(0, $lastIndex)]; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
return $passwordString; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|