1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Google Map package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMap\Service\Utility; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author GeLo <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class XmlParser implements ParserInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function parse($data, array $options = []) |
23
|
|
|
{ |
24
|
|
|
return $this->process( |
25
|
|
|
json_decode(json_encode(new \SimpleXMLElement($data)), true), |
26
|
|
|
isset($options['pluralization_rules']) ? $options['pluralization_rules'] : [], |
27
|
|
|
isset($options['snake_to_camel']) ? $options['snake_to_camel'] : false |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param mixed[] $data |
33
|
|
|
* @param string[] $pluralizationRules |
34
|
|
|
* @param bool $snakeToCamel |
35
|
|
|
* |
36
|
|
|
* @return mixed[] |
37
|
|
|
*/ |
38
|
|
|
private function process(array $data, array $pluralizationRules, $snakeToCamel) |
39
|
|
|
{ |
40
|
|
|
foreach ($data as $attribute => $value) { |
41
|
|
|
if (isset($pluralizationRules[$attribute])) { |
42
|
|
|
$data[$pluralizationRules[$attribute]] = $value; |
43
|
|
|
unset($data[$attribute]); |
44
|
|
|
|
45
|
|
|
$attribute = $pluralizationRules[$attribute]; |
46
|
|
|
|
47
|
|
|
if (!is_array($value) || is_string(key($value))) { |
48
|
|
|
$data[$attribute] = [$value]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (is_array($data[$attribute])) { |
53
|
|
|
$data[$attribute] = $this->process($data[$attribute], $pluralizationRules, $snakeToCamel); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($snakeToCamel && is_string($attribute) |
57
|
|
|
&& ($newAttribute = $this->snakeToCamel($attribute)) !== $attribute |
58
|
|
|
) { |
59
|
|
|
$data[$newAttribute] = $data[$attribute]; |
60
|
|
|
unset($data[$attribute]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $data |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function snakeToCamel($data) |
73
|
|
|
{ |
74
|
|
|
return lcfirst(implode('', array_map(function ($word) { |
75
|
|
|
return ucfirst($word); |
76
|
|
|
}, explode('_', $data)))); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|