1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediadevs\Validator\Helpers; |
4
|
|
|
|
5
|
|
|
class Registry extends Singleton |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* All the registered classes will be stored in here |
9
|
|
|
* @var array |
10
|
|
|
*/ |
11
|
|
|
private static $registry = array(); |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Registering the class in the $data array |
15
|
|
|
* @param array $registries |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public static function register(array $registries): void |
20
|
|
|
{ |
21
|
|
|
self::$registry = array_merge(self::$registry, $registries); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Collecting the registry based upon the given target |
26
|
|
|
* @param string $index |
27
|
|
|
* |
28
|
|
|
* @return string|void |
29
|
|
|
*/ |
30
|
|
|
public static function getRegistry(string $index): string |
31
|
|
|
{ |
32
|
|
|
$targetIndex = null; |
33
|
|
|
|
34
|
|
|
// Whether the $data array has the given index |
35
|
|
|
if (self::hasIndex($index)) { |
36
|
|
|
$targetIndex = $index; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// In this case the $data array has no index of $index, now we'll try to reverse search by value |
40
|
|
|
if (self::hasValue($index)) { |
41
|
|
|
$targetIndex = self::getIndexByValue($index); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return self::getValuesByIndex($targetIndex); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Validating whether the data contains the given index |
49
|
|
|
* @param string $index |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
public static function hasIndex(string $index): bool |
54
|
|
|
{ |
55
|
|
|
return (bool) array_key_exists($index, self::$registry) ? true : false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Validating whether the data contains the given value |
60
|
|
|
* @param string $value |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public static function hasValue(string $value): bool |
65
|
|
|
{ |
66
|
|
|
// Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
67
|
|
|
$reverseData = array_flip(self::$registry); |
68
|
|
|
|
69
|
|
|
return (bool) (isset($reverseData[$value])) ? true : false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Validating whether the given index has a value |
74
|
|
|
* @param string $index |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public static function indexHasValue(string $index): bool |
79
|
|
|
{ |
80
|
|
|
if (self::hasIndex($index)) { |
81
|
|
|
return (bool) !empty(self::$registry[$index]) ? true : false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return (bool) false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Collecting the index by the given value |
89
|
|
|
* @param string $value |
90
|
|
|
* |
91
|
|
|
* @return string|void |
92
|
|
|
*/ |
93
|
|
|
public static function getIndexByValue(string $value) |
94
|
|
|
{ |
95
|
|
|
if (self::hasValue($value)) { |
96
|
|
|
// Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
97
|
|
|
$reverseData = array_flip(self::$registry); |
98
|
|
|
|
99
|
|
|
return $reverseData[$value]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Collecting multiple values based on the given index |
105
|
|
|
* @param string $index |
106
|
|
|
* |
107
|
|
|
* @return array|void |
108
|
|
|
*/ |
109
|
|
|
public static function getValuesByIndex(string $index) |
110
|
|
|
{ |
111
|
|
|
if (self::hasIndex($index) && self::indexHasValue($index)) { |
112
|
|
|
return self::$registry[$index]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|