|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
4
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace eZ\Publish\Core\REST\Server\Output\PathExpansion; |
|
7
|
|
|
|
|
8
|
|
|
use eZ\Publish\Core\Base\Exceptions\BadStateException; |
|
9
|
|
|
use eZ\Publish\Core\REST\Common\Output\Exceptions\OutputGeneratorException; |
|
10
|
|
|
use eZ\Publish\Core\REST\Common\Output\Generator; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A REST output generator meant to expand an existing generator. |
|
14
|
|
|
* |
|
15
|
|
|
* @todo a crappy description for a crappy class name. It all makes sense. Or it will. |
|
16
|
|
|
*/ |
|
17
|
|
|
class ExpansionGenerator extends Generator |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Generator |
|
21
|
|
|
*/ |
|
22
|
|
|
private $innerGenerator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* ExpansionGenerator constructor. |
|
26
|
|
|
* @param Generator $generator |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Generator $generator) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->innerGenerator = $generator; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Not supported in this generator, as the context is always within a generated document. |
|
35
|
|
|
* |
|
36
|
|
|
* @param mixed $data |
|
37
|
|
|
* |
|
38
|
|
|
* @throws BadStateException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function startDocument($data) |
|
41
|
|
|
{ |
|
42
|
|
|
throw new BadStateException( |
|
43
|
|
|
'generator', |
|
44
|
|
|
'start/endDocument can not be used with the ExpansionGenerator' |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns if the document is empty or already contains data. |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function isEmpty() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->innerGenerator->isEmpty(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Not supported in this generator, as the context is always within a generated document. |
|
60
|
|
|
* |
|
61
|
|
|
* @param mixed $data |
|
62
|
|
|
* |
|
63
|
|
|
* @throws BadStateException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function endDocument($data) |
|
66
|
|
|
{ |
|
67
|
|
|
throw new BadStateException('generator', 'start/endDocument can not be used with the ExpansionGenerator'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Start object element. |
|
72
|
|
|
* If the element is the first added to this generator, it is silently skipped. |
|
73
|
|
|
* Subsequent elements are started as expected. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @param string $mediaTypeName |
|
77
|
|
|
*/ |
|
78
|
|
|
public function startObjectElement($name, $mediaTypeName = null) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->stack[] = $name; |
|
81
|
|
|
|
|
82
|
|
|
if (count($this->stack) > 1) { |
|
83
|
|
|
$this->innerGenerator->startObjectElement($name, $mediaTypeName); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* End object element. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name |
|
91
|
|
|
*/ |
|
92
|
|
|
public function endObjectElement($name) |
|
93
|
|
|
{ |
|
94
|
|
|
$objectElementName = array_pop($this->stack); |
|
95
|
|
|
if (count($this->stack) > 0) { |
|
96
|
|
|
$this->innerGenerator->endObjectElement($name); |
|
97
|
|
|
} else { |
|
98
|
|
|
if ($name !== $objectElementName) { |
|
99
|
|
|
throw new OutputGeneratorException("Closing object element name doesn't match the opening one ($objectElementName)"); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Start hash element. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $name |
|
108
|
|
|
*/ |
|
109
|
|
|
public function startHashElement($name) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->innerGenerator->startHashElement($name); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* End hash element. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
*/ |
|
119
|
|
|
public function endHashElement($name) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->innerGenerator->endHashElement($name); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Start value element. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $name |
|
128
|
|
|
* @param string $value |
|
129
|
|
|
*/ |
|
130
|
|
|
public function startValueElement($name, $value) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->innerGenerator->startValueElement($name, $value); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* End value element. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $name |
|
139
|
|
|
*/ |
|
140
|
|
|
public function endValueElement($name) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->innerGenerator->endValueElement($name); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Start list. |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $name |
|
149
|
|
|
*/ |
|
150
|
|
|
public function startList($name) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->innerGenerator->startList($name); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* End list. |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
*/ |
|
160
|
|
|
public function endList($name) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->innerGenerator->endList($name); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Start attribute. |
|
167
|
|
|
* Skips the href and media-type attribute at stack depth 0. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $name |
|
170
|
|
|
* @param string $value |
|
171
|
|
|
*/ |
|
172
|
|
|
public function startAttribute($name, $value) |
|
173
|
|
|
{ |
|
174
|
|
View Code Duplication |
if (!in_array($name, ['href', 'media-type']) || count($this->stack) > 1) { |
|
|
|
|
|
|
175
|
|
|
$this->innerGenerator->startAttribute($name, $value); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* End attribute. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $name |
|
183
|
|
|
*/ |
|
184
|
|
|
public function endAttribute($name) |
|
185
|
|
|
{ |
|
186
|
|
View Code Duplication |
if (!in_array($name, ['href', 'media-type']) || count($this->stack) > 1) { |
|
|
|
|
|
|
187
|
|
|
$this->innerGenerator->endAttribute($name); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get media type. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $name |
|
195
|
|
|
* |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getMediaType($name) |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->innerGenerator->getMediaType($name); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Generates a generic representation of the scalar, hash or list given in |
|
205
|
|
|
* $hashValue into the document, using an element of $hashElementName as |
|
206
|
|
|
* its parent. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $hashElementName |
|
209
|
|
|
* @param mixed $hashValue |
|
210
|
|
|
*/ |
|
211
|
|
|
public function generateFieldTypeHash($hashElementName, $hashValue) |
|
212
|
|
|
{ |
|
213
|
|
|
$this->innerGenerator->generateFieldTypeHash($hashElementName, $hashValue); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Serializes a boolean value. |
|
218
|
|
|
* |
|
219
|
|
|
* @param bool $boolValue |
|
220
|
|
|
* |
|
221
|
|
|
* @return mixed |
|
222
|
|
|
*/ |
|
223
|
|
|
public function serializeBool($boolValue) |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->innerGenerator->serializeBool($boolValue); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function getStackPath() |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->innerGenerator->getStackPath(); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.