for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
/**
* tubee.io
*
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0
*/
namespace Tubee\Endpoint\Xml;
class Converter
{
* Convert XMLElement into nicly formatted php array.
public static function xmlToArray($root)
$result = [];
if ($root->hasAttributes()) {
$attrs = $root->attributes;
foreach ($attrs as $attr) {
$result['@attributes'][$attr->name] = $attr->value;
}
if ($root->hasChildNodes()) {
$children = $root->childNodes;
if ($children->length == 1) {
$child = $children->item(0);
if ($child->nodeType === XML_TEXT_NODE || $child->nodeType === XML_CDATA_SECTION_NODE) {
$result['_value'] = $child->nodeValue;
return count($result) == 1 ? $result['_value'] : $result;
$groups = [];
foreach ($children as $child) {
if (!isset($result[$child->nodeName])) {
$result[$child->nodeName] = self::xmlToArray($child);
} else {
if (!isset($groups[$child->nodeName])) {
$result[$child->nodeName] = [$result[$child->nodeName]];
$groups[$child->nodeName] = 1;
$result[$child->nodeName][] = self::xmlToArray($child);
return $result;