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

Output/Generator/Json.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\Output\Generator;
8
9 use eZ\Publish\Core\REST\Common\Output\Generator;
10
14 class Json extends Generator
15 {
21 protected $json;
22
28 protected $fieldTypeHashGenerator;
29
35 protected $isEmpty = true;
36
42 protected $vendor;
43
48 public function __construct(Json\FieldTypeHashGenerator $fieldTypeHashGenerator, $vendor = 'vnd.ez.api')
49 {
50 $this->fieldTypeHashGenerator = $fieldTypeHashGenerator;
51 $this->vendor = $vendor;
52 }
53
59 public function startDocument($data)
60 {
61 $this->checkStartDocument($data);
62
63 $this->isEmpty = true;
64
65 $this->json = new Json\JsonObject();
66 }
67
73 public function isEmpty()
74 {
75 return $this->isEmpty;
76 }
77
87 public function endDocument($data)
88 {
89 $this->checkEndDocument($data);
90
91 $jsonEncodeOptions = 0;
92 if ($this->formatOutput && defined('JSON_PRETTY_PRINT')) {
93 $jsonEncodeOptions = JSON_PRETTY_PRINT;
94 }
95
96 $this->json = $this->convertArrayObjects($this->json);
97
98 return json_encode($this->json, $jsonEncodeOptions);
99 }
100
111 protected function convertArrayObjects($data)
112 {
113 if ($data instanceof Json\ArrayObject) {
114 // @todo: Check if we need to convert arrays with only one single
115 // element into non-arrays /cc cba
116 $data = $data->getArrayCopy();
117 foreach ($data as $key => $value) {
118 $data[$key] = $this->convertArrayObjects($value);
119 }
120 } elseif ($data instanceof Json\JsonObject) {
121 foreach ($data as $key => $value) {
122 $data->$key = $this->convertArrayObjects($value);
123 }
124 }
125
126 return $data;
127 }
128
135 public function startObjectElement($name, $mediaTypeName = null)
136 {
137 $this->checkStartObjectElement($name);
138
139 $this->isEmpty = false;
140
141 $mediaTypeName = $mediaTypeName ?: $name;
142
143 $object = new Json\JsonObject($this->json);
144
145 if ($this->json instanceof Json\ArrayObject) {
146 $this->json[] = $object;
147 $this->json = $object;
148 } else {
149 $this->json->$name = $object;
150 $this->json = $object;
151 }
152
153 $this->startAttribute('media-type', $this->getMediaType($mediaTypeName));
154 $this->endAttribute('media-type');
155 }
156
162 public function endObjectElement($name)
163 {
164 $this->checkEndObjectElement($name);
165
166 $this->json = $this->json->getParent();
167 }
168
174 public function startHashElement($name)
175 {
176 $this->checkStartHashElement($name);
177
178 $this->isEmpty = false;
179
180 $object = new Json\JsonObject($this->json);
181
182 if ($this->json instanceof Json\ArrayObject) {
183 $this->json[] = $object;
184 $this->json = $object;
185 } else {
186 $this->json->$name = $object;
187 $this->json = $object;
188 }
189 }
190
196 public function endHashElement($name)
197 {
198 $this->checkEndHashElement($name);
199
200 $this->json = $this->json->getParent();
201 }
202
210 public function startValueElement($name, $value, $attributes = [])
211 {
212 $this->checkStartValueElement($name);
213
214 $jsonValue = null;
215
216 if (empty($attributes)) {
217 $jsonValue = $value;
218 } else {
219 $jsonValue = new Json\JsonObject($this->json);
220 foreach ($attributes as $attributeName => $attributeValue) {
221 $jsonValue->{'_' . $attributeName} = $attributeValue;
222 }
223 $jsonValue->{'#text'} = $value;
224 }
225
226 if ($this->json instanceof Json\ArrayObject) {
227 $this->json[] = $jsonValue;
228 } else {
229 $this->json->$name = $jsonValue;
230 }
231 }
232
238 public function endValueElement($name)
239 {
240 $this->checkEndValueElement($name);
241 }
242
248 public function startList($name)
249 {
250 $this->checkStartList($name);
251
252 $array = new Json\ArrayObject($this->json);
253
254 $this->json->$name = $array;
255 $this->json = $array;
256 }
257
263 public function endList($name)
264 {
265 $this->checkEndList($name);
266
267 $this->json = $this->json->getParent();
268 }
269
276 public function startAttribute($name, $value)
277 {
278 $this->checkStartAttribute($name);
279
280 $this->json->{'_' . $name} = $value;
281 }
282
288 public function endAttribute($name)
289 {
290 $this->checkEndAttribute($name);
291 }
292
300 public function getMediaType($name)
301 {
302 return $this->generateMediaTypeWithVendor($name, 'json', $this->vendor);
303 }
304
313 public function generateFieldTypeHash($hashElementName, $hashValue)
314 {
315 $this->fieldTypeHashGenerator->generateHashValue(
316 $this->json,
317 $hashElementName,
318 $hashValue
319 );
320 }
321
329 public function serializeBool($boolValue)
330 {
331 return (bool)$boolValue;
332 }
333 }
eZ\Publish\Core\REST\Common\Output\Generator\Json\ArrayObject
Json array object.
Definition: ArrayObject.php:18
eZ\Publish\Core\REST\Common\Output\Generator\Json
Json generator.
Definition: Output/Generator/Json.php:15
eZ\Publish\Core\REST\Common\Output\Generator\Json\endList
endList($name)
End list.
Definition: Output/Generator/Json.php:263
eZ\Publish\Core\REST\Common\Output\Generator\Json\$fieldTypeHashGenerator
$fieldTypeHashGenerator
Definition: Output/Generator/Json.php:28
eZ\Publish\Core\REST\Common\Output\Generator\Json\endDocument
endDocument($data)
End document.
Definition: Output/Generator/Json.php:87
eZ\Publish\Core\REST\Common\Output\Generator\Json\getMediaType
getMediaType($name)
Get media type.
Definition: Output/Generator/Json.php:300
eZ\Publish\Core\REST\Common\Output\Generator\Json\startList
startList($name)
Start list.
Definition: Output/Generator/Json.php:248
eZ\Publish\Core\REST\Common\Output\Generator\Json\startAttribute
startAttribute($name, $value)
Start attribute.
Definition: Output/Generator/Json.php:276
eZ\Publish\Core\REST\Common\Output\Generator\Json\startObjectElement
startObjectElement($name, $mediaTypeName=null)
Start object element.
Definition: Output/Generator/Json.php:135
eZ\Publish\Core\REST\Common\Output\Generator\Json\startDocument
startDocument($data)
Start document.
Definition: Output/Generator/Json.php:59
eZ\Publish\Core\REST\Common\Output\Generator\Json\$isEmpty
$isEmpty
Definition: Output/Generator/Json.php:35
eZ\Publish\Core\REST\Common\Output\Generator\Json\endValueElement
endValueElement($name)
End value element.
Definition: Output/Generator/Json.php:238
eZ\Publish\Core\REST\Common\Output\Generator\Json\$vendor
$vendor
Definition: Output/Generator/Json.php:42
eZ\Publish\Core\REST\Common\Output\Generator\Json\convertArrayObjects
convertArrayObjects($data)
Convert ArrayObjects to arrays.
Definition: Output/Generator/Json.php:111
eZ\Publish\Core\REST\Common\Output\Generator\Json\isEmpty
isEmpty()
Returns if the document is empty or already contains data.
Definition: Output/Generator/Json.php:73
eZ\Publish\Core\REST\Common\Output\Generator\Json\generateFieldTypeHash
generateFieldTypeHash($hashElementName, $hashValue)
Generates a generic representation of the scalar, hash or list given in $hashValue into the document,...
Definition: Output/Generator/Json.php:313
eZ\Publish\Core\REST\Common\Output\Generator\Json\startHashElement
startHashElement($name)
Start hash element.
Definition: Output/Generator/Json.php:174
eZ\Publish\Core\REST\Common\Output\Generator\Json\$json
$json
Definition: Output/Generator/Json.php:21
eZ\Publish\Core\REST\Common\Output\Generator\Json\endHashElement
endHashElement($name)
End hash element.
Definition: Output/Generator/Json.php:196
eZ\Publish\Core\REST\Common\Output\Generator\Json\endObjectElement
endObjectElement($name)
End object element.
Definition: Output/Generator/Json.php:162
eZ\Publish\Core\REST\Common\Output\Generator\Json\serializeBool
serializeBool($boolValue)
Serializes a boolean value.
Definition: Output/Generator/Json.php:329
eZ\Publish\Core\REST\Common\Output\Generator\Json\endAttribute
endAttribute($name)
End attribute.
Definition: Output/Generator/Json.php:288
eZ\Publish\Core\REST\Common\Output\Generator\Json\__construct
__construct(Json\FieldTypeHashGenerator $fieldTypeHashGenerator, $vendor='vnd.ez.api')
Definition: Output/Generator/Json.php:48
eZ\Publish\Core\REST\Common\Output\Generator\Json\startValueElement
startValueElement($name, $value, $attributes=[])
Start value element.
Definition: Output/Generator/Json.php:210
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\checkEndDocument
checkEndDocument($data)
Check end document.
Definition: REST/Common/Output/Generator.php:91
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\checkEndHashElement
checkEndHashElement($data)
Check end hash element.
Definition: REST/Common/Output/Generator.php:182
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\checkStartObjectElement
checkStartObjectElement($data)
Check start object element.
Definition: REST/Common/Output/Generator.php:109
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.