Documentation

Reader extends XMLReader
in package
Uses ContextStackTrait

The Reader class expands upon PHP's built-in XMLReader.

The intended usage, is to assign certain XML elements to PHP classes. These need to be registered using the $elementMap public property.

After this is done, a single call to parse() will parse the entire document, and delegate sub-sections of the document to element classes.

Tags
copyright

Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).

author

Evert Pot (http://evertpot.com/)

license

http://sabre.io/license/ Modified BSD License

Table of Contents

$classMap  : array<string|int, mixed>
This is a list of custom serializers for specific classes.
$contextUri  : string|null
A contextUri pointing to the document being parsed / written.
$elementMap  : array<string|int, mixed>
This is the element map. It contains a list of XML elements (in clark notation) as keys and PHP class names as values.
$namespaceMap  : array<string|int, mixed>
This is a list of namespaces that you want to give default prefixes.
$contextStack  : array<string|int, mixed>
Backups of previous contexts.
getClark()  : string|null
Returns the current nodename in clark-notation.
getDeserializerForElementName()  : callable
Returns the function that should be used to parse the element identified by it's clark-notation name.
parse()  : array<string|int, mixed>
Reads the entire document.
parseAttributes()  : array<string|int, mixed>
Grabs all the attributes from the current element, and returns them as a key-value array.
parseCurrentElement()  : array<string|int, mixed>
Parses the current XML element.
parseGetElements()  : array<string|int, mixed>
parseGetElements parses everything in the current sub-tree, and returns a an array of elements.
parseInnerTree()  : array<string|int, mixed>|string|null
Parses all elements below the current element.
popContext()  : mixed
Restore the previous "context".
pushContext()  : mixed
Create a new "context".
readText()  : string
Reads all text below the current element, and returns this as a string.

Properties

$classMap

This is a list of custom serializers for specific classes.

public array<string|int, mixed> $classMap = []

The writer may use this if you attempt to serialize an object with a class that does not implement XmlSerializable.

Instead it will look at this classmap to see if there is a custom serializer here. This is useful if you don't want your value objects to be responsible for serializing themselves.

The keys in this classmap need to be fully qualified PHP class names, the values must be callbacks. The callbacks take two arguments. The writer class, and the value that must be written.

function (Writer $writer, object $value)

$contextUri

A contextUri pointing to the document being parsed / written.

public string|null $contextUri

This uri may be used to resolve relative urls that may appear in the document.

The reader and writer don't use this property, but as it's an extremely common use-case for parsing XML documents, it's added here as a convenience.

$elementMap

This is the element map. It contains a list of XML elements (in clark notation) as keys and PHP class names as values.

public array<string|int, mixed> $elementMap = []

The PHP class names must implement Sabre\Xml\Element.

Values may also be a callable. In that case the function will be called directly.

$namespaceMap

This is a list of namespaces that you want to give default prefixes.

public array<string|int, mixed> $namespaceMap = []

You must make sure you create this entire list before starting to write. They should be registered on the root element.

$contextStack

Backups of previous contexts.

protected array<string|int, mixed> $contextStack = []

Methods

getClark()

Returns the current nodename in clark-notation.

public getClark() : string|null

For example: "{http://www.w3.org/2005/Atom}feed". Or if no namespace is defined: "}feed".

This method returns null if we're not currently on an element.

Return values
string|null

getDeserializerForElementName()

Returns the function that should be used to parse the element identified by it's clark-notation name.

public getDeserializerForElementName(string $name) : callable
Parameters
$name : string
Return values
callable

parse()

Reads the entire document.

public parse() : array<string|int, mixed>

This function returns an array with the following three elements:

  • name - The root element name.
  • value - The value for the root element.
  • attributes - An array of attributes.

This function will also disable the standard libxml error handler (which usually just results in PHP errors), and throw exceptions instead.

Return values
array<string|int, mixed>

parseAttributes()

Grabs all the attributes from the current element, and returns them as a key-value array.

public parseAttributes() : array<string|int, mixed>

If the attributes are part of the same namespace, they will simply be short keys. If they are defined on a different namespace, the attribute name will be retured in clark-notation.

Return values
array<string|int, mixed>

parseCurrentElement()

Parses the current XML element.

public parseCurrentElement() : array<string|int, mixed>

This method returns arn array with 3 properties:

  • name - A clark-notation XML element name.
  • value - The parsed value.
  • attributes - A key-value list of attributes.
Return values
array<string|int, mixed>

parseGetElements()

parseGetElements parses everything in the current sub-tree, and returns a an array of elements.

public parseGetElements([array<string|int, mixed> $elementMap = null ]) : array<string|int, mixed>

Each element has a 'name', 'value' and 'attributes' key.

If the the element didn't contain sub-elements, an empty array is always returned. If there was any text inside the element, it will be discarded.

If the $elementMap argument is specified, the existing elementMap will be overridden while parsing the tree, and restored after this process.

Parameters
$elementMap : array<string|int, mixed> = null
Return values
array<string|int, mixed>

parseInnerTree()

Parses all elements below the current element.

public parseInnerTree([array<string|int, mixed> $elementMap = null ]) : array<string|int, mixed>|string|null

This method will return a string if this was a text-node, or an array if there were sub-elements.

If there's both text and sub-elements, the text will be discarded.

If the $elementMap argument is specified, the existing elementMap will be overridden while parsing the tree, and restored after this process.

Parameters
$elementMap : array<string|int, mixed> = null
Return values
array<string|int, mixed>|string|null

popContext()

Restore the previous "context".

public popContext() : mixed
Return values
mixed

pushContext()

Create a new "context".

public pushContext() : mixed

This allows you to safely modify the elementMap, contextUri or namespaceMap. After you're done, you can restore the old data again with popContext.

Return values
mixed

readText()

Reads all text below the current element, and returns this as a string.

public readText() : string
Return values
string

Search results