In your composer.json
:
{
"require": {
"php": ">=5.4.0",
"selene/xml": "dev-development"
}
}
Run composer install
or composer update
$ composer install --dev
Run tests with:
$ vendor/bin/phpunit
The Parser
class can parse xml string, files, DOMDocuments, and DOMElements
to a php array.
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->parse('<data><foo>bar</foo></data>');
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->parse('/path/to/data.xml');
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->parseDom($dom);
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->parseDomElement($element);
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->setMergeAttributes(true);
You my specifay how keys are transformed by setting a key normalizer callback.
The default normalizer transforms dashes to underscores and camelcase to snakecase notation.
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->setKeyNormalizer(function ($key) {
// do string transfomations
return $key;
});
$parser->parseDomElement($element);
If attribute merging is disabled, use this to change the default attributes key
(default is @attributes
).
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->setAttributesKey('@attrs');
This forces the parser to treat nodes with a nodeName of the given key to be handled as list.
<?php
use \Selene\Components\Xml\Parser;
$parser = new Parser;
$parser->setIndexKey('item');
By default the parser will parse xml structures like
<entries>
<entry>1</entry>
<entry>2</entry>
</entries>
To something like:
<?php
['entries' => ['entry' => [1, 2]]]
Setting a pluralizer can fix this.
Note, that a pluralizer can be any callable that takes a string and returns a string.
<?php
$parser->setPluralizer(function ($string) {
if ('entry' === $string) {
return 'entries';
}
});
<?php
['entries' => [1, 2]]
<?php
use \Selene\Components\Xml\Writer;
$writer = new Writer;
$data = [
'foo' => 'bar'
];
$writer->dump($data); // <root><foo>bar</foo></root>
// set the xml root node name:
$writer->dump($data, 'data'); // <data><foo>bar</foo></data>
Note: this will create an instance of Selene\Components\Xml\Dom\DOMDocument
.
<?php
use \Selene\Components\Xml\Writer;
$writer = new Writer;
$data = [
'foo' => 'bar'
];
$dom = $writer->writeToDom($data);
##Writer options
Normaly, the NormalierInterface
implementation is set for you when instantiating a new Writer
, however you can set your own normalizer instance.
Note: the normalizer must implement the Selene\Components\Xml\Normalizer\NormalierInterface
interface.
<?php
use \Selene\Components\Xml\Writer;
use \Selene\Components\Xml\Normalizer\Normalizer;
$writer = new Writer(new Normalizer);
// or
$writer->setNormalizer($myNormalizer);
The inflector is the exact oppoite of the Parser's pluralizer. It singularizes strings.
<?php
$writer->setInflector(function ($string) {
if ('items' === $string) {
return 'item';
}
});
Default encoding is UTF-8
.
<?php
$writer->setEncoding($encoding); // string
This is usefull if you want to output certain keys as xml attribute
<?php
$writer->setKeyMap([
'nodeName' => ['id', 'entry'] // nested keys 'id' and 'entry' of the key
element 'nodeName' will be set as attributes instead of childnodes.
]);
Note: you can also use use addMappedAttribute($nodeName, $attributeName)
to add more mapped attributes.