1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace DummyGenerator\Core; |
6
|
|
|
|
7
|
|
|
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface; |
8
|
|
|
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait; |
9
|
|
|
use DummyGenerator\Definitions\Extension\Exception\ExtensionArgumentException; |
10
|
|
|
use DummyGenerator\Definitions\Extension\Exception\ExtensionRuntimeException; |
11
|
|
|
use DummyGenerator\Definitions\Extension\NumberExtensionInterface; |
12
|
|
|
|
13
|
|
|
class Number implements NumberExtensionInterface, RandomizerAwareExtensionInterface |
14
|
|
|
{ |
15
|
|
|
use RandomizerAwareExtensionTrait; |
16
|
|
|
|
17
|
2 |
|
public function numberBetween(int $min = 0, int $max = 2147483647): int |
18
|
|
|
{ |
19
|
2 |
|
return $this->randomizer->getInt($min, $max); |
20
|
|
|
} |
21
|
|
|
|
22
|
2 |
|
public function randomDigit(): int |
23
|
|
|
{ |
24
|
2 |
|
return $this->randomizer->getInt(0, 9); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function randomDigitNot(int $except = 0, int $retries = 1000): int |
28
|
|
|
{ |
29
|
1 |
|
$count = 0; |
30
|
|
|
do { |
31
|
1 |
|
if ($count > $retries) { |
32
|
|
|
throw new ExtensionRuntimeException('Retries limit exceeded for randomDigitNot.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
$result = $this->numberBetween(0, 9); |
36
|
1 |
|
$count++; |
37
|
1 |
|
} while ($result === $except); |
38
|
|
|
|
39
|
1 |
|
return $result; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function randomDigitNotZero(): int |
43
|
|
|
{ |
44
|
2 |
|
return $this->randomizer->getInt(1, 9); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
public function randomFloat(?int $nbMaxDecimals = null, float $min = 0, ?float $max = null): float |
48
|
|
|
{ |
49
|
2 |
|
if ($max > PHP_FLOAT_MAX) { |
50
|
|
|
throw new ExtensionArgumentException('randomFloat() can only generate numbers up to PHP_FLOAT_MAX'); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$float = $this->randomizer->getFloat($min, $max ?? PHP_FLOAT_MAX); |
54
|
|
|
|
55
|
2 |
|
if (null === $nbMaxDecimals) { |
56
|
1 |
|
$nbMaxDecimals = $this->randomDigit(); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
return round($float, $nbMaxDecimals); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function randomNumber(?int $nbDigits = null, bool $strict = false): int |
63
|
|
|
{ |
64
|
2 |
|
if (null === $nbDigits) { |
65
|
1 |
|
$nbDigits = $this->randomDigitNotZero(); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
$max = 10 ** $nbDigits - 1; |
69
|
|
|
|
70
|
2 |
|
if ($max > PHP_INT_MAX) { |
71
|
|
|
throw new ExtensionArgumentException('randomNumber() can only generate numbers up to PHP_INT_MAX'); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
if ($strict) { |
75
|
1 |
|
return $this->randomizer->getInt(10 ** ($nbDigits - 1), $max); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $this->randomizer->getInt(0, $max); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function boolean(int $chanceOfGettingTrue = 50): bool |
82
|
|
|
{ |
83
|
1 |
|
return $this->randomizer->getBool($chanceOfGettingTrue); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|