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

Xml/FieldTypeHashGenerator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\REST\Common\Output\Generator\Xml;
8
9 class FieldTypeHashGenerator
10 {
19 public function generateHashValue(\XMLWriter $writer, $hashElementName, $hashValue)
20 {
21 $this->generateValue($writer, $hashValue, null, $hashElementName);
22 }
23
32 protected function generateValue(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
33 {
34 switch (($hashValueType = gettype($value))) {
35 case 'NULL':
36 $this->generateNullValue($writer, $key, $elementName);
37 break;
38
39 case 'boolean':
40 $this->generateBooleanValue($writer, $value, $key, $elementName);
41 break;
42
43 case 'integer':
44 $this->generateIntegerValue($writer, $value, $key, $elementName);
45 break;
46
47 case 'double':
48 $this->generateFloatValue($writer, $value, $key, $elementName);
49 break;
50
51 case 'string':
52 $this->generateStringValue($writer, $value, $key, $elementName);
53 break;
54
55 case 'array':
56 $this->generateArrayValue($writer, $value, $key, $elementName);
57 break;
58
59 default:
60 throw new \Exception('Invalid type in field value hash: ' . $hashValueType);
61 }
62 }
63
72 protected function generateArrayValue(\XmlWriter $writer, $value, $key, $elementName = 'value')
73 {
74 if ($this->isNumericArray($value)) {
75 $this->generateListArray($writer, $value, $key, $elementName);
76 } else {
77 $this->generateHashArray($writer, $value, $key, $elementName);
78 }
79 }
80
89 protected function generateHashArray(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
90 {
91 $writer->startElement($elementName);
92 $this->generateKeyAttribute($writer, $key);
93
94 foreach ($value as $hashKey => $hashItemValue) {
95 $this->generateValue($writer, $hashItemValue, $hashKey);
96 }
97
98 $writer->endElement();
99 }
100
109 protected function generateListArray(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
110 {
111 $writer->startElement($elementName);
112 $this->generateKeyAttribute($writer, $key);
113
114 foreach ($value as $listItemValue) {
115 $this->generateValue($writer, $listItemValue);
116 }
117
118 $writer->endElement();
119 }
120
128 protected function isNumericArray(array $value)
129 {
130 foreach (array_keys($value) as $key) {
131 if (is_string($key)) {
132 return false;
133 }
134 }
135
136 return true;
137 }
138
146 protected function generateNullValue(\XmlWriter $writer, $key = null, $elementName = 'value')
147 {
148 $writer->startElement($elementName);
149 $this->generateKeyAttribute($writer, $key);
150 // @todo: xsi:type?
151 $writer->endElement();
152 }
153
162 protected function generateBooleanValue(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
163 {
164 $writer->startElement($elementName);
165 $this->generateKeyAttribute($writer, $key);
166 $writer->text($value ? 'true' : 'false');
167 $writer->endElement();
168 }
169
178 protected function generateIntegerValue(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
179 {
180 $writer->startElement($elementName);
181 $this->generateKeyAttribute($writer, $key);
182 $writer->text($value);
183 $writer->endElement();
184 }
185
194 protected function generateFloatValue(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
195 {
196 $writer->startElement($elementName);
197 $this->generateKeyAttribute($writer, $key);
198 $writer->text(sprintf('%F', $value));
199 $writer->endElement();
200 }
201
210 protected function generateStringValue(\XmlWriter $writer, $value, $key = null, $elementName = 'value')
211 {
212 $writer->startElement($elementName);
213 $this->generateKeyAttribute($writer, $key);
214 $writer->text($value);
215 $writer->endElement();
216 }
217
224 protected function generateKeyAttribute(\XmlWriter $writer, $key = null)
225 {
226 if ($key !== null) {
227 $writer->startAttribute('key');
228 $writer->text($key);
229 $writer->endAttribute();
230 }
231 }
232 }
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator
Definition: Xml/FieldTypeHashGenerator.php:10
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateKeyAttribute
generateKeyAttribute(\XmlWriter $writer, $key=null)
Generates a key attribute with $key as the value, if $key is not null.
Definition: Xml/FieldTypeHashGenerator.php:224
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateValue
generateValue(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates $value into a serialized representation.
Definition: Xml/FieldTypeHashGenerator.php:32
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateHashArray
generateHashArray(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates $value as a hash of value items.
Definition: Xml/FieldTypeHashGenerator.php:89
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateIntegerValue
generateIntegerValue(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates a integer value.
Definition: Xml/FieldTypeHashGenerator.php:178
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateListArray
generateListArray(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates $value as a list of value items.
Definition: Xml/FieldTypeHashGenerator.php:109
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateNullValue
generateNullValue(\XmlWriter $writer, $key=null, $elementName='value')
Generates a null value.
Definition: Xml/FieldTypeHashGenerator.php:146
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\isNumericArray
isNumericArray(array $value)
Checks if the given $value is a purely numeric array.
Definition: Xml/FieldTypeHashGenerator.php:128
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateArrayValue
generateArrayValue(\XmlWriter $writer, $value, $key, $elementName='value')
Generates an array value from $value.
Definition: Xml/FieldTypeHashGenerator.php:72
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateHashValue
generateHashValue(\XMLWriter $writer, $hashElementName, $hashValue)
Generates the field type value $hashValue into the $writer creating an element with $hashElementName ...
Definition: Xml/FieldTypeHashGenerator.php:19
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateStringValue
generateStringValue(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates a string value.
Definition: Xml/FieldTypeHashGenerator.php:210
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateFloatValue
generateFloatValue(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates a float value.
Definition: Xml/FieldTypeHashGenerator.php:194
eZ\Publish\Core\REST\Common\Output\Generator\Xml\FieldTypeHashGenerator\generateBooleanValue
generateBooleanValue(\XmlWriter $writer, $value, $key=null, $elementName='value')
Generates a boolean value.
Definition: Xml/FieldTypeHashGenerator.php:162
eZ\Publish\Core\REST\Common\Output\Generator\Xml
Definition: Xml/FieldTypeHashGenerator.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.