Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ vendor
composer.lock
tests/cov
.*.swp
.php_cs.cache

# Composer binaries
bin/phpunit
Expand Down
12 changes: 12 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

$config = PhpCsFixer\Config::create();
$config->getFinder()
->exclude('vendor')
->in(__DIR__);
$config->setRules([
'@PSR1' => true,
'@Symfony' =>true
]);

return $config;
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
}
},
"require-dev": {
"phpunit/phpunit" : "*"
"phpunit/phpunit" : "^6"
},
"config" : {
"bin-dir" : "bin/"
Expand Down
27 changes: 11 additions & 16 deletions lib/ContextStackTrait.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php declare (strict_types=1);
<?php

declare(strict_types=1);

namespace Sabre\Xml;

/**
* Context Stack
* Context Stack.
*
* The Context maintains information about a document during either reading or
* writing.
Expand All @@ -19,8 +21,8 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
trait ContextStackTrait {

trait ContextStackTrait
{
/**
* This is the element map. It contains a list of XML elements (in clark
* notation) as keys and PHP class names as values.
Expand Down Expand Up @@ -90,34 +92,27 @@ trait ContextStackTrait {
* 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 void
*/
function pushContext() {

public function pushContext()
{
$this->contextStack[] = [
$this->elementMap,
$this->contextUri,
$this->namespaceMap,
$this->classMap
$this->classMap,
];

}

/**
* Restore the previous "context".
*
* @return void
*/
function popContext() {

public function popContext()
{
list(
$this->elementMap,
$this->contextUri,
$this->namespaceMap,
$this->classMap
) = array_pop($this->contextStack);

}

}
68 changes: 32 additions & 36 deletions lib/Deserializer/functions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare (strict_types=1);
<?php

declare(strict_types=1);

namespace Sabre\Xml\Deserializer;

Expand Down Expand Up @@ -53,11 +55,12 @@
* 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.
*/
function keyValue(Reader $reader, string $namespace = null) : array {

function keyValue(Reader $reader, string $namespace = null): array
{
// If there's no children, we don't do anything.
if ($reader->isEmptyElement) {
$reader->next();

return [];
}

Expand All @@ -76,9 +79,8 @@ function keyValue(Reader $reader, string $namespace = null) : array {
$values = [];

do {

if ($reader->nodeType === Reader::ELEMENT) {
if ($namespace !== null && $reader->namespaceURI === $namespace) {
if (Reader::ELEMENT === $reader->nodeType) {
if (null !== $namespace && $reader->namespaceURI === $namespace) {
$values[$reader->localName] = $reader->parseCurrentElement()['value'];
} else {
$clark = $reader->getClark();
Expand All @@ -89,12 +91,11 @@ function keyValue(Reader $reader, string $namespace = null) : array {
break;
}
}
} while ($reader->nodeType !== Reader::END_ELEMENT);
} while (Reader::END_ELEMENT !== $reader->nodeType);

$reader->read();

return $values;

}

/**
Expand Down Expand Up @@ -143,11 +144,12 @@ function keyValue(Reader $reader, string $namespace = null) : array {
*
* @return string[]
*/
function enum(Reader $reader, string $namespace = null) : array {

function enum(Reader $reader, string $namespace = null): array
{
// If there's no children, we don't do anything.
if ($reader->isEmptyElement) {
$reader->next();

return [];
}
if (!$reader->read()) {
Expand All @@ -165,21 +167,19 @@ function enum(Reader $reader, string $namespace = null) : array {

$values = [];
do {

if ($reader->nodeType !== Reader::ELEMENT) {
if (Reader::ELEMENT !== $reader->nodeType) {
continue;
}
if (!is_null($namespace) && $namespace === $reader->namespaceURI) {
$values[] = $reader->localName;
} else {
$values[] = $reader->getClark();
}

} while ($reader->depth >= $currentDepth && $reader->next());

$reader->next();
return $values;

return $values;
}

/**
Expand All @@ -191,21 +191,20 @@ function enum(Reader $reader, string $namespace = null) : array {
*
* @return object
*/
function valueObject(Reader $reader, string $className, string $namespace) {

function valueObject(Reader $reader, string $className, string $namespace)
{
$valueObject = new $className();
if ($reader->isEmptyElement) {
$reader->next();

return $valueObject;
}

$defaultProperties = get_class_vars($className);

$reader->read();
do {

if ($reader->nodeType === Reader::ELEMENT && $reader->namespaceURI == $namespace) {

if (Reader::ELEMENT === $reader->nodeType && $reader->namespaceURI == $namespace) {
if (property_exists($valueObject, $reader->localName)) {
if (is_array($defaultProperties[$reader->localName])) {
$valueObject->{$reader->localName}[] = $reader->parseCurrentElement()['value'];
Expand All @@ -221,16 +220,16 @@ function valueObject(Reader $reader, string $className, string $namespace) {
break;
}
}
} while ($reader->nodeType !== Reader::END_ELEMENT);
} while (Reader::END_ELEMENT !== $reader->nodeType);

$reader->read();
return $valueObject;

return $valueObject;
}

/**
* This deserializer helps you deserialize xml structures that look like
* this:
* this:.
*
* <collection>
* <item>...</item>
Expand All @@ -252,28 +251,25 @@ function valueObject(Reader $reader, string $className, string $namespace) {
* $childElementName must either be a a clark-notation element name, or if no
* namespace is used, the bare element name.
*/
function repeatingElements(Reader $reader, string $childElementName) : array {

if ($childElementName[0] !== '{') {
$childElementName = '{}' . $childElementName;
function repeatingElements(Reader $reader, string $childElementName): array
{
if ('{' !== $childElementName[0]) {
$childElementName = '{}'.$childElementName;
}
$result = [];

foreach ($reader->parseGetElements() as $element) {

if ($element['name'] === $childElementName) {
$result[] = $element['value'];
}

}

return $result;

}

/**
* This deserializer helps you to deserialize structures which contain mixed content like this:
*
* This deserializer helps you to deserialize structures which contain mixed content like this:.
*
* <p>some text <extref>and a inline tag</extref>and even more text</p>
*
* The above example will return
Expand All @@ -290,11 +286,12 @@ function repeatingElements(Reader $reader, string $childElementName) : array {
*
* In strict XML documents you wont find this kind of markup but in html this is a quite common pattern.
*/
function mixedContent(Reader $reader) : array {

function mixedContent(Reader $reader): array
{
// If there's no children, we don't do anything.
if ($reader->isEmptyElement) {
$reader->next();

return [];
}

Expand All @@ -303,12 +300,12 @@ function mixedContent(Reader $reader) : array {
$content = [];
$reader->read();
while (true) {
if ($reader->nodeType == Reader::ELEMENT) {
if (Reader::ELEMENT == $reader->nodeType) {
$content[] = $reader->parseCurrentElement();
} elseif ($reader->depth >= $previousDepth && in_array($reader->nodeType, [Reader::TEXT, Reader::CDATA, Reader::WHITESPACE])) {
$content[] = $reader->value;
$reader->read();
} elseif ($reader->nodeType == Reader::END_ELEMENT) {
} elseif (Reader::END_ELEMENT == $reader->nodeType) {
// Ensuring we are moving the cursor after the end element.
$reader->read();
break;
Expand All @@ -318,5 +315,4 @@ function mixedContent(Reader $reader) : array {
}

return $content;

}
8 changes: 5 additions & 3 deletions lib/Element.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare (strict_types=1);
<?php

declare(strict_types=1);

namespace Sabre\Xml;

Expand All @@ -15,6 +17,6 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
interface Element extends XmlSerializable, XmlDeserializable {

interface Element extends XmlSerializable, XmlDeserializable
{
}
29 changes: 13 additions & 16 deletions lib/Element/Base.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare (strict_types=1);
<?php

declare(strict_types=1);

namespace Sabre\Xml\Element;

Expand All @@ -15,8 +17,8 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Base implements Xml\Element {

class Base implements Xml\Element
{
/**
* PHP value to serialize.
*
Expand All @@ -25,12 +27,11 @@ class Base implements Xml\Element {
protected $value;

/**
* Constructor
* Constructor.
*/
function __construct($value = null) {

public function __construct($value = null)
{
$this->value = $value;

}

/**
Expand All @@ -48,13 +49,10 @@ function __construct($value = null) {
* This allows serializers to be re-used for different element names.
*
* If you are opening new elements, you must also close them again.
*
* @return void
*/
function xmlSerialize(Xml\Writer $writer) {

public function xmlSerialize(Xml\Writer $writer)
{
$writer->write($this->value);

}

/**
Expand All @@ -77,11 +75,10 @@ function xmlSerialize(Xml\Writer $writer) {
*
* @return mixed
*/
static function xmlDeserialize(Xml\Reader $reader) {

public static function xmlDeserialize(Xml\Reader $reader)
{
$subTree = $reader->parseInnerTree();
return $subTree;

return $subTree;
}

}
Loading