Download the PHP package donatorsky/php-xml-template-reader without Composer
On this page you can find all versions of the php package donatorsky/php-xml-template-reader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download donatorsky/php-xml-template-reader
More information about donatorsky/php-xml-template-reader
Files in donatorsky/php-xml-template-reader
Package php-xml-template-reader
Short Description The PHP XML Reader where you show how to read the XML, and it does the rest for you.
License MIT
Homepage https://github.com/donatorsky/php-xml-template-reader
Informations about the package php-xml-template-reader
PHP XML Template Reader
The PHP XML Reader where you show how to read the XML, and it does the rest for you.
How it works
The PHP XML Template Reader helps You to parse given XML file and create an object from it. The parser uses given template as a schema and tries to match it to input XML, optionally validating it with defined rules.
To start, simply create new \Donatorsky\XmlTemplate\Reader\XmlTemplateReader
object, pass it the template and read using one of available reading modes.
Example 1
Assuming the following XML:
You can already see a pattern, so You can define the template as follows:
As the output You will see an object of type \Donatorsky\XmlTemplate\Reader\Models\Node
(by default, can be changed) with processed data:
Please note, that only nodes defined in the template are present in the output Node. When XML changes, You need to update the template.
Example 2
The Reader supports namespaced nodes and attributes. In case suggested template's tpl
namespace conflicts with Yours, feel free to change it to any other XML valid value:
You can already see a pattern, so You can define the template as follows:
Reading modes
Multiple reading modes are available. Given the following example code:
read
: read XML from string
You can provide XML contents and parse it using read
method:
readFile
: read XML from file in given path
You can provide a path to the XML file and parse it using readFile
method:
readStream
: read XML from already opened resource
You can provide a resource with the XML contents and parse it using readStream
method:
open
, update
and close
: custom stream XML reading
You can read the XML chunk by chunk using You own implementation with open
, update
and close
methods:
Parsing modifiers
You can use various parsing modifiers to define some behaviours. Examples below use tpl
namespace.
tpl:castTo
Accepted values: class' FQN, must implement \Donatorsky\XmlTemplate\Reader\Models\Contracts\NodeInterface
.
By default, when node is parsed, it creates new \Donatorsky\XmlTemplate\Reader\Models\Node
instance with parsed data. However, You can use Your own class. This class must implement \Donatorsky\XmlTemplate\Reader\Models\Contracts\NodeInterface
interface.
Example
Define node classes:
Use them in the template:
The output:
tpl:collectAttributes
Accepted values: all
, validated
(default).
By default, only validated nodes' attributes are collected. This means, that only attributes that are defined in the template are collected. However, You can change it if You also want to collect other attributes.
Given the input XML:
Example 1
With the following template:
You will get:
Example 2
With the following template:
You will get:
tpl:contents
Accepted values: none
(default when tpl:type
= collection), raw
(default when tpl:type
= single), trimmed
.
By default, no node's contents is collected (none
). This is especially useful for nodes containing other nodes, thus the contents is only a bunch of whitespaces (when XML if pretty-printed). You can change this behaviour and collect raw, unchanged contents (raw
) of the node or additionally trim whitespaces (trimmed
).
Example
Given the input XML:
With the following template:
You will get:
tpl:type
Accepted values: single
(default), collection
.
By default, each node defined in the template is considered to be a single (single
). However, if You expect multiple elements of the same type, You can change it (collection
).
Example
Given the input XML:
With the following template:
You will get:
Rules
Rules are simple validators and transformers that can be chained. You can use rules to define attributes constraints and transform them to expected value. Rules may be aliased. Rules can accept additional attributes. Names and aliases are case-insensitive. You can examine built-in rules in src/Rules
directory, or You can create one by implementing \Donatorsky\XmlTemplate\Reader\Rules\Contracts\RuleInterface
interface.
Built-in rules
Rule name | Aliases | Validation | Transformation |
---|---|---|---|
callback | Custom validation rule | Custom transformation | |
float | Numeric value | Cast to float type | |
greaterThan:threshold | Numeric value greater than threshold | Value unchanged | |
int | integer | Numeric value | Cast to int type |
numeric | Numeric value | Cast to int or float type | |
required | Value must not be empty | Value unchanged | |
trim | String value | Value is trimmed |
Custom rules
To define custom rule You need to first create a class that implements RuleInterface:
Then, You need to register rule class:
And use it in the template:
All versions of php-xml-template-reader with dependencies
ext-libxml Version *
ext-simplexml Version *
ext-xml Version *
beberlei/assert Version ^3.3
symfony/event-dispatcher Version ^5.2
thecodingmachine/safe Version ^1.3