1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\JsonReference; |
4
|
|
|
|
5
|
|
|
use League\JsonReference\Decoder\JsonDecoder; |
6
|
|
|
use League\JsonReference\Decoder\YamlDecoder; |
7
|
|
|
|
8
|
|
|
final class DecoderManager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ParserInterface[] |
12
|
|
|
*/ |
13
|
|
|
private $decoders = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param DecoderInterface[] $decoders |
17
|
|
|
* @param string $defaultType |
18
|
|
|
*/ |
19
|
118 |
|
public function __construct(array $decoders = [], $defaultType = null) |
20
|
|
|
{ |
21
|
118 |
|
if (empty($decoders)) { |
22
|
108 |
|
$this->registerDefaultDecoders(); |
23
|
|
|
|
24
|
|
|
// Backwards compatibilty |
25
|
108 |
|
if ($defaultType === null) { |
26
|
72 |
|
$defaultType = 'json'; |
27
|
18 |
|
} |
28
|
54 |
|
} else { |
29
|
10 |
|
foreach ($decoders as $type => $decoder) { |
30
|
10 |
|
$this->registerDecoder($type, $decoder); |
31
|
5 |
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
118 |
|
if ($defaultType) { |
|
|
|
|
35
|
108 |
|
$this->setDefaultType($defaultType); |
36
|
54 |
|
} |
37
|
118 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Register a DecoderInterface for the given MIME-types. |
41
|
|
|
* |
42
|
|
|
* @param string $type |
43
|
|
|
* @param DecoderInterface $decoder |
44
|
|
|
*/ |
45
|
118 |
|
public function registerDecoder($type, DecoderInterface $decoder = null) |
46
|
|
|
{ |
47
|
118 |
|
if ($decoder) { |
48
|
118 |
|
$this->decoders[$type] = $decoder; |
49
|
59 |
|
} else { |
50
|
8 |
|
unset($this->decoders[$type]); |
51
|
|
|
} |
52
|
118 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all registered decoders, keyed by the extensions they are registered to decode schemas for. |
56
|
|
|
* |
57
|
|
|
* @return DecoderInterface[] |
58
|
|
|
*/ |
59
|
26 |
|
public function getDecoders() |
60
|
|
|
{ |
61
|
26 |
|
return $this->decoders; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the default type for unknown files |
66
|
|
|
* |
67
|
|
|
* @param string defaultType |
68
|
|
|
*/ |
69
|
108 |
|
public function setDefaultType($defaultType = null) |
70
|
|
|
{ |
71
|
108 |
|
$this->registerDecoder(null, $defaultType ? $this->getDecoder($defaultType) : null); |
72
|
108 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the decoder for the given extension. |
76
|
|
|
* |
77
|
|
|
* @param string $type |
78
|
|
|
* |
79
|
|
|
* @return DecoderInterface |
80
|
|
|
* @throws \InvalidArgumentException |
81
|
|
|
*/ |
82
|
116 |
|
public function getDecoder($type) |
83
|
|
|
{ |
84
|
116 |
|
if (!$this->hasDecoder($type)) { |
85
|
16 |
|
if ($this->hasDecoder(null)) { |
86
|
10 |
|
$type = null; |
87
|
5 |
|
} else { |
88
|
6 |
|
throw new \InvalidArgumentException( |
89
|
6 |
|
sprintf('A decoder is not registered for the extension "%s"', $type) |
90
|
3 |
|
); |
91
|
|
|
} |
92
|
5 |
|
} |
93
|
|
|
|
94
|
116 |
|
return $this->decoders[$type]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $type |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
118 |
|
public function hasDecoder($type) |
103
|
|
|
{ |
104
|
118 |
|
return isset($this->decoders[$type]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Register the default decoders |
109
|
|
|
*/ |
110
|
108 |
|
private function registerDefaultDecoders() |
111
|
|
|
{ |
112
|
108 |
|
$this->registerJsonDecoder(); |
113
|
108 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param DecoderInterface $decoder |
117
|
|
|
*/ |
118
|
108 |
View Code Duplication |
public function registerJsonDecoder(DecoderInterface $decoder = null) |
|
|
|
|
119
|
|
|
{ |
120
|
108 |
|
$decoder = $decoder ?: new JsonDecoder(); |
121
|
|
|
|
122
|
108 |
|
$this->registerDecoder('json', $decoder); |
123
|
108 |
|
$this->registerDecoder('text/json', $decoder); |
124
|
108 |
|
$this->registerDecoder('application/json', $decoder); |
125
|
108 |
|
$this->registerDecoder('+json', $decoder); |
126
|
108 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param DecoderInterface $decoder |
130
|
|
|
*/ |
131
|
2 |
View Code Duplication |
public function registerYamlDecoder(DecoderInterface $decoder = null) |
|
|
|
|
132
|
|
|
{ |
133
|
2 |
|
$decoder = $decoder ?: new YamlDecoder(); |
134
|
|
|
|
135
|
2 |
|
$this->registerDecoder('yml', $decoder); |
136
|
2 |
|
$this->registerDecoder('yaml', $decoder); |
137
|
2 |
|
$this->registerDecoder('text/yaml', $decoder); |
138
|
2 |
|
$this->registerDecoder('application/x-yaml', $decoder); |
139
|
2 |
|
$this->registerDecoder('+yaml', $decoder); |
140
|
2 |
|
} |
141
|
|
|
} |
142
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: