2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-02 04:52:14 +01:00
|
|
|
namespace Symfony\Component\Serializer\Encoder;
|
|
|
|
|
|
|
|
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* Encodes XML data
|
|
|
|
*
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
* @author John Wards <jwards@whiteoctober.co.uk>
|
|
|
|
* @author Fabian Vogler <fabian@equivalence.ch>
|
|
|
|
*/
|
2011-06-06 21:34:09 +01:00
|
|
|
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-04-03 14:13:21 +01:00
|
|
|
private $dom;
|
|
|
|
private $format;
|
2013-07-03 14:43:12 +01:00
|
|
|
private $context;
|
2011-04-03 14:13:21 +01:00
|
|
|
private $rootNodeName = 'response';
|
2011-01-18 19:17:24 +00:00
|
|
|
|
2013-01-18 13:08:59 +00:00
|
|
|
/**
|
|
|
|
* Construct new XmlEncoder and allow to change the root node element name.
|
|
|
|
*
|
|
|
|
* @param string $rootNodeName
|
|
|
|
*/
|
|
|
|
public function __construct($rootNodeName = 'response')
|
|
|
|
{
|
|
|
|
$this->rootNodeName = $rootNodeName;
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-01-18 13:08:59 +00:00
|
|
|
public function encode($data, $format, array $context = array())
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
if ($data instanceof \DOMDocument) {
|
|
|
|
return $data->saveXML();
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:08:59 +00:00
|
|
|
$xmlRootNodeName = $this->resolveXmlRootName($context);
|
|
|
|
|
2013-07-05 09:06:42 +01:00
|
|
|
$this->dom = $this->createDomDocument($context);
|
2011-01-18 19:17:24 +00:00
|
|
|
$this->format = $format;
|
2013-07-03 14:43:12 +01:00
|
|
|
$this->context = $context;
|
2011-01-18 19:17:24 +00:00
|
|
|
|
2011-05-08 17:14:44 +01:00
|
|
|
if (null !== $data && !is_scalar($data)) {
|
2013-01-18 13:08:59 +00:00
|
|
|
$root = $this->dom->createElement($xmlRootNodeName);
|
2011-01-18 19:17:24 +00:00
|
|
|
$this->dom->appendChild($root);
|
2013-01-18 13:08:59 +00:00
|
|
|
$this->buildXml($root, $data, $xmlRootNodeName);
|
2011-01-30 09:10:27 +00:00
|
|
|
} else {
|
2013-01-18 13:08:59 +00:00
|
|
|
$this->appendNode($this->dom, $data, $xmlRootNodeName);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $this->dom->saveXML();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-01-18 13:08:59 +00:00
|
|
|
public function decode($data, $format, array $context = array())
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2012-02-24 12:26:13 +00:00
|
|
|
$internalErrors = libxml_use_internal_errors(true);
|
|
|
|
$disableEntities = libxml_disable_entity_loader(true);
|
|
|
|
libxml_clear_errors();
|
|
|
|
|
2012-08-26 17:38:30 +01:00
|
|
|
$dom = new \DOMDocument();
|
2012-08-27 12:36:18 +01:00
|
|
|
$dom->loadXML($data, LIBXML_NONET);
|
2012-08-26 17:38:30 +01:00
|
|
|
|
2012-02-24 12:26:13 +00:00
|
|
|
libxml_use_internal_errors($internalErrors);
|
|
|
|
libxml_disable_entity_loader($disableEntities);
|
|
|
|
|
2012-08-26 17:38:30 +01:00
|
|
|
foreach ($dom->childNodes as $child) {
|
|
|
|
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
|
|
|
|
throw new UnexpectedValueException('Document types are not allowed.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$xml = simplexml_import_dom($dom);
|
|
|
|
|
2012-02-24 12:26:13 +00:00
|
|
|
if ($error = libxml_get_last_error()) {
|
|
|
|
throw new UnexpectedValueException($error->message);
|
|
|
|
}
|
|
|
|
|
2011-04-27 16:00:31 +01:00
|
|
|
if (!$xml->count()) {
|
|
|
|
if (!$xml->attributes()) {
|
|
|
|
return (string) $xml;
|
|
|
|
}
|
2011-04-27 14:34:41 +01:00
|
|
|
$data = array();
|
|
|
|
foreach ($xml->attributes() as $attrkey => $attr) {
|
|
|
|
$data['@'.$attrkey] = (string) $attr;
|
|
|
|
}
|
|
|
|
$data['#'] = (string) $xml;
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-04-27 14:34:41 +01:00
|
|
|
return $data;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-04-12 10:18:17 +01:00
|
|
|
return $this->parseXml($xml);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 19:00:40 +00:00
|
|
|
/**
|
|
|
|
* Checks whether the serializer can encode to given format
|
|
|
|
*
|
|
|
|
* @param string $format format name
|
2012-11-16 16:06:12 +00:00
|
|
|
*
|
2011-11-08 19:00:40 +00:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function supportsEncoding($format)
|
|
|
|
{
|
|
|
|
return 'xml' === $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the serializer can decode from given format
|
|
|
|
*
|
|
|
|
* @param string $format format name
|
2012-11-16 16:06:12 +00:00
|
|
|
*
|
2011-11-08 19:00:40 +00:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function supportsDecoding($format)
|
|
|
|
{
|
|
|
|
return 'xml' === $format;
|
|
|
|
}
|
|
|
|
|
2011-02-13 14:58:52 +00:00
|
|
|
/**
|
|
|
|
* Sets the root node name
|
2012-11-16 16:06:12 +00:00
|
|
|
*
|
2011-02-13 14:58:52 +00:00
|
|
|
* @param string $name root node name
|
|
|
|
*/
|
|
|
|
public function setRootNodeName($name)
|
|
|
|
{
|
|
|
|
$this->rootNodeName = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the root node name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRootNodeName()
|
|
|
|
{
|
|
|
|
return $this->rootNodeName;
|
|
|
|
}
|
|
|
|
|
2011-04-03 15:21:53 +01:00
|
|
|
/**
|
|
|
|
* @param DOMNode $node
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param string $val
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-03 15:21:53 +01:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
final protected function appendXMLString($node, $val)
|
|
|
|
{
|
|
|
|
if (strlen($val) > 0) {
|
|
|
|
$frag = $this->dom->createDocumentFragment();
|
|
|
|
$frag->appendXML($val);
|
|
|
|
$node->appendChild($frag);
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-04-03 15:21:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DOMNode $node
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param string $val
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-03 15:21:53 +01:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
final protected function appendText($node, $val)
|
|
|
|
{
|
|
|
|
$nodeText = $this->dom->createTextNode($val);
|
|
|
|
$node->appendChild($nodeText);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DOMNode $node
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param string $val
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-03 15:21:53 +01:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
final protected function appendCData($node, $val)
|
|
|
|
{
|
|
|
|
$nodeText = $this->dom->createCDATASection($val);
|
|
|
|
$node->appendChild($nodeText);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param DOMNode $node
|
2011-04-03 15:21:53 +01:00
|
|
|
* @param DOMDocumentFragment $fragment
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-03 15:21:53 +01:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
final protected function appendDocumentFragment($node, $fragment)
|
|
|
|
{
|
|
|
|
if ($fragment instanceof \DOMDocumentFragment) {
|
|
|
|
$node->appendChild($fragment);
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-04-03 15:21:53 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-22 22:33:34 +01:00
|
|
|
* Checks the name is a valid xml element name
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-03 15:21:53 +01:00
|
|
|
* @param string $name
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-03 15:21:53 +01:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
final protected function isElementNameValid($name)
|
|
|
|
{
|
|
|
|
return $name &&
|
|
|
|
false === strpos($name, ' ') &&
|
|
|
|
preg_match('#^[\pL_][\pL0-9._-]*$#ui', $name);
|
|
|
|
}
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
2011-12-13 07:50:54 +00:00
|
|
|
* Parse the input SimpleXmlElement into an array.
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2011-04-12 10:18:17 +01:00
|
|
|
* @param SimpleXmlElement $node xml to parse
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-01-18 19:17:24 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-04-03 14:13:21 +01:00
|
|
|
private function parseXml($node)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
$data = array();
|
2011-04-27 14:34:41 +01:00
|
|
|
if ($node->attributes()) {
|
|
|
|
foreach ($node->attributes() as $attrkey => $attr) {
|
|
|
|
$data['@'.$attrkey] = (string) $attr;
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 10:18:17 +01:00
|
|
|
foreach ($node->children() as $key => $subnode) {
|
|
|
|
if ($subnode->count()) {
|
2011-01-18 19:17:24 +00:00
|
|
|
$value = $this->parseXml($subnode);
|
2011-04-12 10:18:17 +01:00
|
|
|
} elseif ($subnode->attributes()) {
|
|
|
|
$value = array();
|
|
|
|
foreach ($subnode->attributes() as $attrkey => $attr) {
|
|
|
|
$value['@'.$attrkey] = (string) $attr;
|
2011-02-14 09:42:58 +00:00
|
|
|
}
|
2011-04-12 10:18:17 +01:00
|
|
|
$value['#'] = (string) $subnode;
|
|
|
|
} else {
|
|
|
|
$value = (string) $subnode;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-11-24 17:09:38 +00:00
|
|
|
if ($key === 'item') {
|
2011-04-11 23:58:21 +01:00
|
|
|
if (isset($value['@key'])) {
|
2012-08-31 11:27:42 +01:00
|
|
|
if (isset($value['#'])) {
|
2013-06-05 00:48:30 +01:00
|
|
|
$data[$value['@key']] = $value['#'];
|
2012-08-31 11:27:42 +01:00
|
|
|
} else {
|
2013-06-05 00:48:30 +01:00
|
|
|
$data[$value['@key']] = $value;
|
2012-08-31 11:27:42 +01:00
|
|
|
}
|
2011-11-24 17:09:38 +00:00
|
|
|
} else {
|
|
|
|
$data['item'][] = $value;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2012-09-01 17:17:49 +01:00
|
|
|
} elseif (array_key_exists($key, $data) || $key == "entry") {
|
2011-04-12 10:18:17 +01:00
|
|
|
if ((false === is_array($data[$key])) || (false === isset($data[$key][0]))) {
|
|
|
|
$data[$key] = array($data[$key]);
|
2011-02-14 09:42:58 +00:00
|
|
|
}
|
2011-04-12 10:18:17 +01:00
|
|
|
$data[$key][] = $value;
|
2011-01-18 19:17:24 +00:00
|
|
|
} else {
|
2011-04-12 10:18:17 +01:00
|
|
|
$data[$key] = $value;
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the data and convert it to DOMElements
|
|
|
|
*
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param DOMNode $parentNode
|
|
|
|
* @param array|object $data data
|
2013-01-18 13:08:59 +00:00
|
|
|
* @param string $xmlRootNodeName
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-04-27 06:01:12 +01:00
|
|
|
* @return Boolean
|
2012-12-16 12:02:54 +00:00
|
|
|
*
|
|
|
|
* @throws UnexpectedValueException
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
2013-03-05 21:35:37 +00:00
|
|
|
private function buildXml($parentNode, $data, $xmlRootNodeName = null)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
$append = true;
|
|
|
|
|
|
|
|
if (is_array($data) || $data instanceof \Traversable) {
|
|
|
|
foreach ($data as $key => $data) {
|
2011-02-14 09:42:58 +00:00
|
|
|
//Ah this is the magic @ attribute types.
|
2011-12-13 07:50:54 +00:00
|
|
|
if (0 === strpos($key, "@") && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
2011-02-14 09:42:58 +00:00
|
|
|
$parentNode->setAttribute($attributeName, $data);
|
2011-04-11 23:58:21 +01:00
|
|
|
} elseif ($key === '#') {
|
|
|
|
$append = $this->selectNodeType($parentNode, $data);
|
2011-02-14 09:42:58 +00:00
|
|
|
} elseif (is_array($data) && false === is_numeric($key)) {
|
|
|
|
/**
|
2011-05-06 18:37:13 +01:00
|
|
|
* Is this array fully numeric keys?
|
|
|
|
*/
|
2011-02-27 19:56:29 +00:00
|
|
|
if (ctype_digit(implode('', array_keys($data)))) {
|
2011-02-14 09:42:58 +00:00
|
|
|
/**
|
2011-05-06 18:37:13 +01:00
|
|
|
* Create nodes to append to $parentNode based on the $key of this array
|
|
|
|
* Produces <xml><item>0</item><item>1</item></xml>
|
|
|
|
* From array("item" => array(0,1));
|
|
|
|
*/
|
2011-02-14 09:42:58 +00:00
|
|
|
foreach ($data as $subData) {
|
|
|
|
$append = $this->appendNode($parentNode, $subData, $key);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$append = $this->appendNode($parentNode, $data, $key);
|
|
|
|
}
|
2011-01-18 19:17:24 +00:00
|
|
|
} elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
|
|
|
|
$append = $this->appendNode($parentNode, $data, "item", $key);
|
|
|
|
} else {
|
|
|
|
$append = $this->appendNode($parentNode, $data, $key);
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $append;
|
|
|
|
}
|
2013-01-18 13:08:59 +00:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
if (is_object($data)) {
|
2013-07-03 14:43:12 +01:00
|
|
|
$data = $this->serializer->normalize($data, $this->format, $this->context);
|
2011-05-08 17:14:44 +01:00
|
|
|
if (null !== $data && !is_scalar($data)) {
|
2013-01-18 13:08:59 +00:00
|
|
|
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
|
2011-05-08 17:14:44 +01:00
|
|
|
}
|
2013-01-18 13:08:59 +00:00
|
|
|
|
2011-05-08 17:14:44 +01:00
|
|
|
// top level data object was normalized into a scalar
|
|
|
|
if (!$parentNode->parentNode->parentNode) {
|
|
|
|
$root = $parentNode->parentNode;
|
|
|
|
$root->removeChild($parentNode);
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2013-01-18 13:08:59 +00:00
|
|
|
return $this->appendNode($root, $data, $xmlRootNodeName);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-05-08 17:14:44 +01:00
|
|
|
return $this->appendNode($parentNode, $data, 'data');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
2013-01-18 13:08:59 +00:00
|
|
|
|
2013-04-10 11:24:37 +01:00
|
|
|
throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selects the type of node to create and appends it to the parent.
|
|
|
|
*
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param DOMNode $parentNode
|
|
|
|
* @param array|object $data
|
2011-12-13 07:50:54 +00:00
|
|
|
* @param string $nodeName
|
2011-04-23 16:05:44 +01:00
|
|
|
* @param string $key
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-06-04 16:30:56 +01:00
|
|
|
* @return Boolean
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
2011-04-03 14:13:21 +01:00
|
|
|
private function appendNode($parentNode, $data, $nodeName, $key = null)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
$node = $this->dom->createElement($nodeName);
|
|
|
|
if (null !== $key) {
|
|
|
|
$node->setAttribute('key', $key);
|
|
|
|
}
|
|
|
|
$appendNode = $this->selectNodeType($node, $data);
|
|
|
|
// we may have decided not to append this node, either in error or if its $nodeName is not valid
|
|
|
|
if ($appendNode) {
|
|
|
|
$parentNode->appendChild($node);
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $appendNode;
|
|
|
|
}
|
|
|
|
|
2012-01-17 13:18:32 +00:00
|
|
|
/**
|
|
|
|
* Checks if a value contains any characters which would require CDATA wrapping.
|
|
|
|
*
|
|
|
|
* @param string $val
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
private function needsCdataWrapping($val)
|
|
|
|
{
|
|
|
|
return preg_match('/[<>&]/', $val);
|
|
|
|
}
|
2012-01-17 09:51:09 +00:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* Tests the value being passed and decide what sort of element to create
|
|
|
|
*
|
|
|
|
* @param DOMNode $node
|
2012-05-18 18:41:48 +01:00
|
|
|
* @param mixed $val
|
2011-12-13 07:50:54 +00:00
|
|
|
*
|
2011-01-18 19:17:24 +00:00
|
|
|
* @return Boolean
|
|
|
|
*/
|
2011-04-03 14:13:21 +01:00
|
|
|
private function selectNodeType($node, $val)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
|
|
|
if (is_array($val)) {
|
2013-03-05 21:35:37 +00:00
|
|
|
return $this->buildXml($node, $val);
|
2011-01-18 19:17:24 +00:00
|
|
|
} elseif ($val instanceof \SimpleXMLElement) {
|
|
|
|
$child = $this->dom->importNode(dom_import_simplexml($val), true);
|
|
|
|
$node->appendChild($child);
|
|
|
|
} elseif ($val instanceof \Traversable) {
|
|
|
|
$this->buildXml($node, $val);
|
2011-02-12 11:55:25 +00:00
|
|
|
} elseif (is_object($val)) {
|
2013-07-03 14:43:12 +01:00
|
|
|
return $this->buildXml($node, $this->serializer->normalize($val, $this->format, $this->context));
|
2011-01-18 19:17:24 +00:00
|
|
|
} elseif (is_numeric($val)) {
|
|
|
|
return $this->appendText($node, (string) $val);
|
2012-01-17 10:25:25 +00:00
|
|
|
} elseif (is_string($val) && $this->needsCdataWrapping($val)) {
|
2011-01-18 19:17:24 +00:00
|
|
|
return $this->appendCData($node, $val);
|
2012-01-17 09:51:09 +00:00
|
|
|
} elseif (is_string($val)) {
|
|
|
|
return $this->appendText($node, $val);
|
2011-01-18 19:17:24 +00:00
|
|
|
} elseif (is_bool($val)) {
|
|
|
|
return $this->appendText($node, (int) $val);
|
2011-02-14 09:42:58 +00:00
|
|
|
} elseif ($val instanceof \DOMNode) {
|
2011-01-18 19:17:24 +00:00
|
|
|
$child = $this->dom->importNode($val, true);
|
|
|
|
$node->appendChild($child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-16 10:01:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get real XML root node name, taking serializer options into account.
|
|
|
|
*/
|
2013-01-18 13:08:59 +00:00
|
|
|
private function resolveXmlRootName(array $context = array())
|
2012-07-16 10:01:52 +01:00
|
|
|
{
|
2012-08-06 14:52:32 +01:00
|
|
|
return isset($context['xml_root_node_name'])
|
|
|
|
? $context['xml_root_node_name']
|
2012-07-16 10:01:52 +01:00
|
|
|
: $this->rootNodeName;
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:06:42 +01:00
|
|
|
/**
|
|
|
|
* Create a DOM document, taking serializer options into account.
|
|
|
|
*
|
|
|
|
* @param array $context options that the encoder has access to.
|
|
|
|
*
|
|
|
|
* @return \DOMDocument
|
|
|
|
*/
|
|
|
|
private function createDomDocument(array $context)
|
|
|
|
{
|
|
|
|
$document = new \DOMDocument();
|
|
|
|
|
|
|
|
// Set an attribute on the DOM document specifying, as part of the XML declaration,
|
|
|
|
$xmlOptions = array(
|
|
|
|
// the version number of the document
|
|
|
|
'xml_version' => 'xmlVersion',
|
|
|
|
// the encoding of the document
|
|
|
|
'xml_encoding' => 'encoding',
|
|
|
|
// whether the document is standalone
|
|
|
|
'xml_standalone' => 'xmlStandalone',
|
|
|
|
);
|
|
|
|
foreach ($xmlOptions as $xmlOption => $documentProperty) {
|
|
|
|
if (isset($context[$xmlOption])) {
|
|
|
|
$document->$documentProperty = $context[$xmlOption];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $document;
|
|
|
|
}
|
2011-01-30 09:10:27 +00:00
|
|
|
}
|