|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Jim Martens <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace TwoMartens\Bundle\CoreBundle\Util; |
|
11
|
|
|
|
|
12
|
|
|
use TwoMartens\Bundle\CoreBundle\Model\Option; |
|
13
|
|
|
use TwoMartens\Bundle\CoreBundle\Model\OptionCategory; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Provides utility methods for config-related things. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jim Martens <[email protected]> |
|
19
|
|
|
* @copyright 2013-2015 Jim Martens |
|
20
|
|
|
*/ |
|
21
|
|
|
class ConfigUtil |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Converts an array of config values into their option representation. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $configData |
|
27
|
|
|
* |
|
28
|
|
|
* @return OptionCategory |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function convertToOptions($configData) |
|
31
|
|
|
{ |
|
32
|
|
|
$returnData = new OptionCategory(); |
|
33
|
|
|
$categories = []; |
|
34
|
|
|
|
|
35
|
|
|
foreach ($configData as $category => $values) { |
|
36
|
|
|
$optionCategory = new OptionCategory($category); |
|
37
|
|
|
$subCategory = self::convertValues($category, $values); |
|
38
|
|
|
$subOptions = $subCategory->getOptions(); |
|
39
|
|
|
$subCategories = $subCategory->getCategories(); |
|
40
|
|
|
$optionCategory->setOptions($subOptions); |
|
41
|
|
|
$optionCategory->setCategories($subCategories); |
|
42
|
|
|
$categories[] = $optionCategory; |
|
43
|
|
|
} |
|
44
|
|
|
$returnData->setCategories($categories); |
|
45
|
|
|
|
|
46
|
|
|
return $returnData; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Converts the given name and value into an Option. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param mixed $value |
|
54
|
|
|
* |
|
55
|
|
|
* @return Option |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function convertToOption($name, $value) |
|
58
|
|
|
{ |
|
59
|
|
|
$type = str_replace('double', 'float', gettype($value)); |
|
60
|
|
|
$type = str_replace('NULL', 'null', $type); |
|
61
|
|
|
$option = new Option( |
|
62
|
|
|
0, |
|
63
|
|
|
$name, |
|
64
|
|
|
$type, |
|
65
|
|
|
$value |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
return $option; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Converts a top level category into the YAML representation. |
|
73
|
|
|
* |
|
74
|
|
|
* @param OptionCategory $optionCategory |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function convertToArray(OptionCategory $optionCategory) |
|
79
|
|
|
{ |
|
80
|
|
|
$result = []; |
|
81
|
|
|
$categories = $optionCategory->getCategories(); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($categories as $category) { |
|
84
|
|
|
$options = $category->getOptions(); |
|
85
|
|
|
$_categories = $category->getCategories(); |
|
86
|
|
|
|
|
87
|
|
|
$optionsArray = self::getOptionsArray($options); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($_categories as $_category) { |
|
90
|
|
|
$_options = $_category->getOptions(); |
|
91
|
|
|
$_optionsArray = self::getOptionsArray($_options); |
|
92
|
|
|
$optionsArray[$_category->getName()] = $_optionsArray; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$result[$category->getName()] = $optionsArray; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $result; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns an array of options. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Option[] $options |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
private static function getOptionsArray($options) |
|
109
|
|
|
{ |
|
110
|
|
|
$optionsArray = []; |
|
111
|
|
|
foreach ($options as $option) { |
|
112
|
|
|
$optionsArray[$option->getName()] = $option->getValue(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $optionsArray; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Converts the given values into options. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $key |
|
122
|
|
|
* @param array|null $values |
|
123
|
|
|
* |
|
124
|
|
|
* @return OptionCategory |
|
125
|
|
|
*/ |
|
126
|
|
|
private static function convertValues($key, $values) |
|
127
|
|
|
{ |
|
128
|
|
|
$returnCategory = new OptionCategory(); |
|
129
|
|
|
|
|
130
|
|
|
if ($values === null) { |
|
131
|
|
|
return $returnCategory; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (self::isAssoc($values)) { |
|
135
|
|
|
$options = []; |
|
136
|
|
|
$categories = []; |
|
137
|
|
|
foreach ($values as $_key => $value) { |
|
138
|
|
|
// case: value = [...] |
|
|
|
|
|
|
139
|
|
|
if (is_array($value)) { |
|
140
|
|
|
// recursive call |
|
141
|
|
|
$subCategory = self::convertValues($_key, $value); |
|
142
|
|
|
$subOptions = $subCategory->getOptions(); |
|
143
|
|
|
$subCategories = $subCategory->getCategories(); |
|
144
|
|
|
|
|
145
|
|
|
// case: value = [a, b, c] |
|
|
|
|
|
|
146
|
|
|
if (count($subOptions) == 1 && empty($subCategories) && !self::isAssoc($value)) { |
|
147
|
|
|
$options[] = $subOptions[0]; |
|
148
|
|
|
// case: value = [[a: b], [c: d, e: f]] || [a: b, c: d, e: [...]] |
|
|
|
|
|
|
149
|
|
|
} else { |
|
150
|
|
|
$category = new OptionCategory($_key); |
|
151
|
|
|
$category->setOptions($subOptions); |
|
152
|
|
|
$category->setCategories($subCategories); |
|
153
|
|
|
$categories[] = $category; |
|
154
|
|
|
} |
|
155
|
|
|
// case: value = b |
|
156
|
|
|
} else { |
|
157
|
|
|
$type = str_replace('double', 'float', gettype($value)); |
|
158
|
|
|
$type = str_replace('NULL', 'null', $type); |
|
159
|
|
|
$options[] = new Option( |
|
160
|
|
|
0, |
|
161
|
|
|
$_key, |
|
162
|
|
|
$type, |
|
163
|
|
|
$value |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
$returnCategory->setOptions($options); |
|
168
|
|
|
$returnCategory->setCategories($categories); |
|
169
|
|
|
} else { |
|
170
|
|
|
$options = []; |
|
171
|
|
|
$_values = []; |
|
172
|
|
|
foreach ($values as $_key => $value) { |
|
173
|
|
|
if (is_array($value)) { |
|
174
|
|
|
$subCategory = self::convertValues($_key, $value); |
|
175
|
|
|
$subOptions = $subCategory->getOptions(); |
|
176
|
|
|
$subCategories = $subCategory->getCategories(); |
|
177
|
|
|
// case: value = [a: b] |
|
|
|
|
|
|
178
|
|
|
if (count($subOptions) == 1 && empty($subCategories)) { |
|
179
|
|
|
$option = $subOptions[0]; |
|
180
|
|
|
$options[] = $option; |
|
181
|
|
|
// case: value = [a: b, d: e] |
|
182
|
|
|
} elseif (count($subOptions) > 1) { |
|
183
|
|
|
$option = new Option(0, '', 'array'); |
|
184
|
|
|
$optionValues = []; |
|
185
|
|
|
foreach ($subOptions as $subOption) { |
|
186
|
|
|
$optionValues[$subOption->getName()] = $subOption->getValue(); |
|
187
|
|
|
} |
|
188
|
|
|
$option->setValue($optionValues); |
|
189
|
|
|
$options[] = $option; |
|
190
|
|
|
} |
|
191
|
|
|
} else { |
|
192
|
|
|
$_values[] = $value; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
// case: values = [a, b, c, d] |
|
196
|
|
|
if (!empty($_values)) { |
|
197
|
|
|
$options[] = new Option(0, $key, 'array', $_values); |
|
198
|
|
|
} |
|
199
|
|
|
$returnCategory->setOptions($options); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $returnCategory; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Checks, if the array is associative or not. |
|
207
|
|
|
* |
|
208
|
|
|
* @param array $array |
|
209
|
|
|
* |
|
210
|
|
|
* @return bool |
|
211
|
|
|
*/ |
|
212
|
|
|
private static function isAssoc($array) |
|
213
|
|
|
{ |
|
214
|
|
|
return (bool) count(array_filter(array_keys($array), 'is_string')); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.