1 | <?php |
||
12 | class Config |
||
13 | { |
||
14 | /** |
||
15 | * @var string The delimiter used in the array keys to specify the heirachy |
||
16 | */ |
||
17 | const KEY_DELIMITER = '.'; |
||
18 | |||
19 | /** |
||
20 | * @var string the pattern to match array keys |
||
21 | */ |
||
22 | const ARRAY_PATTERN = "/\[([0-9]+)\]$/"; |
||
23 | |||
24 | /** |
||
25 | * The configuration data |
||
26 | * @var array |
||
27 | */ |
||
28 | private $data; |
||
29 | |||
30 | /** |
||
31 | * constructor |
||
32 | * The initial data |
||
33 | * |
||
34 | * @param array $data the flattened data |
||
35 | */ |
||
36 | public function __construct($data) |
||
40 | |||
41 | /** |
||
42 | * Reduce the configuration to a simple key/value array, despite the heirachy |
||
43 | * of information |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function flatten() |
||
51 | |||
52 | /** |
||
53 | * Create/Update a configuration value |
||
54 | * |
||
55 | * @param string $key |
||
56 | * @param mixed $value |
||
57 | */ |
||
58 | public function set($key, $value) |
||
62 | |||
63 | /** |
||
64 | * Get the configuration value based on it's key |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @param mixed $default |
||
68 | * @return mixed |
||
69 | */ |
||
70 | public function get($key, $default = null) |
||
74 | |||
75 | /** |
||
76 | * From an item from the configuration |
||
77 | * |
||
78 | * @param string $key |
||
79 | * @return boolean |
||
80 | */ |
||
81 | public function forget($key) |
||
85 | |||
86 | /** |
||
87 | * Clear all of the settings from the configuration |
||
88 | * |
||
89 | * @return boolean |
||
90 | */ |
||
91 | public function clear() |
||
99 | |||
100 | /** |
||
101 | * Check if a configuration setting exists |
||
102 | * |
||
103 | * @param string $key |
||
104 | * @return boolean |
||
105 | */ |
||
106 | public function exists($key) |
||
110 | |||
111 | /** |
||
112 | * Converts the flat key/value from the storage engine |
||
113 | * to a heirachy structure based on the key sytax |
||
114 | * |
||
115 | * @param array $data |
||
116 | * @return array |
||
117 | */ |
||
118 | private function dataDecode($data) |
||
132 | |||
133 | /** |
||
134 | * unpack the keys that are structured for arrays so that they no |
||
135 | * longer have the [] syntax at the end. Rather they're now a proper |
||
136 | * array. |
||
137 | * |
||
138 | * @param array $data [description] |
||
139 | * @return array |
||
140 | */ |
||
141 | private function unpackArray($data) |
||
156 | |||
157 | /** |
||
158 | * Flatten a multi-dimensional array into a linear key/value list |
||
159 | * |
||
160 | * @param array $data |
||
161 | * @return array |
||
162 | */ |
||
163 | private function dataEncode($data, $prefix = null) |
||
175 | |||
176 | /** |
||
177 | * Encode the array of values against the provided key |
||
178 | * |
||
179 | * @param string $key |
||
180 | * @param array $value either an associative or keyed array |
||
181 | * @param string $prefix |
||
182 | * @return array |
||
183 | */ |
||
184 | private function encodeArray($key, array $value, $prefix = null) |
||
195 | } |
||
196 |