|
1
|
|
|
<?php namespace SimpleHash; |
|
2
|
|
|
|
|
3
|
|
|
use SimpleHash\Exception\SimpleHashException; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Hash facade |
|
7
|
|
|
* |
|
8
|
|
|
* @package SimpleHash |
|
9
|
|
|
* @author Kai Hempel <[email protected]> |
|
10
|
|
|
* @copyright 2016 Kai Hempel <[email protected]> |
|
11
|
|
|
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License |
|
12
|
|
|
* @link https://www.kuweh.de/ |
|
13
|
|
|
* @since Class available since Release 1.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Hash |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Facade interface method |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $name |
|
21
|
|
|
* @param array $arguments |
|
22
|
|
|
* @return \SimpleHash\Container\HashContainer |
|
23
|
|
|
* @throws \SimpleHash\Exception\SimpleHashException |
|
24
|
|
|
*/ |
|
25
|
6 |
|
public static function __callStatic($name, $arguments) |
|
26
|
|
|
{ |
|
27
|
6 |
|
$factory = new HashFactory(self::getCalclatorParams($arguments)); |
|
28
|
|
|
|
|
29
|
|
|
// Check if the requested algorithm is implemented |
|
30
|
|
|
|
|
31
|
6 |
|
if (! $factory->hasCalculator($name)) { |
|
32
|
1 |
|
throw SimpleHashException::make('Algorithm "' . $name . '" is not implemented yet!'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// Build hash container |
|
36
|
|
|
|
|
37
|
5 |
|
$getterMethod = self::getFactoryMethodName($name); |
|
38
|
5 |
|
return $factory->$getterMethod(self::getPlainTextString($arguments)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns the plain text string from the arguments |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $arguments |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
5 |
|
private static function getPlainTextString(array $arguments) |
|
48
|
|
|
{ |
|
49
|
5 |
|
return (empty($arguments[0])) ? '' : (string)$arguments[0]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns the optional calculator parameter from the arguments |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $arguments |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
6 |
|
private static function getCalclatorParams(array $arguments) |
|
59
|
|
|
{ |
|
60
|
6 |
|
return (! empty($arguments[1]) && is_array($arguments[1])) ? $arguments[1] : []; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Returns the factory hash getter name |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
5 |
|
private static function getFactoryMethodName($name) |
|
70
|
|
|
{ |
|
71
|
5 |
|
return 'get' . ucfirst(strtolower($name)) . 'Hash'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|