View source
<?php
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface {
private $dom;
private $format;
private $context;
private $rootNodeName = 'response';
public function __construct($rootNodeName = 'response') {
$this->rootNodeName = $rootNodeName;
}
public function encode($data, $format, array $context = array()) {
if ($data instanceof \DOMDocument) {
return $data
->saveXML();
}
$xmlRootNodeName = $this
->resolveXmlRootName($context);
$this->dom = $this
->createDomDocument($context);
$this->format = $format;
$this->context = $context;
if (null !== $data && !is_scalar($data)) {
$root = $this->dom
->createElement($xmlRootNodeName);
$this->dom
->appendChild($root);
$this
->buildXml($root, $data, $xmlRootNodeName);
}
else {
$this
->appendNode($this->dom, $data, $xmlRootNodeName);
}
return $this->dom
->saveXML();
}
public function decode($data, $format, array $context = array()) {
if ('' === trim($data)) {
throw new UnexpectedValueException('Invalid XML data, it can not be empty.');
}
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
libxml_clear_errors();
$dom = new \DOMDocument();
$dom
->loadXML($data, LIBXML_NONET | LIBXML_NOBLANKS);
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if ($error = libxml_get_last_error()) {
libxml_clear_errors();
throw new UnexpectedValueException($error->message);
}
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new UnexpectedValueException('Document types are not allowed.');
}
}
$rootNode = $dom->firstChild;
if ($rootNode
->hasChildNodes()) {
$xpath = new \DOMXPath($dom);
$data = array();
foreach ($xpath
->query('namespace::*', $dom->documentElement) as $nsNode) {
$data['@' . $nsNode->nodeName] = $nsNode->nodeValue;
}
unset($data['@xmlns:xml']);
if (empty($data)) {
return $this
->parseXml($rootNode);
}
return array_merge($data, (array) $this
->parseXml($rootNode));
}
if (!$rootNode
->hasAttributes()) {
return $rootNode->nodeValue;
}
$data = array();
foreach ($rootNode->attributes as $attrKey => $attr) {
$data['@' . $attrKey] = $attr->nodeValue;
}
$data['#'] = $rootNode->nodeValue;
return $data;
}
public function supportsEncoding($format) {
return 'xml' === $format;
}
public function supportsDecoding($format) {
return 'xml' === $format;
}
public function setRootNodeName($name) {
$this->rootNodeName = $name;
}
public function getRootNodeName() {
return $this->rootNodeName;
}
protected final function appendXMLString(\DOMNode $node, $val) {
if (strlen($val) > 0) {
$frag = $this->dom
->createDocumentFragment();
$frag
->appendXML($val);
$node
->appendChild($frag);
return true;
}
return false;
}
protected final function appendText(\DOMNode $node, $val) {
$nodeText = $this->dom
->createTextNode($val);
$node
->appendChild($nodeText);
return true;
}
protected final function appendCData(\DOMNode $node, $val) {
$nodeText = $this->dom
->createCDATASection($val);
$node
->appendChild($nodeText);
return true;
}
protected final function appendDocumentFragment(\DOMNode $node, $fragment) {
if ($fragment instanceof \DOMDocumentFragment) {
$node
->appendChild($fragment);
return true;
}
return false;
}
protected final function isElementNameValid($name) {
return $name && false === strpos($name, ' ') && preg_match('#^[\\pL_][\\pL0-9._:-]*$#ui', $name);
}
private function parseXml(\DOMNode $node) {
$data = $this
->parseXmlAttributes($node);
$value = $this
->parseXmlValue($node);
if (!count($data)) {
return $value;
}
if (!is_array($value)) {
$data['#'] = $value;
return $data;
}
if (1 === count($value) && key($value)) {
$data[key($value)] = current($value);
return $data;
}
foreach ($value as $key => $val) {
$data[$key] = $val;
}
return $data;
}
private function parseXmlAttributes(\DOMNode $node) {
if (!$node
->hasAttributes()) {
return array();
}
$data = array();
foreach ($node->attributes as $attr) {
if (ctype_digit($attr->nodeValue)) {
$data['@' . $attr->nodeName] = (int) $attr->nodeValue;
}
else {
$data['@' . $attr->nodeName] = $attr->nodeValue;
}
}
return $data;
}
private function parseXmlValue(\DOMNode $node) {
if (!$node
->hasChildNodes()) {
return $node->nodeValue;
}
if (1 === $node->childNodes->length && in_array($node->firstChild->nodeType, array(
XML_TEXT_NODE,
XML_CDATA_SECTION_NODE,
))) {
return $node->firstChild->nodeValue;
}
$value = array();
foreach ($node->childNodes as $subnode) {
$val = $this
->parseXml($subnode);
if ('item' === $subnode->nodeName && isset($val['@key'])) {
if (isset($val['#'])) {
$value[$val['@key']] = $val['#'];
}
else {
$value[$val['@key']] = $val;
}
}
else {
$value[$subnode->nodeName][] = $val;
}
}
foreach ($value as $key => $val) {
if (is_array($val) && 1 === count($val)) {
$value[$key] = current($val);
}
}
return $value;
}
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null) {
$append = true;
if (is_array($data) || $data instanceof \Traversable) {
foreach ($data as $key => $data) {
if (0 === strpos($key, '@') && is_scalar($data) && $this
->isElementNameValid($attributeName = substr($key, 1))) {
$parentNode
->setAttribute($attributeName, $data);
}
elseif ($key === '#') {
$append = $this
->selectNodeType($parentNode, $data);
}
elseif (is_array($data) && false === is_numeric($key)) {
if (ctype_digit(implode('', array_keys($data)))) {
foreach ($data as $subData) {
$append = $this
->appendNode($parentNode, $subData, $key);
}
}
else {
$append = $this
->appendNode($parentNode, $data, $key);
}
}
elseif (is_numeric($key) || !$this
->isElementNameValid($key)) {
$append = $this
->appendNode($parentNode, $data, 'item', $key);
}
else {
$append = $this
->appendNode($parentNode, $data, $key);
}
}
return $append;
}
if (is_object($data)) {
$data = $this->serializer
->normalize($data, $this->format, $this->context);
if (null !== $data && !is_scalar($data)) {
return $this
->buildXml($parentNode, $data, $xmlRootNodeName);
}
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
$root
->removeChild($parentNode);
return $this
->appendNode($root, $data, $xmlRootNodeName);
}
return $this
->appendNode($parentNode, $data, 'data');
}
throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
}
private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null) {
$node = $this->dom
->createElement($nodeName);
if (null !== $key) {
$node
->setAttribute('key', $key);
}
$appendNode = $this
->selectNodeType($node, $data);
if ($appendNode) {
$parentNode
->appendChild($node);
}
return $appendNode;
}
private function needsCdataWrapping($val) {
return preg_match('/[<>&]/', $val);
}
private function selectNodeType(\DOMNode $node, $val) {
if (is_array($val)) {
return $this
->buildXml($node, $val);
}
elseif ($val instanceof \SimpleXMLElement) {
$child = $this->dom
->importNode(dom_import_simplexml($val), true);
$node
->appendChild($child);
}
elseif ($val instanceof \Traversable) {
$this
->buildXml($node, $val);
}
elseif (is_object($val)) {
return $this
->buildXml($node, $this->serializer
->normalize($val, $this->format, $this->context));
}
elseif (is_numeric($val)) {
return $this
->appendText($node, (string) $val);
}
elseif (is_string($val) && $this
->needsCdataWrapping($val)) {
return $this
->appendCData($node, $val);
}
elseif (is_string($val)) {
return $this
->appendText($node, $val);
}
elseif (is_bool($val)) {
return $this
->appendText($node, (int) $val);
}
elseif ($val instanceof \DOMNode) {
$child = $this->dom
->importNode($val, true);
$node
->appendChild($child);
}
return true;
}
private function resolveXmlRootName(array $context = array()) {
return isset($context['xml_root_node_name']) ? $context['xml_root_node_name'] : $this->rootNodeName;
}
private function createDomDocument(array $context) {
$document = new \DOMDocument();
$xmlOptions = array(
'xml_format_output' => 'formatOutput',
'xml_version' => 'xmlVersion',
'xml_encoding' => 'encoding',
'xml_standalone' => 'xmlStandalone',
);
foreach ($xmlOptions as $xmlOption => $documentProperty) {
if (isset($context[$xmlOption])) {
$document->{$documentProperty} = $context[$xmlOption];
}
}
return $document;
}
}