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 Parser |
18
|
|
|
{ |
19
|
|
|
const FORMAT_JSON = 'json'; |
20
|
|
|
const FORMAT_XML = 'xml'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ParserInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $parsers = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ParserInterface[] $parsers |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $parsers = []) |
31
|
|
|
{ |
32
|
|
|
$this->setParsers($parsers); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public function hasParsers() |
39
|
|
|
{ |
40
|
|
|
return !empty($this->parsers); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return ParserInterface[] |
45
|
|
|
*/ |
46
|
|
|
public function getParsers() |
47
|
|
|
{ |
48
|
|
|
return $this->parsers; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ParserInterface[] $parsers |
53
|
|
|
*/ |
54
|
|
|
public function setParsers(array $parsers) |
55
|
|
|
{ |
56
|
|
|
$this->parsers = []; |
57
|
|
|
$this->addParsers($parsers); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ParserInterface[] $parsers |
62
|
|
|
*/ |
63
|
|
|
public function addParsers(array $parsers) |
64
|
|
|
{ |
65
|
|
|
foreach ($parsers as $format => $parser) { |
66
|
|
|
$this->setParser($format, $parser); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $format |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function hasParser($format) |
76
|
|
|
{ |
77
|
|
|
return isset($this->parsers[$format]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $format |
82
|
|
|
* |
83
|
|
|
* @return ParserInterface|null |
84
|
|
|
*/ |
85
|
|
|
public function getParser($format) |
86
|
|
|
{ |
87
|
|
|
return $this->hasParser($format) ? $this->parsers[$format] : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $format |
92
|
|
|
* @param ParserInterface $parser |
93
|
|
|
*/ |
94
|
|
|
public function setParser($format, ParserInterface $parser) |
95
|
|
|
{ |
96
|
|
|
$this->parsers[$format] = $parser; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $format |
101
|
|
|
*/ |
102
|
|
|
public function removeParser($format) |
103
|
|
|
{ |
104
|
|
|
unset($this->parsers[$format]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $data |
109
|
|
|
* @param string $format |
110
|
|
|
* @param array $options |
111
|
|
|
* |
112
|
|
|
* @return mixed[] |
113
|
|
|
*/ |
114
|
|
|
public function parse($data, $format, array $options = []) |
115
|
|
|
{ |
116
|
|
|
$parser = $this->getParser($format); |
117
|
|
|
|
118
|
|
|
if ($parser === null) { |
119
|
|
|
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $format)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $parser->parse($data, $options); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|