1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Factory object for pseudo-randomness generator instancing. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Factories; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern; |
10
|
|
|
use \CryptoManana\Core\Abstractions\Randomness\AbstractRandomness as RandomnessSource; |
11
|
|
|
use \CryptoManana\Randomness\QuasiRandom as QuasiRandomness; |
12
|
|
|
use \CryptoManana\Randomness\PseudoRandom as PseudoRandomness; |
13
|
|
|
use \CryptoManana\Randomness\CryptoRandom as CryptographyRandomness; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class RandomnessFactory - Factory object for pseudo-randomness generator instancing. |
17
|
|
|
* |
18
|
|
|
* @package CryptoManana\Factories |
19
|
|
|
*/ |
20
|
|
|
class RandomnessFactory extends FactoryPattern |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The quasi-random generator type. |
24
|
|
|
*/ |
25
|
|
|
const QUASI_SOURCE = QuasiRandomness::class; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The pseudo-random generator type. |
29
|
|
|
*/ |
30
|
|
|
const PSEUDO_SOURCE = PseudoRandomness::class; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The cryptographically secure pseudo-random generator type. |
34
|
|
|
*/ |
35
|
|
|
const CRYPTO_SOURCE = CryptographyRandomness::class; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a pseudo-randomness generator. |
39
|
|
|
* |
40
|
|
|
* @param string|null $type The generator class name as type for creation. |
41
|
|
|
* |
42
|
|
|
* @return RandomnessSource|object|null An exception object or null. |
43
|
|
|
*/ |
44
|
3 |
|
public function create($type) |
45
|
|
|
{ |
46
|
3 |
|
return self::createInstance($type); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create a pseudo-randomness generator. |
51
|
|
|
* |
52
|
|
|
* @param string|null $type The generator class name as type for creation. |
53
|
|
|
* |
54
|
|
|
* @return RandomnessSource|object|null An exception object or null. |
55
|
|
|
*/ |
56
|
4 |
|
public static function createInstance($type) |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* Check if class exists and has a correct base class |
60
|
|
|
* |
61
|
|
|
* @var RandomnessSource|null $exception Object instance. |
62
|
|
|
*/ |
63
|
4 |
|
if (class_exists($type) && is_subclass_of($type, RandomnessSource::class)) { |
64
|
4 |
|
$exception = new $type(); |
65
|
|
|
} else { |
66
|
2 |
|
$exception = null; // Invalid exception type given |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
return $exception; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get debug information for the class instance. |
74
|
|
|
* |
75
|
|
|
* @return array Debug information. |
76
|
|
|
*/ |
77
|
1 |
|
public function __debugInfo() |
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
1 |
|
self::class . '::QUASI_SOURCE' => self::QUASI_SOURCE, |
81
|
1 |
|
self::class . '::PSEUDO_SOURCE' => self::PSEUDO_SOURCE, |
82
|
1 |
|
self::class . '::CRYPTO_SOURCE' => self::CRYPTO_SOURCE, |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|