1 | <?php |
||
13 | class Config implements Repository |
||
14 | { |
||
15 | /** |
||
16 | * @var string The delimiter used in the array keys to specify the heirachy |
||
17 | */ |
||
18 | const KEY_DELIMITER = '.'; |
||
19 | |||
20 | /** |
||
21 | * @var string the pattern to match array keys |
||
22 | */ |
||
23 | const ARRAY_PATTERN = "/\[([0-9]+)\]$/"; |
||
24 | |||
25 | /** |
||
26 | * The configuration data |
||
27 | * @var array |
||
28 | */ |
||
29 | private $data; |
||
30 | |||
31 | /** |
||
32 | * constructor |
||
33 | * The initial data |
||
34 | * |
||
35 | * @param array $data the flattened data |
||
36 | */ |
||
37 | public function __construct($data) |
||
38 | { |
||
39 | $this->data = $this->dataDecode($data); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Reduce the configuration to a simple key/value array, despite the heirachy |
||
44 | * of information |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | public function flatten() |
||
49 | { |
||
50 | return $this->dataEncode($this->data); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Create/Update a configuration value |
||
55 | * |
||
56 | * @param string $key |
||
57 | * @param mixed $value |
||
58 | */ |
||
59 | public function set($key, $value = null) |
||
60 | { |
||
61 | Arr::set($this->data, $key, $value); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the configuration value based on it's key |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @param mixed $default |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function get($key, $default = null) |
||
72 | { |
||
73 | return Arr::get($this->data, $key, $default); |
||
74 | } |
||
75 | |||
76 | /*** |
||
77 | * Get all of the configuration data in it's hierarchical state |
||
78 | */ |
||
79 | public function all() |
||
80 | { |
||
81 | return $this->data; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * From an item from the configuration |
||
86 | * |
||
87 | * @param string $key |
||
88 | * @return boolean |
||
|
|||
89 | */ |
||
90 | public function forget($key) |
||
94 | |||
95 | /** |
||
96 | * Clear all of the settings from the configuration |
||
97 | * |
||
98 | * @return boolean |
||
99 | */ |
||
100 | public function clear() |
||
108 | |||
109 | /** |
||
110 | * Check if a configuration setting exists |
||
111 | * |
||
112 | * @param string $key |
||
113 | * @return boolean |
||
114 | */ |
||
115 | public function has($key) |
||
119 | |||
120 | /** |
||
121 | * Prepend a value onto the key. |
||
122 | * |
||
123 | * If that existing key is not an array it will be converted into an array |
||
124 | * and the the value will be the first element of the array |
||
125 | * |
||
126 | * @param string $key |
||
127 | * @param mixed $value |
||
128 | * @return void |
||
129 | */ |
||
130 | public function prepend($key, $value) |
||
136 | |||
137 | /** |
||
138 | * Push a value onto the key |
||
139 | * |
||
140 | * If that existing key is not an array it will be converted into an array |
||
141 | * and the the value will be the first element of the array |
||
142 | * |
||
143 | * @param string $key |
||
144 | * @param mixed $value |
||
145 | * @return void |
||
146 | */ |
||
147 | public function push($key, $value) |
||
153 | |||
154 | /** |
||
155 | * Get the value, as an array |
||
156 | * |
||
157 | * @param string $key |
||
158 | * @return array any existing value will be converted to the first element |
||
159 | * of the array |
||
160 | */ |
||
161 | private function getAsArray($key) |
||
169 | |||
170 | /** |
||
171 | * Converts the flat key/value from the storage engine |
||
172 | * to a heirachy structure based on the key sytax |
||
173 | * |
||
174 | * @param array $data |
||
175 | * @return array |
||
176 | */ |
||
177 | private function dataDecode($data) |
||
191 | |||
192 | /** |
||
193 | * unpack the keys that are structured for arrays so that they no |
||
194 | * longer have the [] syntax at the end. Rather they're now a proper |
||
195 | * array. |
||
196 | * |
||
197 | * @param array $data [description] |
||
198 | * @return array |
||
199 | */ |
||
200 | private function unpackArray($data) |
||
215 | |||
216 | /** |
||
217 | * Flatten a multi-dimensional array into a linear key/value list |
||
218 | * |
||
219 | * @param array $data |
||
220 | * @return array |
||
221 | */ |
||
222 | private function dataEncode($data, $prefix = null) |
||
234 | |||
235 | /** |
||
236 | * Encode the array of values against the provided key |
||
237 | * |
||
238 | * @param string $key |
||
239 | * @param array $value either an associative or keyed array |
||
240 | * @param string $prefix |
||
241 | * @return array |
||
242 | */ |
||
243 | private function encodeArray($key, array $value, $prefix = null) |
||
254 | } |
||
255 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.