|
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
|
|
|
* Magic factory method |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $name |
|
22
|
|
|
* @param array $arguments |
|
23
|
|
|
* @throws SimpleHashException |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function __call($name, $arguments) |
|
26
|
|
|
{ |
|
27
|
1 |
|
if ($this->isCalledGetter($name) === false) { |
|
28
|
|
|
throw SimpleHashException::make('Unsupported method "' . $name . '" called!'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
// Intialize variables |
|
32
|
|
|
|
|
33
|
1 |
|
$plainText = $this->getPlainStringFromArguments($arguments); |
|
34
|
1 |
|
$calculatorParams = $this->getCalculatorParamsFromArguments($arguments); |
|
35
|
|
|
|
|
36
|
|
|
// Initialize calculator |
|
37
|
|
|
|
|
38
|
1 |
|
$calculatorClass = $this->getCalculatorName($name); |
|
39
|
1 |
|
if ( ! $this->calculatorExists($calculatorClass)) { |
|
40
|
|
|
throw SimpleHashException::make('No calclator "' . $calculatorClass . '" exists!'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
$calculator = $this->initializeCalculator($calculatorClass, $calculatorParams); |
|
44
|
|
|
|
|
45
|
|
|
// Initialize hash container with hash string |
|
46
|
|
|
|
|
47
|
1 |
|
$container = new HashContainer(); |
|
48
|
1 |
|
$container->setHashString($calculator->getHash($plainText)); |
|
49
|
|
|
|
|
50
|
|
|
// Return hash container |
|
51
|
|
|
|
|
52
|
1 |
|
return $container; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Checks if a getter method was called |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
1 |
|
private function isCalledGetter($name) |
|
62
|
|
|
{ |
|
63
|
1 |
|
return substr($name, 0, 3) == 'get'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Builds the calculator class name string |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
1 |
|
private function getCalculatorName($name) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$algorithmName = str_replace(['get', 'Hash'], ['', ''], $name); |
|
75
|
1 |
|
return '\SimpleHash\Calculator\\' . ucfirst(strtolower($algorithmName)) . 'Calculator'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Checks if the given class name exists |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $calculatorClass |
|
82
|
|
|
* @return type |
|
83
|
|
|
*/ |
|
84
|
1 |
|
private function calculatorExists($calculatorClass) |
|
85
|
|
|
{ |
|
86
|
1 |
|
return class_exists($calculatorClass, true); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Extracts the string for hashing from the given argument array |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $arguments |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
1 |
|
private function getPlainStringFromArguments($arguments) |
|
96
|
|
|
{ |
|
97
|
1 |
|
if (empty($arguments) || empty($arguments[0])) { |
|
98
|
1 |
|
return ''; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $arguments[0]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Extracts the calculator parameter from given argument array |
|
106
|
|
|
* |
|
107
|
|
|
* @param array $arguments |
|
108
|
|
|
* @return type |
|
109
|
|
|
*/ |
|
110
|
1 |
|
private function getCalculatorParamsFromArguments($arguments) |
|
111
|
|
|
{ |
|
112
|
1 |
|
if (empty($arguments) || empty($arguments[1]) || ! is_array($arguments[1])) { |
|
113
|
1 |
|
return []; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $arguments[1]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Initialize calculator object |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $calculatorClass |
|
123
|
|
|
* @param array $params |
|
124
|
|
|
* @return \SimpleHash\Calculator\HashCalculatorInterface |
|
125
|
|
|
*/ |
|
126
|
1 |
|
private function initializeCalculator($calculatorClass, array $params) |
|
127
|
|
|
{ |
|
128
|
1 |
|
return new $calculatorClass($params); |
|
129
|
|
|
} |
|
130
|
|
|
} |