HummingBirdAnimeClient/src/API/XML.php

280 lines
5.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2018-10-01 11:35:51 -04:00
* PHP version 7.1
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-15 14:43:15 -05:00
* @copyright 2015 - 2018 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2018-10-01 11:35:51 -04:00
* @version 4.1
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API;
use DOMDocument, DOMNode, DOMNodeList, InvalidArgumentException;
/**
* XML <=> PHP Array codec
*/
final class XML {
/**
* XML representation of the data
*
* @var string
*/
private $xml;
/**
* PHP array version of the data
*
* @var array
*/
private $data;
/**
* XML constructor
2017-02-17 10:55:17 -05:00
*
* @param string $xml
* @param array $data
*/
public function __construct(string $xml = '', array $data = [])
{
$this->setXML($xml)->setData($data);
}
/**
* Serialize the data to an xml string
2017-02-17 10:55:17 -05:00
*
* @return string
*/
public function __toString(): string
{
return static::toXML($this->getData());
}
/**
* Get the data parsed from the XML
*
* @return array
*/
public function getData(): array
{
return $this->data;
}
/**
* Set the data to create xml from
*
* @param array $data
2017-02-17 10:55:17 -05:00
* @return self
*/
public function setData(array $data): self
{
$this->data = $data;
return $this;
}
/**
* Get the xml created from the data
*
* @return string
*/
public function getXML(): string
{
return $this->xml;
}
/**
* Set the xml to parse the data from
*
* @param string $xml
2017-02-17 10:55:17 -05:00
* @return self
*/
public function setXML(string $xml): self
{
$this->xml = $xml;
return $this;
}
/**
* Parse an xml document string to a php array
*
* @param string $xml
* @return array
*/
public static function toArray(string $xml): array
{
$data = [];
2017-02-04 15:18:34 -05:00
$xml = static::stripXMLWhitespace($xml);
$dom = new DOMDocument();
$hasLoaded = @$dom->loadXML($xml);
if ( ! $hasLoaded)
{
throw new InvalidArgumentException('Failed to load XML');
}
$root = $dom->documentElement;
$data[$root->tagName] = [];
if ($root->hasChildNodes())
{
static::childNodesToArray($data[$root->tagName], $root->childNodes);
}
return $data;
}
/**
* Transform the array into XML
*
* @param array $data
* @return string
*/
public static function toXML(array $data): string
{
$dom = new DOMDocument();
$dom->encoding = 'UTF-8';
static::arrayPropertiesToXmlNodes($dom, $dom, $data);
return $dom->saveXML();
}
/**
* Parse the xml document string to a php array
*
* @return array
*/
public function parse(): array
{
$xml = $this->getXML();
$data = static::toArray($xml);
return $this->setData($data)->getData();
}
/**
* Transform the array into XML
*
* @return string
*/
public function createXML(): string
{
return static::toXML($this->getData());
}
2017-02-17 10:55:17 -05:00
/**
* Strip whitespace from raw xml to remove irrelevant text nodes
*
* @param string $xml
* @return string
*/
2017-02-04 15:18:34 -05:00
private static function stripXMLWhitespace(string $xml): string
{
// Get rid of unimportant text nodes by removing
// whitespace characters from between xml tags,
// except for the xml declaration tag, Which looks
// something like:
/* <?xml version="1.0" encoding="UTF-8"?> */
2018-02-02 09:50:58 -05:00
return preg_replace('/([^?])>\s+</', '$1><', $xml);
2017-02-04 15:18:34 -05:00
}
/**
* Recursively create array structure based on xml structure
*
2017-02-17 10:55:17 -05:00
* @param array $root A reference to the current array location
* @param DOMNodeList $nodeList The current NodeList object
* @return void
*/
2018-02-02 09:50:58 -05:00
private static function childNodesToArray(array &$root, DOMNodelist $nodeList): void
{
$length = $nodeList->length;
for ($i = 0; $i < $length; $i++)
{
$el = $nodeList->item($i);
2017-01-27 09:43:42 -05:00
$current =& $root[$el->nodeName];
2017-02-04 15:18:34 -05:00
2017-01-27 09:43:42 -05:00
// It's a top level element!
2018-02-02 09:50:58 -05:00
if (( ! $el->hasChildNodes()) || is_a($el->childNodes->item(0), 'DomText'))
{
2017-01-27 09:43:42 -05:00
$current = $el->textContent;
continue;
}
2017-01-27 09:43:42 -05:00
// An empty value at the current root
2018-02-02 09:50:58 -05:00
if ($current === NULL)
2017-01-27 09:43:42 -05:00
{
$current = [];
static::childNodesToArray($current, $el->childNodes);
continue;
}
$keys = array_keys($current);
// Wrap the array in a containing array
// if there are only string keys
if ( ! is_numeric($keys[0]))
{
2017-01-27 09:43:42 -05:00
// But if there is only one key, don't wrap it in
// an array, just recurse to parse the child nodes
if (count($current) === 1)
{
static::childNodesToArray($current, $el->childNodes);
continue;
}
$current = [$current];
}
2017-01-27 09:43:42 -05:00
2018-02-02 09:50:58 -05:00
$current[] = [];
2017-01-27 09:43:42 -05:00
$index = count($current) - 1;
static::childNodesToArray($current[$index], $el->childNodes);
}
}
/**
* Recursively create xml nodes from array properties
*
* @param DOMDocument $dom The current DOM object
* @param DOMNode $parent The parent element to append children to
* @param array $data The data for the current node
* @return void
*/
2018-02-02 09:50:58 -05:00
private static function arrayPropertiesToXmlNodes(DOMDocument $dom, DOMNode $parent, array $data): void
{
foreach($data as $key => $props)
{
2017-01-27 09:43:42 -05:00
// 'Flatten' the array as you create the xml
if (is_numeric($key))
{
foreach($props as $key => $props)
{
break;
}
}
2017-02-04 15:18:34 -05:00
$node = $dom->createElement($key);
2017-02-04 15:18:34 -05:00
2018-02-02 09:50:58 -05:00
if (\is_array($props))
{
static::arrayPropertiesToXmlNodes($dom, $node, $props);
}
else
{
$tNode = $dom->createTextNode((string)$props);
$node->appendChild($tNode);
}
$parent->appendChild($node);
}
}
}