Upgrade 1-11.38

This commit is contained in:
xesmyd
2026-03-30 14:10:30 +02:00
parent f2a7e6d1fc
commit ac648ef29d
24665 changed files with 69682 additions and 2205004 deletions
+151 -153
View File
@@ -13,38 +13,76 @@ namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\BadMethodCallException;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;
/**
* Encodes XML data.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author John Wards <jwards@whiteoctober.co.uk>
* @author Fabian Vogler <fabian@equivalence.ch>
* @author Kévin Dunglas <dunglas@gmail.com>
* @author Dany Maillard <danymaillard93b@gmail.com>
*/
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface, SerializerAwareInterface
{
const FORMAT = 'xml';
use SerializerAwareTrait;
public const FORMAT = 'xml';
public const AS_COLLECTION = 'as_collection';
/**
* @var \DOMDocument
* An array of ignored XML node types while decoding, each one of the DOM Predefined XML_* constants.
*/
private $dom;
private $format;
private $context;
private $rootNodeName = 'response';
private $loadOptions;
public const DECODER_IGNORED_NODE_TYPES = 'decoder_ignored_node_types';
/**
* Construct new XmlEncoder and allow to change the root node element name.
*
* @param string $rootNodeName
* @param int|null $loadOptions A bit field of LIBXML_* constants
* An array of ignored XML node types while encoding, each one of the DOM Predefined XML_* constants.
*/
public function __construct($rootNodeName = 'response', $loadOptions = null)
public const ENCODER_IGNORED_NODE_TYPES = 'encoder_ignored_node_types';
public const ENCODING = 'xml_encoding';
public const FORMAT_OUTPUT = 'xml_format_output';
/**
* A bit field of LIBXML_* constants.
*/
public const LOAD_OPTIONS = 'load_options';
public const REMOVE_EMPTY_TAGS = 'remove_empty_tags';
public const ROOT_NODE_NAME = 'xml_root_node_name';
public const STANDALONE = 'xml_standalone';
/** @deprecated The constant TYPE_CASE_ATTRIBUTES is deprecated since version 4.4 and will be removed in version 5. Use TYPE_CAST_ATTRIBUTES instead. */
public const TYPE_CASE_ATTRIBUTES = 'xml_type_cast_attributes';
public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
public const VERSION = 'xml_version';
private $defaultContext = [
self::AS_COLLECTION => false,
self::DECODER_IGNORED_NODE_TYPES => [\XML_PI_NODE, \XML_COMMENT_NODE],
self::ENCODER_IGNORED_NODE_TYPES => [],
self::LOAD_OPTIONS => \LIBXML_NONET | \LIBXML_NOBLANKS,
self::REMOVE_EMPTY_TAGS => false,
self::ROOT_NODE_NAME => 'response',
self::TYPE_CAST_ATTRIBUTES => true,
];
/**
* @param array $defaultContext
*/
public function __construct($defaultContext = [], int $loadOptions = null, array $decoderIgnoredNodeTypes = [\XML_PI_NODE, \XML_COMMENT_NODE], array $encoderIgnoredNodeTypes = [])
{
$this->rootNodeName = $rootNodeName;
$this->loadOptions = null !== $loadOptions ? $loadOptions : \LIBXML_NONET | \LIBXML_NOBLANKS;
if (!\is_array($defaultContext)) {
@trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', \E_USER_DEPRECATED);
$defaultContext = [
self::DECODER_IGNORED_NODE_TYPES => $decoderIgnoredNodeTypes,
self::ENCODER_IGNORED_NODE_TYPES => $encoderIgnoredNodeTypes,
self::LOAD_OPTIONS => $loadOptions ?? \LIBXML_NONET | \LIBXML_NOBLANKS,
self::ROOT_NODE_NAME => (string) $defaultContext,
];
}
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
/**
@@ -52,25 +90,25 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
*/
public function encode($data, $format, array $context = [])
{
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
$ignorePiNode = \in_array(\XML_PI_NODE, $encoderIgnoredNodeTypes, true);
if ($data instanceof \DOMDocument) {
return $data->saveXML();
return $data->saveXML($ignorePiNode ? $data->documentElement : null);
}
$xmlRootNodeName = $this->resolveXmlRootName($context);
$xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
$this->dom = $this->createDomDocument($context);
$this->format = $format;
$this->context = $context;
$dom = $this->createDomDocument($context);
if (null !== $data && !is_scalar($data)) {
$root = $this->dom->createElement($xmlRootNodeName);
$this->dom->appendChild($root);
$this->buildXml($root, $data, $xmlRootNodeName);
if (null !== $data && !\is_scalar($data)) {
$root = $dom->createElement($xmlRootNodeName);
$dom->appendChild($root);
$this->buildXml($root, $data, $format, $context, $xmlRootNodeName);
} else {
$this->appendNode($this->dom, $data, $xmlRootNodeName);
$this->appendNode($dom, $data, $format, $context, $xmlRootNodeName);
}
return $this->dom->saveXML();
return $dom->saveXML($ignorePiNode ? $dom->documentElement : null);
}
/**
@@ -89,7 +127,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
libxml_clear_errors();
$dom = new \DOMDocument();
$dom->loadXML($data, $this->loadOptions);
$dom->loadXML($data, $context[self::LOAD_OPTIONS] ?? $this->defaultContext[self::LOAD_OPTIONS]);
libxml_use_internal_errors($internalErrors);
if (\LIBXML_VERSION < 20900) {
@@ -103,11 +141,15 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
$rootNode = null;
$decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
foreach ($dom->childNodes as $child) {
if (\in_array($child->nodeType, $decoderIgnoredNodeTypes, true)) {
continue;
}
if (\XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
throw new NotEncodableValueException('Document types are not allowed.');
}
if (!$rootNode && \XML_PI_NODE !== $child->nodeType) {
if (!$rootNode) {
$rootNode = $child;
}
}
@@ -164,32 +206,35 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Sets the root node name.
*
* @deprecated since Symfony 4.2
*
* @param string $name Root node name
*/
public function setRootNodeName($name)
{
$this->rootNodeName = $name;
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the context instead.', __METHOD__), \E_USER_DEPRECATED);
$this->defaultContext[self::ROOT_NODE_NAME] = $name;
}
/**
* Returns the root node name.
*
* @deprecated since Symfony 4.2
*
* @return string
*/
public function getRootNodeName()
{
return $this->rootNodeName;
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the context instead.', __METHOD__), \E_USER_DEPRECATED);
return $this->defaultContext[self::ROOT_NODE_NAME];
}
/**
* @param string $val
*
* @return bool
*/
final protected function appendXMLString(\DOMNode $node, $val)
final protected function appendXMLString(\DOMNode $node, string $val): bool
{
if (\strlen($val) > 0) {
$frag = $this->dom->createDocumentFragment();
if ('' !== $val) {
$frag = $node->ownerDocument->createDocumentFragment();
$frag->appendXML($val);
$node->appendChild($frag);
@@ -199,27 +244,17 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
return false;
}
/**
* @param string $val
*
* @return bool
*/
final protected function appendText(\DOMNode $node, $val)
final protected function appendText(\DOMNode $node, string $val): bool
{
$nodeText = $this->dom->createTextNode($val);
$nodeText = $node->ownerDocument->createTextNode($val);
$node->appendChild($nodeText);
return true;
}
/**
* @param string $val
*
* @return bool
*/
final protected function appendCData(\DOMNode $node, $val)
final protected function appendCData(\DOMNode $node, string $val): bool
{
$nodeText = $this->dom->createCDATASection($val);
$nodeText = $node->ownerDocument->createCDATASection($val);
$node->appendChild($nodeText);
return true;
@@ -227,10 +262,8 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* @param \DOMDocumentFragment $fragment
*
* @return bool
*/
final protected function appendDocumentFragment(\DOMNode $node, $fragment)
final protected function appendDocumentFragment(\DOMNode $node, $fragment): bool
{
if ($fragment instanceof \DOMDocumentFragment) {
$node->appendChild($fragment);
@@ -241,17 +274,20 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
return false;
}
final protected function appendComment(\DOMNode $node, string $data): bool
{
$node->appendChild($node->ownerDocument->createComment($data));
return true;
}
/**
* Checks the name is a valid xml element name.
*
* @param string $name
*
* @return bool
*/
final protected function isElementNameValid($name)
final protected function isElementNameValid(string $name): bool
{
return $name &&
false === strpos($name, ' ') &&
!str_contains($name, ' ') &&
preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
}
@@ -291,17 +327,15 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Parse the input DOMNode attributes into an array.
*
* @return array
*/
private function parseXmlAttributes(\DOMNode $node, array $context = [])
private function parseXmlAttributes(\DOMNode $node, array $context = []): array
{
if (!$node->hasAttributes()) {
return [];
}
$data = [];
$typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);
$typeCastAttributes = (bool) ($context[self::TYPE_CAST_ATTRIBUTES] ?? $this->defaultContext[self::TYPE_CAST_ATTRIBUTES]);
foreach ($node->attributes as $attr) {
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes || (isset($attr->nodeValue[1]) && '0' === $attr->nodeValue[0] && '.' !== $attr->nodeValue[1])) {
@@ -338,27 +372,24 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
$value = [];
$decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
foreach ($node->childNodes as $subnode) {
if (\XML_PI_NODE === $subnode->nodeType) {
if (\in_array($subnode->nodeType, $decoderIgnoredNodeTypes, true)) {
continue;
}
$val = $this->parseXml($subnode, $context);
if ('item' === $subnode->nodeName && isset($val['@key'])) {
if (isset($val['#'])) {
$value[$val['@key']] = $val['#'];
} else {
$value[$val['@key']] = $val;
}
$value[$val['@key']] = $val['#'] ?? $val;
} else {
$value[$subnode->nodeName][] = $val;
}
}
$asCollection = $context[self::AS_COLLECTION] ?? $this->defaultContext[self::AS_COLLECTION];
foreach ($value as $key => $val) {
if (\is_array($val) && 1 === \count($val)) {
if (!$asCollection && \is_array($val) && 1 === \count($val)) {
$value[$key] = current($val);
}
}
@@ -370,26 +401,32 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
* Parse the data and convert it to DOMElements.
*
* @param array|object $data
* @param string|null $xmlRootNodeName
*
* @return bool
*
* @throws NotEncodableValueException
*/
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
private function buildXml(\DOMNode $parentNode, $data, string $format, array $context, string $xmlRootNodeName = null): bool
{
$append = true;
$removeEmptyTags = $context[self::REMOVE_EMPTY_TAGS] ?? $this->defaultContext[self::REMOVE_EMPTY_TAGS] ?? false;
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) {
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $format)))) {
foreach ($data as $key => $data) {
//Ah this is the magic @ attribute types.
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
if (!is_scalar($data)) {
$data = $this->serializer->normalize($data, $this->format, $this->context);
// Ah this is the magic @ attribute types.
if (str_starts_with($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
if (!\is_scalar($data)) {
$data = $this->serializer->normalize($data, $format, $context);
}
if (\is_bool($data)) {
$data = (int) $data;
}
$parentNode->setAttribute($attributeName, $data);
} elseif ('#' === $key) {
$append = $this->selectNodeType($parentNode, $data);
$append = $this->selectNodeType($parentNode, $data, $format, $context);
} elseif ('#comment' === $key) {
if (!\in_array(\XML_COMMENT_NODE, $encoderIgnoredNodeTypes, true)) {
$append = $this->appendComment($parentNode, $data);
}
} elseif (\is_array($data) && false === is_numeric($key)) {
// Is this array fully numeric keys?
if (ctype_digit(implode('', array_keys($data)))) {
@@ -399,15 +436,15 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
* From ["item" => [0,1]];.
*/
foreach ($data as $subData) {
$append = $this->appendNode($parentNode, $subData, $key);
$append = $this->appendNode($parentNode, $subData, $format, $context, $key);
}
} else {
$append = $this->appendNode($parentNode, $data, $key);
$append = $this->appendNode($parentNode, $data, $format, $context, $key);
}
} elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
$append = $this->appendNode($parentNode, $data, 'item', $key);
} elseif (null !== $data || !isset($this->context['remove_empty_tags']) || false === $this->context['remove_empty_tags']) {
$append = $this->appendNode($parentNode, $data, $key);
$append = $this->appendNode($parentNode, $data, $format, $context, 'item', $key);
} elseif (null !== $data || !$removeEmptyTags) {
$append = $this->appendNode($parentNode, $data, $format, $context, $key);
}
}
@@ -419,9 +456,9 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__));
}
$data = $this->serializer->normalize($data, $this->format, $this->context);
if (null !== $data && !is_scalar($data)) {
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
$data = $this->serializer->normalize($data, $format, $context);
if (null !== $data && !\is_scalar($data)) {
return $this->buildXml($parentNode, $data, $format, $context, $xmlRootNodeName);
}
// top level data object was normalized into a scalar
@@ -429,10 +466,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$root = $parentNode->parentNode;
$root->removeChild($parentNode);
return $this->appendNode($root, $data, $xmlRootNodeName);
return $this->appendNode($root, $data, $format, $context, $xmlRootNodeName);
}
return $this->appendNode($parentNode, $data, 'data');
return $this->appendNode($parentNode, $data, $format, $context, 'data');
}
throw new NotEncodableValueException('An unexpected value could not be serialized: '.(!\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
@@ -442,18 +479,15 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
* Selects the type of node to create and appends it to the parent.
*
* @param array|object $data
* @param string $nodeName
* @param string $key
*
* @return bool
*/
private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
private function appendNode(\DOMNode $parentNode, $data, string $format, array $context, string $nodeName, string $key = null): bool
{
$node = $this->dom->createElement($nodeName);
$dom = $parentNode instanceof \DOMDocument ? $parentNode : $parentNode->ownerDocument;
$node = $dom->createElement($nodeName);
if (null !== $key) {
$node->setAttribute('key', $key);
}
$appendNode = $this->selectNodeType($node, $data);
$appendNode = $this->selectNodeType($node, $data, $format, $context);
// we may have decided not to append this node, either in error or if its $nodeName is not valid
if ($appendNode) {
$parentNode->appendChild($node);
@@ -464,43 +498,35 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
/**
* Checks if a value contains any characters which would require CDATA wrapping.
*
* @param string $val
*
* @return bool
*/
private function needsCdataWrapping($val)
private function needsCdataWrapping(string $val): bool
{
return 0 < preg_match('/[<>&]/', $val);
return preg_match('/[<>&]/', $val);
}
/**
* Tests the value being passed and decide what sort of element to create.
*
* @param mixed $val
*
* @return bool
*
* @throws NotEncodableValueException
*/
private function selectNodeType(\DOMNode $node, $val)
private function selectNodeType(\DOMNode $node, $val, string $format, array $context): bool
{
if (\is_array($val)) {
return $this->buildXml($node, $val);
return $this->buildXml($node, $val, $format, $context);
} elseif ($val instanceof \SimpleXMLElement) {
$child = $this->dom->importNode(dom_import_simplexml($val), true);
$child = $node->ownerDocument->importNode(dom_import_simplexml($val), true);
$node->appendChild($child);
} elseif ($val instanceof \Traversable) {
$this->buildXml($node, $val);
$this->buildXml($node, $val, $format, $context);
} elseif ($val instanceof \DOMNode) {
$child = $this->dom->importNode($val, true);
$child = $node->ownerDocument->importNode($val, true);
$node->appendChild($child);
} elseif (\is_object($val)) {
if (null === $this->serializer) {
throw new BadMethodCallException(sprintf('The serializer needs to be set to allow "%s()" to be used with object data.', __METHOD__));
}
return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
return $this->selectNodeType($node, $this->serializer->normalize($val, $format, $context), $format, $context);
} elseif (is_numeric($val)) {
return $this->appendText($node, (string) $val);
} elseif (\is_string($val) && $this->needsCdataWrapping($val)) {
@@ -514,55 +540,27 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
return true;
}
/**
* Get real XML root node name, taking serializer options into account.
*
* @return string
*/
private function resolveXmlRootName(array $context = [])
{
return isset($context['xml_root_node_name'])
? $context['xml_root_node_name']
: $this->rootNodeName;
}
/**
* Get XML option for type casting attributes Defaults to true.
*
* @return bool
*/
private function resolveXmlTypeCastAttributes(array $context = [])
{
return isset($context['xml_type_cast_attributes'])
? (bool) $context['xml_type_cast_attributes']
: true;
}
/**
* 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)
private function createDomDocument(array $context): \DOMDocument
{
$document = new \DOMDocument();
// Set an attribute on the DOM document specifying, as part of the XML declaration,
$xmlOptions = [
// nicely formats output with indentation and extra space
'xml_format_output' => 'formatOutput',
self::FORMAT_OUTPUT => 'formatOutput',
// the version number of the document
'xml_version' => 'xmlVersion',
self::VERSION => 'xmlVersion',
// the encoding of the document
'xml_encoding' => 'encoding',
self::ENCODING => 'encoding',
// whether the document is standalone
'xml_standalone' => 'xmlStandalone',
self::STANDALONE => 'xmlStandalone',
];
foreach ($xmlOptions as $xmlOption => $documentProperty) {
if (isset($context[$xmlOption])) {
$document->$documentProperty = $context[$xmlOption];
if ($contextOption = $context[$xmlOption] ?? $this->defaultContext[$xmlOption] ?? false) {
$document->$documentProperty = $contextOption;
}
}