Deserializer
Interfaces, Classes and Traits
- EnumTest
- FunctionCallerTest
- Person
- Address
- Language
- KeyValueTest
- MixedContentTest
- RepeatingElementsTest
- ValueObjectTest
- TestVo
Table of Contents
- keyValue() : array<string|int, mixed>
- The 'keyValue' deserializer parses all child elements, and outputs them as a "key=>value" array.
- enum() : array<string|int, string>
- The 'enum' deserializer parses elements into a simple list without values or attributes.
- valueObject() : object
- The valueObject deserializer turns an xml element into a PHP object of a specific class.
- repeatingElements() : array<string|int, mixed>
- This deserializer helps you deserialize xml structures that look like this:.
- mixedContent() : array<string|int, mixed>
- This deserializer helps you to deserialize structures which contain mixed content like this:.
- functionCaller() : mixed
- The functionCaller deserializer turns an xml element into whatever your callable return.
- newAddressFromXml() : Address
Functions
keyValue()
The 'keyValue' deserializer parses all child elements, and outputs them as a "key=>value" array.
keyValue(Reader $reader[, string $namespace = null ]) : array<string|int, mixed>
For example, keyvalue will parse:
<s:root xmlns:s="http://sabredav.org/ns"> <s:elem1>value1</s:elem1> <s:elem2>value2</s:elem2> <s:elem3 /> </s:root>
Into:
[ "{http://sabredav.org/ns}elem1" => "value1", "{http://sabredav.org/ns}elem2" => "value2", "{http://sabredav.org/ns}elem3" => null, ];
If you specify the 'namespace' argument, the deserializer will remove the namespaces of the keys that match that namespace.
For example, if you call keyValue like this:
keyValue($reader, 'http://sabredav.org/ns')
it's output will instead be:
[ "elem1" => "value1", "elem2" => "value2", "elem3" => null, ];
Attributes will be removed from the top-level elements. If elements with the same name appear twice in the list, only the last one will be kept.
Parameters
- $reader : Reader
- $namespace : string = null
Return values
array<string|int, mixed> —enum()
The 'enum' deserializer parses elements into a simple list without values or attributes.
enum(Reader $reader[, string $namespace = null ]) : array<string|int, string>
For example, Elements will parse:
Parameters
- $reader : Reader
- $namespace : string = null
Return values
array<string|int, string> —valueObject()
The valueObject deserializer turns an xml element into a PHP object of a specific class.
valueObject(Reader $reader, string $className, string $namespace) : object
This is primarily used by the mapValueObject function from the Service class, but it can also easily be used for more specific situations.
Parameters
- $reader : Reader
- $className : string
- $namespace : string
Return values
object —repeatingElements()
This deserializer helps you deserialize xml structures that look like this:.
repeatingElements(Reader $reader, string $childElementName) : array<string|int, mixed>
Many XML documents use patterns like that, and this deserializer allow you to get all the 'items' as an array.
In that previous example, you would register the deserializer as such:
$reader->elementMap['}collection'] = function($reader) { return repeatingElements($reader, '}item'); }
The repeatingElements deserializer simply returns everything as an array.
$childElementName must either be a a clark-notation element name, or if no namespace is used, the bare element name.
Parameters
- $reader : Reader
- $childElementName : string
Return values
array<string|int, mixed> —mixedContent()
This deserializer helps you to deserialize structures which contain mixed content like this:.
mixedContent(Reader $reader) : array<string|int, mixed>
some text
The above example will return
[ 'some text', [ 'name' => '}extref', 'value' => 'and a inline tag', 'attributes' => [] ], 'and even more text' ]
In strict XML documents you wont find this kind of markup but in html this is a quite common pattern.
Parameters
- $reader : Reader
Return values
array<string|int, mixed> —functionCaller()
The functionCaller deserializer turns an xml element into whatever your callable return.
functionCaller(Reader $reader, callable $func, string $namespace) : mixed
You can use, e.g., a named constructor (factory method) to create an object using this function.
Parameters
- $reader : Reader
- $func : callable
- $namespace : string
Return values
mixed —newAddressFromXml()
newAddressFromXml(string $street, string $number) : Address
Parameters
- $street : string
- $number : string