1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Abstraction container for dependency injection of pseudo-random generator services. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Abstractions\Containers; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Abstractions\Randomness\AbstractGenerator as RandomnessSource; |
10
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\SeedableContainerInterface as SeedableService; |
11
|
|
|
use \CryptoManana\Core\Interfaces\Randomness\SeedableGeneratorInterface as SeedableGenerator; |
12
|
|
|
use \CryptoManana\Core\Interfaces\Containers\RandomnessInjectableInterface as SetterInjectable; |
13
|
|
|
use \CryptoManana\Core\Traits\Containers\RandomnessInjectableTrait as SetterInjectableImplementation; |
14
|
|
|
use \CryptoManana\Randomness\CryptoRandom as DefaultRandomnessSource; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractRandomnessInjectable - Abstraction container for dependency injection of data generator services. |
18
|
|
|
* |
19
|
|
|
* @package CryptoManana\Core\Abstractions\Containers |
20
|
|
|
* |
21
|
|
|
* @mixin SetterInjectableImplementation |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractRandomnessInjectable implements SetterInjectable, SeedableService |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Dependency injection via setter method. |
27
|
|
|
* |
28
|
|
|
* {@internal Reusable implementation of `RandomnessInjectableInterface`. }} |
29
|
|
|
*/ |
30
|
|
|
use SetterInjectableImplementation; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The pseudo-random generator service property storage. |
34
|
|
|
* |
35
|
|
|
* @var RandomnessSource|null The pseudo-random generator service. |
36
|
|
|
*/ |
37
|
|
|
protected $randomnessSource = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Container constructor. |
41
|
|
|
* |
42
|
|
|
* @internal The default service is always the most secure one available. |
43
|
|
|
* |
44
|
|
|
* @param RandomnessSource|null $generator The pseudo-random generator service. |
45
|
|
|
* |
46
|
|
|
* @throws \Exception Initialization validation. |
47
|
|
|
*/ |
48
|
8 |
|
public function __construct(RandomnessSource $generator = null) |
49
|
|
|
{ |
50
|
8 |
|
if (is_null($generator)) { |
51
|
6 |
|
$this->randomnessSource = new DefaultRandomnessSource(); |
52
|
|
|
} else { |
53
|
2 |
|
$this->randomnessSource = $generator; |
54
|
|
|
} |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Container destructor. |
59
|
|
|
*/ |
60
|
8 |
|
public function __destruct() |
61
|
|
|
{ |
62
|
8 |
|
unset($this->randomnessSource); |
63
|
8 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Container cloning via deep copy. |
67
|
|
|
*/ |
68
|
1 |
|
public function __clone() |
69
|
|
|
{ |
70
|
1 |
|
$this->randomnessSource = clone $this->randomnessSource; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Pass a seed value to the pseudo-randomness generator service. |
75
|
|
|
* |
76
|
|
|
* @param null|int $seed Seed value in integer format or null for auto-seeding. |
77
|
|
|
* |
78
|
|
|
* @return $this The container object. |
79
|
|
|
* @throws \Exception Validation errors and service misuse. |
80
|
|
|
*/ |
81
|
3 |
|
public function seedRandomGenerator($seed = null) |
82
|
|
|
{ |
83
|
3 |
|
if ($this->randomnessSource instanceof SeedableGenerator) { |
84
|
|
|
/** |
85
|
|
|
* {@internal Back-ward comparability way of calling a static method (`::`) via dynamic operator (`->`). } |
86
|
|
|
*/ |
87
|
3 |
|
$this->randomnessSource->setSeed($seed); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|