|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Crassat <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FabienCrassat\CurriculumVitaeBundle\Entity; |
|
13
|
|
|
|
|
14
|
|
|
use FabienCrassat\CurriculumVitaeBundle\Utility\AgeCalculator; |
|
15
|
|
|
|
|
16
|
|
|
class Xml2arrayFunctions { |
|
17
|
|
|
private $language; |
|
18
|
|
|
private $file; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param \SimpleXMLElement $file |
|
22
|
|
|
* @param string $language |
|
23
|
|
|
*/ |
|
24
|
35 |
|
public function __construct($file, $language = 'en') { |
|
25
|
35 |
|
$this->language = $language; |
|
26
|
35 |
|
$this->file = $file; |
|
27
|
35 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \SimpleXMLElement $xml |
|
31
|
|
|
* @param integer $recursiveDepth |
|
32
|
|
|
* @param boolean $format |
|
33
|
|
|
* |
|
34
|
|
|
* @return null|array<string,array> |
|
35
|
|
|
*/ |
|
36
|
32 |
|
public function xml2array(\SimpleXMLElement $xml, $recursiveDepth = 0, $format = TRUE) { |
|
37
|
32 |
|
$recursiveDepth++; |
|
38
|
32 |
|
$result = []; |
|
39
|
|
|
|
|
40
|
|
|
// Extraction of the node |
|
41
|
32 |
|
$key = trim($xml->getName()); |
|
42
|
32 |
|
$value = trim((string) $xml); |
|
43
|
|
|
|
|
44
|
|
|
// Specific Attribute: do nothing when it is not the good language |
|
45
|
32 |
|
if ($xml->attributes()->lang) { |
|
46
|
22 |
|
if ($xml->attributes()->lang <> $this->language) { |
|
|
|
|
|
|
47
|
18 |
|
return NULL; |
|
48
|
|
|
} |
|
49
|
20 |
|
unset($xml->attributes()->lang); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Specific Attributes changing the xml |
|
53
|
32 |
|
$key = $this->setSpecificAttributeKeyWithGivenId($xml, $key); |
|
54
|
32 |
|
$value = $this->setSpecificAttributeAge($xml, $recursiveDepth, $value); |
|
55
|
32 |
|
$result = $this->retrieveSpecificAttributeCrossRef($xml, $recursiveDepth, $result, $key); |
|
56
|
|
|
// Standard Attributes |
|
57
|
32 |
|
$result = $this->setStandardAttributes($xml, $result, $key); |
|
58
|
|
|
// Specific Key |
|
59
|
32 |
|
$value = $this->setValueForSpecificKeys($key, $value, $format); |
|
60
|
|
|
|
|
61
|
32 |
|
$result = $this->setValue($result, $key, $value); |
|
62
|
32 |
|
return $this->setChildren($xml, $recursiveDepth, $key, $result); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param array $arrayToSet |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* @param array|string $value |
|
69
|
|
|
* |
|
70
|
|
|
* @return array<string,string> |
|
71
|
|
|
*/ |
|
72
|
32 |
|
private function setValue(array $arrayToSet, $key, $value) { |
|
73
|
32 |
|
if ($value <> '') { |
|
74
|
29 |
|
return array_merge($arrayToSet, [$key => $value]); |
|
75
|
|
|
} |
|
76
|
32 |
|
return $arrayToSet; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param \SimpleXMLElement $xml |
|
81
|
|
|
* @param integer $depth |
|
82
|
|
|
* @param string $key |
|
83
|
|
|
* @param array $arrayXML |
|
84
|
|
|
* |
|
85
|
|
|
* @return array<string,array> |
|
86
|
|
|
*/ |
|
87
|
32 |
|
private function setChildren(\SimpleXMLElement $xml, $depth, $key, array $arrayXML) { |
|
88
|
32 |
|
$return = $arrayXML; |
|
89
|
32 |
|
if ($xml->children()->count() > 0) { |
|
90
|
30 |
|
foreach ($xml->children() as $childValue) { |
|
91
|
30 |
|
$child = $this->xml2array($childValue, $depth); |
|
|
|
|
|
|
92
|
30 |
|
if ($depth > 1 && ! empty($child)) { |
|
93
|
21 |
|
$return = array_merge_recursive($return, [$key => $child]); |
|
94
|
30 |
|
} elseif (! empty($child)) { |
|
95
|
30 |
|
$return = array_merge_recursive($return, $child); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
32 |
|
return $return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param \SimpleXMLElement $xml |
|
104
|
|
|
* @param array $arrayToMerge |
|
105
|
|
|
* @param string $key |
|
106
|
|
|
* |
|
107
|
|
|
* @return array<string,string> |
|
108
|
|
|
*/ |
|
109
|
32 |
|
private function setStandardAttributes(\SimpleXMLElement $xml, array $arrayToMerge, $key) { |
|
110
|
|
|
// Standard Attributes (without Specific thanks to unset()) |
|
111
|
32 |
|
$attributes = []; |
|
112
|
32 |
|
foreach ($xml->attributes() as $attributeKey => $attributeValue) { |
|
113
|
25 |
|
if ($this->isStandardAttributes($attributeKey)) { |
|
114
|
25 |
|
$attributes[$attributeKey] = trim($attributeValue); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
32 |
|
if (count($attributes) > 0) { |
|
118
|
16 |
|
return array_merge_recursive($arrayToMerge, [$key => $attributes]); |
|
119
|
|
|
} |
|
120
|
32 |
|
return $arrayToMerge; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $attribute |
|
125
|
|
|
* |
|
126
|
|
|
* @return boolean |
|
127
|
|
|
*/ |
|
128
|
25 |
|
private function isStandardAttributes($attribute) { |
|
129
|
25 |
|
return $attribute <> 'id' |
|
130
|
25 |
|
&& $attribute <> 'ref' |
|
131
|
25 |
|
&& $attribute <> 'lang' |
|
132
|
25 |
|
&& $attribute <> 'crossref'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param \SimpleXMLElement $xml |
|
137
|
|
|
* @param string $keyValue |
|
138
|
|
|
* |
|
139
|
|
|
* @return string |
|
140
|
|
|
*/ |
|
141
|
32 |
|
private function setSpecificAttributeKeyWithGivenId(\SimpleXMLElement $xml, $keyValue) { |
|
142
|
|
|
// Specific Attribute: change the key with the given id |
|
143
|
32 |
|
if ($xml->attributes()->id) { |
|
144
|
19 |
|
return (string) $xml->attributes()->id; |
|
145
|
|
|
} |
|
146
|
32 |
|
return $keyValue; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param \SimpleXMLElement $xml |
|
151
|
|
|
* @param integer $depth |
|
152
|
|
|
* @param string $age |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
32 |
|
private function setSpecificAttributeAge(\SimpleXMLElement $xml, $depth, $age) { |
|
157
|
|
|
// Specific Attribute: Retreive the age |
|
158
|
32 |
|
if ($xml->attributes()->getAge) { |
|
159
|
10 |
|
$crossref = $this->file->xpath(trim($xml->attributes()->getAge)); |
|
160
|
10 |
|
$birthday = $this->xml2array(clone $crossref[0], $depth, FALSE); |
|
161
|
10 |
|
$birthday = implode('', $birthday); |
|
162
|
|
|
|
|
163
|
10 |
|
$ageCalculator = new AgeCalculator((string) $birthday); |
|
164
|
|
|
|
|
165
|
10 |
|
return (string) $ageCalculator->age(); |
|
166
|
|
|
} |
|
167
|
32 |
|
return $age; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param \SimpleXMLElement $xml |
|
172
|
|
|
* @param integer $depth |
|
173
|
|
|
* @param array $arrayXML |
|
174
|
|
|
* @param string $key |
|
175
|
|
|
* |
|
176
|
|
|
* @return array<string,array> |
|
177
|
|
|
*/ |
|
178
|
32 |
|
private function retrieveSpecificAttributeCrossRef(\SimpleXMLElement $xml, $depth, array $arrayXML, $key) { |
|
179
|
|
|
// Specific Attribute: Retrieve the given crossref |
|
180
|
32 |
|
if ($xml->attributes()->crossref) { |
|
181
|
14 |
|
$crossref = $this->file->xpath(trim($xml->attributes()->crossref)); |
|
182
|
14 |
|
$temp = []; |
|
183
|
14 |
|
foreach ($crossref as $value) { |
|
184
|
14 |
|
$resultArray = $this->xml2array($value, $depth); |
|
185
|
|
|
|
|
186
|
14 |
|
if (! empty($resultArray)) { |
|
187
|
14 |
|
$temp = array_merge($temp, $resultArray); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
14 |
|
return array_merge($arrayXML, [$key => $temp]); |
|
191
|
|
|
} |
|
192
|
32 |
|
return $arrayXML; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $key |
|
197
|
|
|
* @param string $value |
|
198
|
|
|
* @param boolean $format |
|
199
|
|
|
* |
|
200
|
|
|
* @return string|string[] |
|
201
|
|
|
*/ |
|
202
|
32 |
|
private function setValueForSpecificKeys($key, $value, $format) { |
|
203
|
32 |
|
if ($key == 'birthday') { |
|
204
|
11 |
|
return $this->setValueForBirthdayKey($value, $format); |
|
205
|
32 |
|
} elseif ($key == 'item') { |
|
206
|
9 |
|
return [$value]; // convert to apply array_merge() |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
32 |
|
return $value; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Specific Key: Format to french date format |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $date |
|
216
|
|
|
* @param boolean $format |
|
217
|
|
|
* |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
11 |
|
private function setValueForBirthdayKey($date, $format) { |
|
221
|
11 |
|
if ($format) { |
|
222
|
11 |
|
setlocale(LC_TIME, ['fra_fra', 'fr', 'fr_FR', 'fr_FR.UTF8']); |
|
223
|
11 |
|
return strftime('%d %B %Y', strtotime(date($date))); |
|
224
|
|
|
} |
|
225
|
10 |
|
return $date; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|