1
|
|
|
<?php namespace SimpleHash; |
2
|
|
|
|
3
|
|
|
use SimpleHash\Container\HashContainer; |
4
|
|
|
use SimpleHash\Exception\SimpleHashException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Hash factory |
8
|
|
|
* |
9
|
|
|
* @package SimpleHash |
10
|
|
|
* @author Kai Hempel <[email protected]> |
11
|
|
|
* @copyright 2016 Kai Hempel <[email protected]> |
12
|
|
|
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License |
13
|
|
|
* @link https://www.kuweh.de/ |
14
|
|
|
* @since Class available since Release 1.0.0 |
15
|
|
|
*/ |
16
|
|
|
class HashFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Calculator parameter |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $calculatorParams = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param array $calculatorParams |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct(array $calculatorParams = []) |
31
|
|
|
{ |
32
|
5 |
|
$this->calculatorParams = $calculatorParams; |
33
|
5 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Magic factory method |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* @param array $arguments |
40
|
|
|
* @throws SimpleHashException |
41
|
|
|
*/ |
42
|
4 |
|
public function __call($name, $arguments) |
43
|
|
|
{ |
44
|
4 |
|
if ($this->isCalledGetter($name) === false) { |
45
|
1 |
|
throw SimpleHashException::make('Unsupported method "' . $name . '" called!'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Intialize variables |
49
|
|
|
|
50
|
3 |
|
$plainText = $this->getPlainStringFromArguments($arguments); |
51
|
|
|
|
52
|
|
|
// Initialize calculator |
53
|
|
|
|
54
|
3 |
|
$calculatorClass = $this->getCalculatorName($name); |
55
|
3 |
|
if (! $this->calculatorExists($calculatorClass)) { |
56
|
1 |
|
throw SimpleHashException::make('No calclator "' . $calculatorClass . '" exists!'); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$calculator = $this->initializeCalculator($calculatorClass); |
60
|
|
|
|
61
|
|
|
// Initialize hash container with hash string |
62
|
|
|
|
63
|
2 |
|
$container = new HashContainer(); |
64
|
2 |
|
$container->setHashString($calculator->getHash($plainText)); |
65
|
|
|
|
66
|
|
|
// Return hash container |
67
|
|
|
|
68
|
2 |
|
return $container; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if a getter method was called |
73
|
|
|
* |
74
|
|
|
* @param string $name |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
4 |
|
private function isCalledGetter($name) |
78
|
|
|
{ |
79
|
4 |
|
return substr($name, 0, 3) == 'get'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Builds the calculator class name string |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
private function getCalculatorName($name) |
89
|
|
|
{ |
90
|
3 |
|
$algorithmName = str_replace(['get', 'Hash'], ['', ''], $name); |
91
|
3 |
|
return '\SimpleHash\Calculator\\' . ucfirst(strtolower($algorithmName)) . 'Calculator'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Checks if the given class name exists |
96
|
|
|
* |
97
|
|
|
* @param string $calculatorClass |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
3 |
|
private function calculatorExists($calculatorClass) |
101
|
|
|
{ |
102
|
3 |
|
return class_exists($calculatorClass, true); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Extracts the string for hashing from the given argument array |
107
|
|
|
* |
108
|
|
|
* @param array $arguments |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
3 |
|
private function getPlainStringFromArguments($arguments) |
112
|
|
|
{ |
113
|
3 |
|
if (empty($arguments) || empty($arguments[0])) { |
114
|
1 |
|
return ''; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return (string)$arguments[0]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Initialize calculator object |
122
|
|
|
* |
123
|
|
|
* @param string $calculatorClass |
124
|
|
|
* @param array $params |
|
|
|
|
125
|
|
|
* @return \SimpleHash\Calculator\HashCalculatorInterface |
126
|
|
|
*/ |
127
|
2 |
|
private function initializeCalculator($calculatorClass) |
128
|
|
|
{ |
129
|
2 |
|
return new $calculatorClass($this->calculatorParams); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.