Service
in package
XML parsing and writing service.
You are encouraged to make a instance of this for your application and potentially extend it, as a central API point for dealing with xml and configuring the reader and writer.
Tags
Table of Contents
- $classMap : array<string|int, mixed>
- This is a list of custom serializers for specific classes.
- $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.
- $options : int
- A bitmask of the LIBXML_* constants.
- $valueObjectMap : mixed
- A list of classes and which XML elements they map to.
- expect() : array<string|int, mixed>|object|string
- Parses a document in full, and specify what the expected root element name is.
- getReader() : Reader
- Returns a fresh XML Reader.
- getWriter() : Writer
- Returns a fresh xml writer.
- mapValueObject() : mixed
- Map an xml element to a PHP class.
- parse() : array<string|int, mixed>|object|string
- Parses a document in full.
- parseClarkNotation() : array<string|int, mixed>
- Parses a clark-notation string, and returns the namespace and element name components.
- write() : string
- Generates an XML document in one go.
- writeValueObject() : mixed
- Writes a value object.
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)
$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.
$options
A bitmask of the LIBXML_* constants.
public
int
$options
= 0
$valueObjectMap
A list of classes and which XML elements they map to.
protected
mixed
$valueObjectMap
= []
Methods
expect()
Parses a document in full, and specify what the expected root element name is.
public
expect(string|array<string|int, string> $rootElementName, string|resource $input[, string $contextUri = null ]) : array<string|int, mixed>|object|string
This function works similar to parse, but the difference is that the user can specify what the expected name of the root element should be, in clark notation.
This is useful in cases where you expected a specific document to be passed, and reduces the amount of if statements.
It's also possible to pass an array of expected rootElements if your code may expect more than one document type.
Parameters
- $rootElementName : string|array<string|int, string>
- $input : string|resource
- $contextUri : string = null
Tags
Return values
array<string|int, mixed>|object|string —getReader()
Returns a fresh XML Reader.
public
getReader() : Reader
Return values
Reader —getWriter()
Returns a fresh xml writer.
public
getWriter() : Writer
Return values
Writer —mapValueObject()
Map an xml element to a PHP class.
public
mapValueObject(string $elementName, string $className) : mixed
Calling this function will automatically setup the Reader and Writer classes to turn a specific XML element to a PHP class.
For example, given a class such as :
class Author { public $firstName; public $lastName; }
and an XML element such as:
These can easily be mapped by calling:
$service->mapValueObject('{http://example.org}author', 'Author');
Parameters
- $elementName : string
- $className : string
Return values
mixed —parse()
Parses a document in full.
public
parse(string|resource $input[, string $contextUri = null ][, string &$rootElementName = null ]) : array<string|int, mixed>|object|string
Input may be specified as a string or readable stream resource. The returned value is the value of the root document.
Specifying the $contextUri allows the parser to figure out what the URI of the document was. This allows relative URIs within the document to be expanded easily.
The $rootElementName is specified by reference and will be populated with the root element name of the document.
Parameters
- $input : string|resource
- $contextUri : string = null
- $rootElementName : string = null
Tags
Return values
array<string|int, mixed>|object|string —parseClarkNotation()
Parses a clark-notation string, and returns the namespace and element name components.
public
static parseClarkNotation(string $str) : array<string|int, mixed>
If the string was invalid, it will throw an InvalidArgumentException.
Parameters
- $str : string
Tags
Return values
array<string|int, mixed> —write()
Generates an XML document in one go.
public
write(string $rootElementName, string|array<string|int, mixed>|object|XmlSerializable $value[, string $contextUri = null ]) : string
The $rootElement must be specified in clark notation. The value must be a string, an array or an object implementing XmlSerializable. Basically, anything that's supported by the Writer object.
$contextUri can be used to specify a sort of 'root' of the PHP application, in case the xml document is used as a http response.
This allows an implementor to easily create URI's relative to the root of the domain.
Parameters
- $rootElementName : string
- $value : string|array<string|int, mixed>|object|XmlSerializable
- $contextUri : string = null
Return values
string —writeValueObject()
Writes a value object.
public
writeValueObject(object $object[, string $contextUri = null ]) : mixed
This function largely behaves similar to write(), except that it's intended specifically to serialize a Value Object into an XML document.
The ValueObject must have been previously registered using mapValueObject().
Parameters
- $object : object
- $contextUri : string = null