1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of the ability to switch between different variations of a digestion algorithm. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\MessageDigestion; |
8
|
|
|
|
9
|
|
|
use CryptoManana\Core\Interfaces\MessageDigestion\AlgorithmVariationInterface as VariationSwitchingSpecification; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait AlgorithmVariationTrait - Reusable implementation of `AlgorithmVariationInterface`. |
13
|
|
|
* |
14
|
|
|
* @see \CryptoManana\Core\Interfaces\MessageDigestion\AlgorithmVariationInterface The abstract specification. |
15
|
|
|
* |
16
|
|
|
* @package CryptoManana\Core\Traits\MessageDigestion |
17
|
|
|
* |
18
|
|
|
* @property int $algorithmVariation The internal algorithm variation property storage. |
19
|
|
|
* |
20
|
|
|
* @mixin VariationSwitchingSpecification |
21
|
|
|
* |
22
|
|
|
* @codeCoverageIgnore |
23
|
|
|
*/ |
24
|
|
|
trait AlgorithmVariationTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Internal method for version range validation. |
28
|
|
|
* |
29
|
|
|
* @param int $version The version for validation checks. |
30
|
|
|
* |
31
|
|
|
* @throws \Exception Validation errors. |
32
|
|
|
*/ |
33
|
|
|
abstract protected function validateVersion($version); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Setter for the algorithm variation version property. |
37
|
|
|
* |
38
|
|
|
* @param int $version The algorithm variation version. |
39
|
|
|
* |
40
|
|
|
* @return $this The hash algorithm object. |
41
|
|
|
* @throws \Exception Validation errors. |
42
|
|
|
*/ |
43
|
|
|
public function setAlgorithmVariation($version) |
44
|
|
|
{ |
45
|
|
|
$version = filter_var( |
46
|
|
|
$version, |
47
|
|
|
FILTER_VALIDATE_INT, |
48
|
|
|
[ |
49
|
|
|
"options" => [ |
50
|
|
|
"min_range" => static::VERSION_ONE, |
51
|
|
|
"max_range" => static::VERSION_TWO, |
52
|
|
|
], |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
if ($version === false) { |
57
|
|
|
throw new \InvalidArgumentException( |
58
|
|
|
'The algorithm variation version must be a valid integer between ' . |
59
|
|
|
static::VERSION_ONE . ' and ' . static::VERSION_TWO . '.' |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->validateVersion($version); |
64
|
|
|
|
65
|
|
|
$this->algorithmVariation = $version; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Getter for the algorithm variation version property. |
72
|
|
|
* |
73
|
|
|
* @return int The algorithm variation version. |
74
|
|
|
*/ |
75
|
|
|
public function getAlgorithmVariation() |
76
|
|
|
{ |
77
|
|
|
return $this->algorithmVariation; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|