Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

REST/Common/Output/Generator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\Output;
8
12 abstract class Generator
13 {
22 protected $stack = [];
23
29 protected $formatOutput = false;
30
31 public function setFormatOutput($formatOutput)
32 {
33 $this->formatOutput = (bool)$formatOutput;
34 }
35
39 public function reset()
40 {
41 $this->stack = [];
42 $this->isEmpty = true;
43 }
44
50 abstract public function startDocument($data);
51
57 abstract public function isEmpty();
58
64 protected function checkStartDocument($data)
65 {
66 if (count($this->stack)) {
67 throw new Exceptions\OutputGeneratorException(
68 'Starting a document may only be the very first operation.'
69 );
70 }
71
72 $this->stack[] = ['document', $data, []];
73 }
74
84 abstract public function endDocument($data);
85
91 protected function checkEndDocument($data)
92 {
93 $this->checkEnd('document', $data);
94 }
95
102 abstract public function startObjectElement($name, $mediaTypeName = null);
103
109 protected function checkStartObjectElement($data)
110 {
111 $this->checkStart('objectElement', $data, ['document', 'objectElement', 'hashElement', 'list']);
112
113 $last = count($this->stack) - 2;
114 if ($this->stack[$last][0] !== 'list') {
115 // Ensure object element type only occurs once outside of lists
116 if (isset($this->stack[$last][2][$data])) {
117 throw new Exceptions\OutputGeneratorException(
118 "Element {$data} may only occur once inside of {$this->stack[$last][0]}."
119 );
120 }
121 }
122 $this->stack[$last][2][$data] = true;
123 }
124
130 abstract public function endObjectElement($name);
131
137 protected function checkEndObjectElement($data)
138 {
139 $this->checkEnd('objectElement', $data);
140 }
141
147 abstract public function startHashElement($name);
148
154 protected function checkStartHashElement($data)
155 {
156 $this->checkStart('hashElement', $data, ['document', 'objectElement', 'hashElement', 'list']);
157
158 $last = count($this->stack) - 2;
159 if ($this->stack[$last][0] !== 'list') {
160 // Ensure hash element type only occurs once outside of lists
161 if (isset($this->stack[$last][2][$data])) {
162 throw new Exceptions\OutputGeneratorException(
163 "Element {$data} may only occur once inside of {$this->stack[$last][0]}."
164 );
165 }
166 }
167 $this->stack[$last][2][$data] = true;
168 }
169
175 abstract public function endHashElement($name);
176
182 protected function checkEndHashElement($data)
183 {
184 $this->checkEnd('hashElement', $data);
185 }
186
193 abstract public function startValueElement($name, $value);
194
200 protected function checkStartValueElement($data)
201 {
202 $this->checkStart('valueElement', $data, ['objectElement', 'hashElement', 'list']);
203 }
204
210 abstract public function endValueElement($name);
211
217 protected function checkEndValueElement($data)
218 {
219 $this->checkEnd('valueElement', $data);
220 }
221
227 abstract public function startList($name);
228
234 protected function checkStartList($data)
235 {
236 $this->checkStart('list', $data, ['objectElement', 'hashElement']);
237 }
238
244 abstract public function endList($name);
245
251 protected function checkEndList($data)
252 {
253 $this->checkEnd('list', $data);
254 }
255
262 abstract public function startAttribute($name, $value);
263
269 protected function checkStartAttribute($data)
270 {
271 $this->checkStart('attribute', $data, ['objectElement', 'hashElement']);
272 }
273
279 abstract public function endAttribute($name);
280
286 protected function checkEndAttribute($data)
287 {
288 $this->checkEnd('attribute', $data);
289 }
290
298 abstract public function getMediaType($name);
299
310 protected function generateMediaType($name, $type)
311 {
312 return "application/vnd.ez.api.{$name}+{$type}";
313 }
314
324 protected function generateMediaTypeWithVendor($name, $type, $vendor = 'vnd.ez.api')
325 {
326 return "application/{$vendor}.{$name}+{$type}";
327 }
328
337 abstract public function generateFieldTypeHash($hashElementName, $hashValue);
338
346 protected function checkStart($type, $data, array $validParents)
347 {
348 $lastTag = end($this->stack);
349
350 if (!is_array($lastTag)) {
351 throw new Exceptions\OutputGeneratorException(
352 sprintf(
353 'Invalid start: Trying to open outside of a document.'
354 )
355 );
356 }
357
358 if (!in_array($lastTag[0], $validParents)) {
359 throw new Exceptions\OutputGeneratorException(
360 sprintf(
361 'Invalid start: Trying to open %s inside %s, valid parent nodes are: %s.',
362 $type,
363 $lastTag[0],
364 implode(', ', $validParents)
365 )
366 );
367 }
368
369 $this->stack[] = [$type, $data, []];
370 }
371
378 protected function checkEnd($type, $data)
379 {
380 $lastTag = array_pop($this->stack);
381
382 if (!is_array($lastTag)) {
383 throw new Exceptions\OutputGeneratorException(
384 sprintf(
385 'Invalid close: Trying to close on empty stack.'
386 )
387 );
388 }
389
390 if ([$lastTag[0], $lastTag[1]] !== [$type, $data]) {
391 throw new Exceptions\OutputGeneratorException(
392 sprintf(
393 'Invalid close: Trying to close %s:%s, while last element was %s:%s.',
394 $type,
395 $data,
396 $lastTag[0],
397 $lastTag[1]
398 )
399 );
400 }
401 }
402
410 abstract public function serializeBool($boolValue);
411 }
eZ\Publish\Core\REST\Common\Output\Exceptions\OutputGeneratorException
Invalid output generation.
Definition: OutputGeneratorException.php:15
eZ\Publish\Core\REST\Common\Output\Generator
Output generator.
Definition: REST/Common/Output/Generator.php:13
eZ\Publish\Core\REST\Common\Output\Generator\checkStartDocument
checkStartDocument($data)
Check start document.
Definition: REST/Common/Output/Generator.php:64
eZ\Publish\Core\REST\Common\Output\Generator\generateMediaType
generateMediaType($name, $type)
Generates a media type from $name and $type.
Definition: REST/Common/Output/Generator.php:310
eZ\Publish\Core\REST\Common\Output\Generator\checkEndList
checkEndList($data)
Check end list.
Definition: REST/Common/Output/Generator.php:251
eZ\Publish\Core\REST\Common\Output\Generator\checkStartList
checkStartList($data)
Check start list.
Definition: REST/Common/Output/Generator.php:234
eZ\Publish\Core\REST\Common\Output\Generator\startAttribute
startAttribute($name, $value)
Start attribute.
eZ\Publish\Core\REST\Common\Output\Generator\$stack
$stack
Definition: REST/Common/Output/Generator.php:22
eZ\Publish\Core\REST\Common\Output\Generator\checkEndAttribute
checkEndAttribute($data)
Check end attribute.
Definition: REST/Common/Output/Generator.php:286
eZ\Publish\Core\REST\Common\Output\Generator\setFormatOutput
setFormatOutput($formatOutput)
Definition: REST/Common/Output/Generator.php:31
eZ\Publish\Core\REST\Common\Output\Generator\generateMediaTypeWithVendor
generateMediaTypeWithVendor($name, $type, $vendor='vnd.ez.api')
Generates a media type from $name, $type and $vendor.
Definition: REST/Common/Output/Generator.php:324
eZ\Publish\Core\REST\Common\Output\Generator\endObjectElement
endObjectElement($name)
End object element.
eZ\Publish\Core\REST\Common\Output\Generator\startValueElement
startValueElement($name, $value)
Start value element.
eZ\Publish\Core\REST\Common\Output\Generator\endHashElement
endHashElement($name)
End hash element.
eZ\Publish\Core\REST\Common\Output\Generator\checkEndDocument
checkEndDocument($data)
Check end document.
Definition: REST/Common/Output/Generator.php:91
eZ\Publish\Core\REST\Common\Output\Generator\checkEnd
checkEnd($type, $data)
Check close / end operation.
Definition: REST/Common/Output/Generator.php:378
eZ\Publish\Core\REST\Common\Output\Generator\getMediaType
getMediaType($name)
Get media type.
eZ\Publish\Core\REST\Common\Output\Generator\serializeBool
serializeBool($boolValue)
Serializes a boolean value.
eZ\Publish\Core\REST\Common\Output\Generator\checkEndObjectElement
checkEndObjectElement($data)
Check end object element.
Definition: REST/Common/Output/Generator.php:137
eZ\Publish\Core\REST\Common\Output\Generator\checkStartValueElement
checkStartValueElement($data)
Check start value element.
Definition: REST/Common/Output/Generator.php:200
eZ\Publish\Core\REST\Common\Output\Generator\endDocument
endDocument($data)
End document.
eZ\Publish\Core\REST\Common\Output\Generator\endValueElement
endValueElement($name)
End value element.
eZ\Publish\Core\REST\Common\Output\Generator\checkEndValueElement
checkEndValueElement($data)
Check end value element.
Definition: REST/Common/Output/Generator.php:217
eZ\Publish\Core\REST\Common\Output\Generator\checkEndHashElement
checkEndHashElement($data)
Check end hash element.
Definition: REST/Common/Output/Generator.php:182
eZ\Publish\Core\REST\Common\Output\Generator\endList
endList($name)
End list.
eZ\Publish\Core\REST\Common\Output\Generator\checkStartAttribute
checkStartAttribute($data)
Check start attribute.
Definition: REST/Common/Output/Generator.php:269
eZ\Publish\Core\REST\Common\Output\Generator\startDocument
startDocument($data)
Start document.
eZ\Publish\Core\REST\Common\Output\Generator\reset
reset()
Reset output visitor to a virgin state.
Definition: REST/Common/Output/Generator.php:39
eZ\Publish\Core\REST\Common\Output\Generator\startList
startList($name)
Start list.
eZ\Publish\Core\REST\Common\Output\Generator\checkStart
checkStart($type, $data, array $validParents)
Check close / end operation.
Definition: REST/Common/Output/Generator.php:346
eZ\Publish\Core\REST\Common\Output\Generator\$formatOutput
$formatOutput
Definition: REST/Common/Output/Generator.php:29
eZ\Publish\Core\REST\Common\Output\Generator\generateFieldTypeHash
generateFieldTypeHash($hashElementName, $hashValue)
Generates a generic representation of the scalar, hash or list given in $hashValue into the document,...
eZ\Publish\Core\REST\Common\Output\Generator\checkStartHashElement
checkStartHashElement($data)
Check start hash element.
Definition: REST/Common/Output/Generator.php:154
eZ\Publish\Core\REST\Common\Output\Generator\isEmpty
isEmpty()
Returns if the document is empty or already contains data.
eZ\Publish\Core\REST\Common\Output\Generator\startHashElement
startHashElement($name)
Start hash element.
eZ\Publish\Core\REST\Common\Output\Generator\endAttribute
endAttribute($name)
End attribute.
eZ\Publish\Core\REST\Common\Output\Generator\checkStartObjectElement
checkStartObjectElement($data)
Check start object element.
Definition: REST/Common/Output/Generator.php:109
eZ\Publish\Core\REST\Common\Output\Generator\startObjectElement
startObjectElement($name, $mediaTypeName=null)
Start object element.
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.