1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstraction for pseudo-random generator objects. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Abstractions\Randomness; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\Randomness\AbstractRandomness as RandomnessRepresentation; |
10
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\FloatOutputInterface as FloatOutput; |
11
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\ArbitraryBaseOutputInterface as BaseOutput; |
12
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\StringOutputInterface as StringOutput; |
13
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\IdentifierOutputInterface as UuidOutput; |
14
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\RgbOutputInterface as RgbOutput; |
15
|
|
|
use \CryptoManana\Core\Traits\Randomness\FloatOutputTrait as FloatGeneration; |
16
|
|
|
use \CryptoManana\Core\Traits\Randomness\ArbitraryBaseOutputTrait as BaseGeneration; |
17
|
|
|
use \CryptoManana\Core\Traits\Randomness\StringOutputTrait as StringGeneration; |
18
|
|
|
use \CryptoManana\Core\Traits\Randomness\IdentifierOutputTrait as UuidGeneration; |
19
|
|
|
use \CryptoManana\Core\Traits\Randomness\RgbOutputTrait as RgbGeneration; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class AbstractGenerator - Abstraction for pseudo-random generator classes. |
23
|
|
|
* |
24
|
|
|
* @package CryptoManana\Core\Abstractions\Randomness |
25
|
|
|
* |
26
|
|
|
* @mixin FloatGeneration |
27
|
|
|
* @mixin BaseGeneration |
28
|
|
|
* @mixin StringGeneration |
29
|
|
|
* @mixin UuidGeneration |
30
|
|
|
* @mixin RgbGeneration |
31
|
|
|
*/ |
32
|
|
|
abstract class AbstractGenerator extends RandomnessRepresentation implements |
33
|
|
|
FloatOutput, |
34
|
|
|
BaseOutput, |
35
|
|
|
StringOutput, |
36
|
|
|
UuidOutput, |
37
|
|
|
RgbOutput |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Float generation formats. |
41
|
|
|
* |
42
|
|
|
* {@internal Reusable implementation of `FloatOutputInterface`. }} |
43
|
|
|
*/ |
44
|
|
|
use FloatGeneration; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Arbitrary base generation formats. |
48
|
|
|
* |
49
|
|
|
* {@internal Reusable implementation of `ArbitraryBaseOutputInterface`. }} |
50
|
|
|
*/ |
51
|
|
|
use BaseGeneration; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* String generation formats. |
55
|
|
|
* |
56
|
|
|
* {@internal Reusable implementation of `StringOutputInterface`. }} |
57
|
|
|
*/ |
58
|
|
|
use StringGeneration; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Unique string identifier generation formats. |
62
|
|
|
* |
63
|
|
|
* {@internal Reusable implementation of `IdentifierOutputInterface`. }} |
64
|
|
|
*/ |
65
|
|
|
use UuidGeneration; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Red-Green-Blue (RGB) colour generation formats. |
69
|
|
|
* |
70
|
|
|
* {@internal Reusable implementation of `RgbOutputInterface`. }} |
71
|
|
|
*/ |
72
|
|
|
use RgbGeneration; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The default system precision storage. |
76
|
|
|
* |
77
|
|
|
* @var int|null The used default floating number precision. |
78
|
|
|
*/ |
79
|
|
|
protected static $systemPrecision = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Internal method for integer range validation. |
83
|
|
|
* |
84
|
|
|
* @param int $from The minimum number in the wanted range. |
85
|
|
|
* @param int $to The maximum number in the wanted range. |
86
|
|
|
* |
87
|
|
|
* @throws \Exception Validation errors. |
88
|
|
|
*/ |
89
|
102 |
|
protected function validateIntegerRange($from, $to) |
90
|
|
|
{ |
91
|
102 |
|
$from = filter_var( |
92
|
102 |
|
$from, |
93
|
102 |
|
FILTER_VALIDATE_INT, |
94
|
|
|
[ |
95
|
|
|
"options" => [ |
96
|
102 |
|
"min_range" => $this->getMinNumber(), |
97
|
102 |
|
"max_range" => $this->getMaxNumber(), |
98
|
|
|
], |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
|
102
|
102 |
|
$to = filter_var( |
103
|
102 |
|
$to, |
104
|
102 |
|
FILTER_VALIDATE_INT, |
105
|
|
|
[ |
106
|
|
|
"options" => [ |
107
|
102 |
|
"min_range" => $this->getMinNumber(), |
108
|
102 |
|
"max_range" => $this->getMaxNumber(), |
109
|
|
|
], |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
|
113
|
102 |
|
if ($from === false || $to === false) { |
114
|
18 |
|
throw new \DomainException( |
115
|
18 |
|
"The provided values are of invalid type or are out of the supported range." |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
84 |
|
if ($from >= $to) { |
120
|
12 |
|
throw new \LogicException( |
121
|
12 |
|
"The chosen generation maximum is less or equal the provided minimum value." |
122
|
|
|
); |
123
|
|
|
} |
124
|
72 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Internal method for validation of positive integers. |
128
|
|
|
* |
129
|
|
|
* @param int $integer The positive integer value. |
130
|
|
|
* @param bool $includeZero Flag for enabling the zero as a valid value. |
131
|
|
|
* |
132
|
|
|
* @throws \Exception Validation errors. |
133
|
|
|
*/ |
134
|
106 |
|
protected function validatePositiveInteger($integer, $includeZero = false) |
135
|
|
|
{ |
136
|
106 |
|
$integer = filter_var( |
137
|
106 |
|
$integer, |
138
|
106 |
|
FILTER_VALIDATE_INT, |
139
|
|
|
[ |
140
|
|
|
"options" => [ |
141
|
106 |
|
"min_range" => $includeZero ? 0 : 1, |
142
|
106 |
|
"max_range" => PHP_INT_MAX, |
143
|
|
|
], |
144
|
|
|
] |
145
|
|
|
); |
146
|
|
|
|
147
|
106 |
|
if ($integer === false) { |
148
|
6 |
|
throw new \DomainException('The provided value must be a positive integer.'); |
149
|
|
|
} |
150
|
100 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Randomness generator constructor. |
154
|
|
|
*/ |
155
|
206 |
|
public function __construct() |
156
|
|
|
{ |
157
|
|
|
// Fetch the global system precision setting |
158
|
206 |
|
if (is_null(self::$systemPrecision)) { |
159
|
2 |
|
self::$systemPrecision = (int)ini_get('precision'); |
160
|
|
|
} |
161
|
206 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Randomness generator reinitialization tasks after unserialization. |
165
|
|
|
*/ |
166
|
12 |
|
public function __wakeup() |
167
|
|
|
{ |
168
|
|
|
// Ensures randomness is reinitialized and auto-seeded |
169
|
12 |
|
$this->__construct(); |
170
|
12 |
|
} |
171
|
|
|
} |
172
|
|
|
|