|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Benrowe\Laravel\Config; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Facilitates the conversion of hierarchical data into a flat structure |
|
7
|
|
|
* |
|
8
|
|
|
* @package Benrowe\Laravel\Config |
|
9
|
|
|
*/ |
|
10
|
|
|
trait StorageConverterTrait |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Converts the flat key/value from the storage engine |
|
14
|
|
|
* to a heirachy structure based on the key sytax |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $data |
|
17
|
|
|
* @return array |
|
18
|
|
|
*/ |
|
19
|
|
|
private function dataDecode($data) |
|
20
|
|
|
{ |
|
21
|
|
|
// preprocess the keys into a unique list where the array values are |
|
22
|
|
|
// stored against the same key |
|
23
|
|
|
|
|
24
|
|
|
$data = $this->unpackArray($data); |
|
25
|
|
|
|
|
26
|
|
|
$newData = []; |
|
27
|
|
|
foreach ($data as $key => $value) { |
|
28
|
|
|
$this->arrHelper->set($newData, $key, $value); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $newData; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* unpack the keys that are structured for arrays so that they no |
|
36
|
|
|
* longer have the [] syntax at the end. Rather they're now a proper |
|
37
|
|
|
* array. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $data [description] |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
private function unpackArray($data) |
|
43
|
|
|
{ |
|
44
|
|
|
$arrKeys = array_filter($data, function ($val) { |
|
45
|
|
|
return preg_match(self::ARRAY_PATTERN, $val); |
|
46
|
|
|
}); |
|
47
|
|
|
foreach ($arrKeys as $key => $value) { |
|
48
|
|
|
$newKey = preg_replace(self::ARRAY_PATTERN, '', $key); |
|
49
|
|
|
if (!isset($data[$newKey])) { |
|
50
|
|
|
$data[$newKey] = []; |
|
51
|
|
|
} |
|
52
|
|
|
$data[$newKey][] = $value; |
|
53
|
|
|
unset($data[$key]); |
|
54
|
|
|
} |
|
55
|
|
|
return $data; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Flatten a multi-dimensional array into a linear key/value list |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $data |
|
62
|
|
|
* @param string|null $prefix |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
private function dataEncode($data, $prefix = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$newData = []; |
|
68
|
|
|
foreach ($data as $key => $value) { |
|
69
|
|
|
if (is_array($value)) { |
|
70
|
|
|
$newData = array_merge( |
|
71
|
|
|
$newData, |
|
72
|
|
|
$this->encodeArray($key, $value, $prefix) |
|
73
|
|
|
); |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
$newData[$prefix.$key] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
return $newData; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Encode the array of values against the provided key |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $key |
|
85
|
|
|
* @param array $value either an associative or keyed array |
|
86
|
|
|
* @param string|null $prefix |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
private function encodeArray($key, array $value, $prefix = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$data = []; |
|
92
|
|
|
if (!$this->arrHelper->isAssoc($value)) { |
|
93
|
|
|
foreach ($value as $index => $val) { |
|
94
|
|
|
$data[$prefix.$key.'['.$index.']'] = $val; |
|
95
|
|
|
} |
|
96
|
|
|
return $data; |
|
97
|
|
|
} |
|
98
|
|
|
return $this->dataEncode($value, $prefix.$key.self::KEY_DELIMITER); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: