|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace L10nBundle\Utils\Helper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Provide array manipulation method |
|
7
|
|
|
*/ |
|
8
|
|
|
class L10nCatalogueHelper |
|
9
|
|
|
{ |
|
10
|
|
|
const LOCALE_KEY = 'locale'; |
|
11
|
|
|
const VALUE_KEY = 'value'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Transforms the config into a two dimensional array where a final element is a boolean, string or numeric value or |
|
15
|
|
|
* an array of translations. |
|
16
|
|
|
* This is done by grouping first keys with dots in order to create |
|
17
|
|
|
* the idResource. The idLocalization is kept. |
|
18
|
|
|
* |
|
19
|
|
|
* It drops the elements that do not have the good structure. |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $config |
|
22
|
|
|
* @param string $prefixKey |
|
23
|
|
|
* |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function createCatalogue($config, $prefixKey = null) |
|
27
|
|
|
{ |
|
28
|
5 |
|
$flatArray = array(); |
|
29
|
|
|
|
|
30
|
5 |
|
$configArrayDimension = $this->configLevel($config); |
|
31
|
|
|
|
|
32
|
5 |
|
if (0 === $configArrayDimension) { |
|
33
|
3 |
|
return $flatArray; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
if (1 === $configArrayDimension) { |
|
37
|
2 |
|
return $this->createResource($flatArray, $config, $prefixKey); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
foreach ($config as $key => $subArray) { |
|
41
|
2 |
|
$key = $prefixKey ? $prefixKey . '.' . $key : strval($key); |
|
42
|
2 |
|
$flatArray = array_merge($flatArray, $this->createCatalogue($subArray, $key)); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
return $flatArray; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the level of the given config. |
|
50
|
|
|
* The level is 0 if the config is a leaf. |
|
51
|
|
|
* The level of an array is the max of the level of its elements plus one. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $config |
|
54
|
|
|
* |
|
55
|
|
|
* @return integer |
|
56
|
|
|
*/ |
|
57
|
5 |
|
private function configLevel($config) |
|
58
|
|
|
{ |
|
59
|
5 |
|
if (!$this->isLeaf($config)) { |
|
60
|
2 |
|
return max(array_map(array($this, 'configLevel'), $config)) + 1; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
5 |
|
return 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns true if the given config is a leaf. |
|
68
|
|
|
* A leaf is either a boolean, string or numeric value or a non associative array. |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $config |
|
71
|
|
|
* |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
*/ |
|
74
|
5 |
|
private function isLeaf($config) |
|
75
|
|
|
{ |
|
76
|
5 |
|
if (is_array($config)) { |
|
77
|
4 |
|
$associativeKeysCount = count(array_filter(array_keys($config), 'is_string')); |
|
78
|
|
|
|
|
79
|
4 |
|
return $associativeKeysCount === 0; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $flatArray |
|
87
|
|
|
* @param array $config |
|
88
|
|
|
* @param string $prefixKey |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
2 |
|
private function createResource(array $flatArray, array $config, $prefixKey) |
|
93
|
|
|
{ |
|
94
|
2 |
|
if (null !== $prefixKey) { |
|
95
|
2 |
|
$resourceArray = $this->formatResource($config); |
|
96
|
2 |
|
if (count($resourceArray) > 0) { |
|
97
|
1 |
|
$flatArray[$prefixKey] = $resourceArray; |
|
98
|
1 |
|
} |
|
99
|
2 |
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return $flatArray; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Formats the given array to be in the format of a catalogue resource. |
|
106
|
|
|
* A catalogue resource is the value (array) corresponding to a reource id. |
|
107
|
|
|
* The format is an associative array of value. |
|
108
|
|
|
* A value is either a boolean, string or numeric value or an associative array where each value is a boolean, |
|
109
|
|
|
* string or numeric value. |
|
110
|
|
|
* |
|
111
|
|
|
* The value that cannot be matched in this format are dropped. |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $rawResourceArray |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
2 |
|
private function formatResource(array $rawResourceArray) |
|
118
|
|
|
{ |
|
119
|
2 |
|
$formattedResourceArray = array(); |
|
120
|
|
|
|
|
121
|
2 |
|
foreach ($rawResourceArray as $key => $leaf) { |
|
122
|
2 |
|
if (is_array($leaf)) { |
|
123
|
2 |
|
$formattedLeafArray = $this->formatLeafArray($leaf); |
|
124
|
2 |
|
if (count($formattedLeafArray) > 0) { |
|
125
|
1 |
|
$formattedResourceArray[$key] = $formattedLeafArray; |
|
126
|
1 |
|
} |
|
127
|
2 |
|
} else { |
|
128
|
1 |
|
$formattedResourceArray[$key] = $leaf; |
|
129
|
|
|
} |
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
return $formattedResourceArray; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Formats the given array to be in the format of a leaf. |
|
137
|
|
|
* A leaf that is an array is an associative array with boolean, string or numeric values. |
|
138
|
|
|
* The input format is a non associative array of associative array. |
|
139
|
|
|
* Each associative array must contain: |
|
140
|
|
|
* a string value for the key LOCALE_KEY which is the locale in the formatted array |
|
141
|
|
|
* and a boolean, string or numeric value for the key VALUE_KEY which is the value in the formatted array. |
|
142
|
|
|
* |
|
143
|
|
|
* The value that cannot be matched in this format are dropped |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $leafArray |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
2 |
|
private function formatLeafArray(array $leafArray) |
|
150
|
|
|
{ |
|
151
|
2 |
|
$formattedLeafArray = array(); |
|
152
|
|
|
|
|
153
|
2 |
|
foreach ($leafArray as $leafElement) { |
|
154
|
2 |
|
if (!array_key_exists(self::LOCALE_KEY, $leafElement) || |
|
155
|
2 |
|
!array_key_exists(self::VALUE_KEY, $leafElement)) { |
|
156
|
2 |
|
continue; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
$locale = $leafElement[self::LOCALE_KEY]; |
|
160
|
2 |
|
$value = $leafElement[self::VALUE_KEY]; |
|
161
|
|
|
|
|
162
|
2 |
|
if (!is_string($locale) || is_array($value)) { |
|
163
|
2 |
|
continue; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
1 |
|
$formattedLeafArray[$locale] = $value; |
|
167
|
2 |
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
return $formattedLeafArray; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|