ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sabre\Xml\Serializer Namespace Reference

Data Structures

class  EnumTest
 
class  RepeatingElementsTest
 

Functions

 enum (Writer $writer, array $values)
 This file provides a number of 'serializer' helper functions. More...
 
 valueObject (Writer $writer, $valueObject, $namespace)
 The valueObject serializer turns a simple PHP object into a classname. More...
 
 repeatingElements (Writer $writer, array $items, $childElementName)
 This serializer helps you serialize xml structures that look like this: More...
 
 standardSerializer (Writer $writer, $value)
 This function is the 'default' serializer that is able to serialize most things, and delegates to other serializers if needed. More...
 

Function Documentation

◆ enum()

Sabre\Xml\Serializer\enum ( Writer  $writer,
array  $values 
)

This file provides a number of 'serializer' helper functions.

These helper functions can be used to easily xml-encode common PHP data structures, or can be placed in the $classMap. The 'enum' serializer writes simple list of elements.

For example, calling:

enum($writer, [ "{http://sabredav.org/ns}elem1", "{http://sabredav.org/ns}elem2", "{http://sabredav.org/ns}elem3", "{http://sabredav.org/ns}elem4", "{http://sabredav.org/ns}elem5", ]);

Will generate something like this (if the correct namespace is declared):

<s:elem1 /> <s:elem2 /> <s:elem3 /> <s:elem4>content</s:elem4> <s:elem5 attr="val" />

Parameters
Writer$writer
string[]$values
Returns
void

Definition at line 41 of file functions.php.

41 {
42
43 foreach ($values as $value) {
44 $writer->writeElement($value);
45 }
46}
writeElement($name, $content=null)
Write a full element tag and it's contents.
Definition: Writer.php:189
$values

References $values, and Sabre\Xml\Writer\writeElement().

Referenced by Sabre\Xml\Element\Elements\xmlSerialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ repeatingElements()

Sabre\Xml\Serializer\repeatingElements ( Writer  $writer,
array  $items,
  $childElementName 
)

This serializer helps you serialize xml structures that look like this:

<collection> ... ... ... </collection>

In that previous example, this serializer just serializes the item element, and this could be called like this:

repeatingElements($writer, $items, '{}item');

Parameters
Writer$writer
array$itemsA list of items sabre/xml can serialize.
string$childElementNameElement name in clark-notation
Returns
void

Definition at line 97 of file functions.php.

97 {
98
99 foreach ($items as $item) {
100 $writer->writeElement($childElementName, $item);
101 }
102
103}

References Sabre\Xml\Writer\writeElement().

Referenced by Sabre\Xml\Serializer\RepeatingElementsTest\testSerialize().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ standardSerializer()

Sabre\Xml\Serializer\standardSerializer ( Writer  $writer,
  $value 
)

This function is the 'default' serializer that is able to serialize most things, and delegates to other serializers if needed.

The standardSerializer supports a wide-array of values.

$value may be a string or integer, it will just write out the string as text. $value may be an instance of XmlSerializable or Element, in which case it calls it's xmlSerialize() method. $value may be a PHP callback/function/closure, in case we call the callback and give it the Writer as an argument. $value may be a an object, and if it's in the classMap we automatically call the correct serializer for it. $value may be null, in which case we do nothing.

If $value is an array, the array must look like this:

[ [ 'name' => '{namespaceUri}element-name', 'value' => '...', 'attributes' => [ 'attName' => 'attValue' ] ] [, 'name' => '{namespaceUri}element-name2', 'value' => '...', ] ]

This would result in xml like:

<element-name xmlns="namespaceUri" attName="attValue"> ... </element-name> <element-name2> ... </element-name2>

The value property may be any value standardSerializer supports, so you can nest data-structures this way. Both value and attributes are optional.

Alternatively, you can also specify the array using this syntax:

[ [ '{namespaceUri}element-name' => '...', '{namespaceUri}element-name2' => '...', ] ]

This is excellent for simple key->value structures, and here you can also specify anything for the value.

You can even mix the two array syntaxes.

Parameters
Writer$writer
string|int|float|bool|array|object
Returns
void

Definition at line 164 of file functions.php.

164 {
165
166 if (is_scalar($value)) {
167
168 // String, integer, float, boolean
169 $writer->text($value);
170
171 } elseif ($value instanceof XmlSerializable) {
172
173 // XmlSerializable classes or Element classes.
174 $value->xmlSerialize($writer);
175
176 } elseif (is_object($value) && isset($writer->classMap[get_class($value)])) {
177
178 // It's an object which class appears in the classmap.
179 $writer->classMap[get_class($value)]($writer, $value);
180
181 } elseif (is_callable($value)) {
182
183 // A callback
184 $value($writer);
185
186 } elseif (is_null($value)) {
187
188 // nothing!
189
190 } elseif (is_array($value) && array_key_exists('name', $value)) {
191
192 // if the array had a 'name' element, we assume that this array
193 // describes a 'name' and optionally 'attributes' and 'value'.
194
195 $name = $value['name'];
196 $attributes = isset($value['attributes']) ? $value['attributes'] : [];
197 $value = isset($value['value']) ? $value['value'] : null;
198
199 $writer->startElement($name);
201 $writer->write($value);
202 $writer->endElement();
203
204 } elseif (is_array($value)) {
205
206 foreach ($value as $name => $item) {
207
208 if (is_int($name)) {
209
210 // This item has a numeric index. We just loop through the
211 // array and throw it back in the writer.
212 standardSerializer($writer, $item);
213
214 } elseif (is_string($name) && is_array($item) && isset($item['attributes'])) {
215
216 // The key is used for a name, but $item has 'attributes' and
217 // possibly 'value'
218 $writer->startElement($name);
219 $writer->writeAttributes($item['attributes']);
220 if (isset($item['value'])) {
221 $writer->write($item['value']);
222 }
223 $writer->endElement();
224
225 } elseif (is_string($name)) {
226
227 // This was a plain key-value array.
228 $writer->startElement($name);
229 $writer->write($item);
230 $writer->endElement();
231
232 } else {
233
234 throw new InvalidArgumentException('The writer does not know how to serialize arrays with keys of type: ' . gettype($name));
235
236 }
237 }
238
239 } elseif (is_object($value)) {
240
241 throw new InvalidArgumentException('The writer cannot serialize objects of class: ' . get_class($value));
242
243 } else {
244
245 throw new InvalidArgumentException('The writer cannot serialize values of type: ' . gettype($value));
246
247 }
248
249}
startElement($name)
Opens a new element.
Definition: Writer.php:121
write($value)
Writes a value to the output stream.
Definition: Writer.php:98
writeAttributes(array $attributes)
Writes a list of attributes.
Definition: Writer.php:211
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
Objects implementing XmlSerializable can control how they are represented in Xml.
standardSerializer(Writer $writer, $value)
This function is the 'default' serializer that is able to serialize most things, and delegates to oth...
Definition: functions.php:164

References $attributes, $name, Sabre\Xml\Serializer\standardSerializer(), Sabre\Xml\Writer\startElement(), Sabre\Xml\Writer\write(), and Sabre\Xml\Writer\writeAttributes().

Referenced by Sabre\Xml\Serializer\standardSerializer(), and Sabre\Xml\Writer\write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ valueObject()

Sabre\Xml\Serializer\valueObject ( Writer  $writer,
  $valueObject,
  $namespace 
)

The valueObject serializer turns a simple PHP object into a classname.

Every public property will be encoded as an xml element with the same name, in the XML namespace as specified.

Values that are set to null or an empty array are not serialized. To serialize empty properties, you must specify them as an empty string.

Parameters
Writer$writer
object$valueObject
string$namespace

Definition at line 61 of file functions.php.

61 {
62 foreach (get_object_vars($valueObject) as $key => $val) {
63 if (is_array($val)) {
64 // If $val is an array, it has a special meaning. We need to
65 // generate one child element for each item in $val
66 foreach ($val as $child) {
67 $writer->writeElement('{' . $namespace . '}' . $key, $child);
68 }
69
70 } elseif ($val !== null) {
71 $writer->writeElement('{' . $namespace . '}' . $key, $val);
72 }
73 }
74}
$key
Definition: croninfo.php:18
if($err=$client->getError()) $namespace

References $key, $namespace, and Sabre\Xml\Writer\writeElement().

+ Here is the call graph for this function: