1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of arbitrary base formats 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 ArbitraryBaseOutputTrait - Reusable implementation of `ArbitraryBaseOutputInterface`. |
14
|
|
|
* |
15
|
|
|
* @see \CryptoManana\Core\Interfaces\Randomness\ArbitraryBaseOutputInterface The abstract specification. |
16
|
|
|
* |
17
|
|
|
* @package CryptoManana\Core\Traits\Randomness |
18
|
|
|
* |
19
|
|
|
* @mixin RandomnessSpecification |
20
|
|
|
*/ |
21
|
|
|
trait ArbitraryBaseOutputTrait |
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 boolean. |
32
|
|
|
* |
33
|
|
|
* @return bool Randomly generated boolean value. |
34
|
|
|
* @throws \Exception Validation errors. |
35
|
|
|
*/ |
36
|
13 |
|
public function getBool() |
37
|
|
|
{ |
38
|
13 |
|
return $this->getInt() % 2 === 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Generate a random ternary format (-1, 0, 1). |
43
|
|
|
* |
44
|
|
|
* Note: Passing `false` to the `$asInteger` parameter will convert values to `null`, `false` and `true`. |
45
|
|
|
* |
46
|
|
|
* @param bool|int $asInteger Flag for returning as integer (default => true). |
47
|
|
|
* |
48
|
|
|
* @return bool|int Randomly generated ternary value. |
49
|
|
|
* @throws \Exception Validation errors. |
50
|
|
|
*/ |
51
|
3 |
|
public function getTernary($asInteger = true) |
52
|
|
|
{ |
53
|
3 |
|
$ternary = $this->getInt(-1, 1); |
54
|
|
|
|
55
|
3 |
|
if ($asInteger) { |
56
|
3 |
|
return $ternary; |
57
|
|
|
} else { |
58
|
|
|
switch ($ternary) { |
59
|
3 |
|
case 1: |
60
|
2 |
|
return true; |
61
|
|
|
case -1: |
62
|
3 |
|
return null; |
63
|
|
|
default: // case 0: |
64
|
3 |
|
return false; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Generate a random HEX string. |
71
|
|
|
* |
72
|
|
|
* @param int $length The output string length (default => 1). |
73
|
|
|
* @param bool $upperCase Flag for using uppercase output (default => false). |
74
|
|
|
* |
75
|
|
|
* @return string Randomly generated HEX string. |
76
|
|
|
* @throws \Exception Validation errors. |
77
|
|
|
*/ |
78
|
4 |
|
public function getHex($length = 1, $upperCase = false) |
79
|
|
|
{ |
80
|
4 |
|
$hexString = bin2hex($this->getBytes($length)); |
81
|
|
|
|
82
|
4 |
|
return ($upperCase) ? StringBuilder::stringToUpper($hexString) : $hexString; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Generate a random Base64 string. |
87
|
|
|
* |
88
|
|
|
* @param int $length The internal byte string length (default => 1). |
89
|
|
|
* @param bool $urlFriendly Flag for using URL friendly output (default => false). |
90
|
|
|
* |
91
|
|
|
* @return string Randomly generated Base64 RFC 4648 standard string. |
92
|
|
|
* @throws \Exception Validation errors. |
93
|
|
|
*/ |
94
|
3 |
|
public function getBase64($length = 1, $urlFriendly = false) |
95
|
|
|
{ |
96
|
3 |
|
$base64 = base64_encode($this->getBytes($length)); |
97
|
|
|
|
98
|
3 |
|
if ($urlFriendly) { |
99
|
3 |
|
return StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], $base64); |
100
|
|
|
} else { |
101
|
3 |
|
return $base64; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|