|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Framework; |
|
3
|
|
|
|
|
4
|
|
|
final class ArrayStorage |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* Parse the setting key to make sure it's a simple string |
|
8
|
|
|
* or an array. |
|
9
|
|
|
*/ |
|
10
|
|
|
final private static function parseSetting($setting) |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
if (is_string($setting) && |
|
13
|
|
|
false !== strpos($setting, \WebServCo\Framework\Settings::DIVIDER)) { |
|
14
|
|
|
return explode(\WebServCo\Framework\Settings::DIVIDER, $setting); |
|
15
|
|
|
} |
|
16
|
|
|
return $setting; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Retrieve a value from a storage array. |
|
21
|
|
|
* |
|
22
|
|
|
* @param array $storage |
|
23
|
|
|
* @param mixed $setting Can be an array, a string, |
|
24
|
|
|
* or a special formatted string |
|
25
|
|
|
* (eg 'app|path|project'). |
|
26
|
|
|
* @param mixed $defaultValue |
|
27
|
|
|
* @return mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
final public static function get($storage = [], $setting = null, $defaultValue = false) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
$setting = self::parseSetting($setting); |
|
32
|
|
|
|
|
33
|
|
|
if (empty($setting) || empty($storage)) { |
|
34
|
|
|
return $defaultValue; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* If $setting is an array, process it recursively. |
|
39
|
|
|
*/ |
|
40
|
|
|
if (is_array($setting)) { |
|
41
|
|
|
/** |
|
42
|
|
|
* Check if we have the first $setting element in the |
|
43
|
|
|
* configuration data array. |
|
44
|
|
|
*/ |
|
45
|
|
|
if (array_key_exists(0, $setting) && array_key_exists($setting[0], $storage)) { |
|
46
|
|
|
/** |
|
47
|
|
|
* Remove first element from $setting. |
|
48
|
|
|
*/ |
|
49
|
|
|
$key = array_shift($setting); |
|
50
|
|
|
/** |
|
51
|
|
|
* At the end of the recursion $setting will be |
|
52
|
|
|
* an empty array. In this case we simply return the |
|
53
|
|
|
* current configuration data. |
|
54
|
|
|
*/ |
|
55
|
|
|
if (empty($setting)) { |
|
56
|
|
|
return $storage[$key]; |
|
57
|
|
|
} |
|
58
|
|
|
/** |
|
59
|
|
|
* Go down one lement in the configuration data |
|
60
|
|
|
* and call the method again, with the remainig setting. |
|
61
|
|
|
*/ |
|
62
|
|
|
return self::get($storage[$key], $setting, $defaultValue); |
|
63
|
|
|
} |
|
64
|
|
|
/** |
|
65
|
|
|
* The requested setting doesn't exist in our |
|
66
|
|
|
* configuration data array. |
|
67
|
|
|
*/ |
|
68
|
|
|
return $defaultValue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* If we arrive here, $setting must be a simple string. |
|
73
|
|
|
*/ |
|
74
|
|
|
if (array_key_exists($setting, $storage)) { |
|
75
|
|
|
return $storage[$setting]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* If we got this far, there is no data to return. |
|
80
|
|
|
*/ |
|
81
|
|
|
return $defaultValue; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets a value in a storage array. |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $storage |
|
88
|
|
|
* @param mixed $setting Can be an array, a string, |
|
89
|
|
|
* or a special formatted string |
|
90
|
|
|
* (eg 'app|path|project'). |
|
91
|
|
|
* @param mixed $value The value to be stored. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array The storage array with new data. |
|
94
|
|
|
*/ |
|
95
|
|
|
final public static function set($storage, $setting, $value) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
if (!is_array($storage) || empty($setting)) { |
|
98
|
|
|
return $storage; |
|
99
|
|
|
} |
|
100
|
|
|
$setting = self::parseSetting($setting); |
|
101
|
|
|
if (is_array($setting)) { |
|
102
|
|
|
$reference = &$storage; |
|
103
|
|
|
foreach ($setting as $item) { |
|
104
|
|
|
$reference = &$reference[$item]; |
|
105
|
|
|
} |
|
106
|
|
|
$reference = $value; |
|
107
|
|
|
unset($reference); |
|
108
|
|
|
return $storage; |
|
109
|
|
|
} |
|
110
|
|
|
$storage[$setting] = $value; |
|
111
|
|
|
return $storage; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Append data to a storage array. |
|
116
|
|
|
* |
|
117
|
|
|
* @param array $storage |
|
118
|
|
|
* @param mixed $data |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
final public static function append($storage, $data = []) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
if (is_array($storage) && is_array($data)) { |
|
124
|
|
|
foreach ($data as $setting => $value) { |
|
125
|
|
|
if (array_key_exists($setting, $storage) && |
|
126
|
|
|
is_array($storage[$setting]) && |
|
127
|
|
|
is_array($value) |
|
128
|
|
|
) { |
|
129
|
|
|
$storage[$setting] = self::append($storage[$setting], $value); |
|
130
|
|
|
} else { |
|
131
|
|
|
$storage[$setting] = $value; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
return $storage; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|