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
+9 -11
View File
@@ -20,9 +20,9 @@ use Symfony\Component\Serializer\Exception\RuntimeException;
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*
* @final since version 3.3.
* @final
*/
class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*/
class ChainDecoder implements ContextAwareDecoderInterface
{
protected $decoders = [];
protected $decoderByFormat = [];
@@ -43,10 +43,8 @@ class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*
/**
* {@inheritdoc}
*/
public function supportsDecoding($format/*, array $context = []*/)
public function supportsDecoding($format, array $context = []): bool
{
$context = \func_num_args() > 1 ? func_get_arg(1) : [];
try {
$this->getDecoder($format, $context);
} catch (RuntimeException $e) {
@@ -59,13 +57,9 @@ class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*
/**
* Gets the decoder supporting the format.
*
* @param string $format
*
* @return DecoderInterface
*
* @throws RuntimeException if no decoder is found
*/
private function getDecoder($format, array $context)
private function getDecoder(string $format, array $context): DecoderInterface
{
if (isset($this->decoderByFormat[$format])
&& isset($this->decoders[$this->decoderByFormat[$format]])
@@ -73,9 +67,13 @@ class ChainDecoder implements DecoderInterface /*, ContextAwareDecoderInterface*
return $this->decoders[$this->decoderByFormat[$format]];
}
$cache = true;
foreach ($this->decoders as $i => $decoder) {
$cache = $cache && !$decoder instanceof ContextAwareDecoderInterface;
if ($decoder->supportsDecoding($format, $context)) {
$this->decoderByFormat[$format] = $i;
if ($cache) {
$this->decoderByFormat[$format] = $i;
}
return $decoder;
}
+10 -17
View File
@@ -20,9 +20,9 @@ use Symfony\Component\Serializer\Exception\RuntimeException;
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*
* @final since version 3.3.
* @final
*/
class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*/
class ChainEncoder implements ContextAwareEncoderInterface
{
protected $encoders = [];
protected $encoderByFormat = [];
@@ -43,10 +43,8 @@ class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*
/**
* {@inheritdoc}
*/
public function supportsEncoding($format/*, array $context = []*/)
public function supportsEncoding($format, array $context = []): bool
{
$context = \func_num_args() > 1 ? func_get_arg(1) : [];
try {
$this->getEncoder($format, $context);
} catch (RuntimeException $e) {
@@ -58,14 +56,9 @@ class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*
/**
* Checks whether the normalization is needed for the given format.
*
* @param string $format
*
* @return bool
*/
public function needsNormalization($format/*, array $context = []*/)
public function needsNormalization(string $format, array $context = []): bool
{
$context = \func_num_args() > 1 ? func_get_arg(1) : [];
$encoder = $this->getEncoder($format, $context);
if (!$encoder instanceof NormalizationAwareInterface) {
@@ -82,13 +75,9 @@ class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*
/**
* Gets the encoder supporting the format.
*
* @param string $format
*
* @return EncoderInterface
*
* @throws RuntimeException if no encoder is found
*/
private function getEncoder($format, array $context)
private function getEncoder(string $format, array $context): EncoderInterface
{
if (isset($this->encoderByFormat[$format])
&& isset($this->encoders[$this->encoderByFormat[$format]])
@@ -96,9 +85,13 @@ class ChainEncoder implements EncoderInterface /*, ContextAwareEncoderInterface*
return $this->encoders[$this->encoderByFormat[$format]];
}
$cache = true;
foreach ($this->encoders as $i => $encoder) {
$cache = $cache && !$encoder instanceof ContextAwareEncoderInterface;
if ($encoder->supportsEncoding($format, $context)) {
$this->encoderByFormat[$format] = $i;
if ($cache) {
$this->encoderByFormat[$format] = $i;
}
return $encoder;
}
+102 -48
View File
@@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* Encodes CSV data.
@@ -21,34 +22,55 @@ use Symfony\Component\Serializer\Exception\InvalidArgumentException;
*/
class CsvEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'csv';
const DELIMITER_KEY = 'csv_delimiter';
const ENCLOSURE_KEY = 'csv_enclosure';
const ESCAPE_CHAR_KEY = 'csv_escape_char';
const KEY_SEPARATOR_KEY = 'csv_key_separator';
const HEADERS_KEY = 'csv_headers';
public const FORMAT = 'csv';
public const DELIMITER_KEY = 'csv_delimiter';
public const ENCLOSURE_KEY = 'csv_enclosure';
public const ESCAPE_CHAR_KEY = 'csv_escape_char';
public const KEY_SEPARATOR_KEY = 'csv_key_separator';
public const HEADERS_KEY = 'csv_headers';
public const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
public const AS_COLLECTION_KEY = 'as_collection';
public const NO_HEADERS_KEY = 'no_headers';
public const OUTPUT_UTF8_BOM_KEY = 'output_utf8_bom';
private $delimiter;
private $enclosure;
private $escapeChar;
private $keySeparator;
private const UTF8_BOM = "\xEF\xBB\xBF";
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];
private $defaultContext = [
self::DELIMITER_KEY => ',',
self::ENCLOSURE_KEY => '"',
self::ESCAPE_CHAR_KEY => '',
self::ESCAPE_FORMULAS_KEY => false,
self::HEADERS_KEY => [],
self::KEY_SEPARATOR_KEY => '.',
self::NO_HEADERS_KEY => false,
self::AS_COLLECTION_KEY => false,
self::OUTPUT_UTF8_BOM_KEY => false,
];
/**
* @param string $delimiter
* @param string $enclosure
* @param string $escapeChar
* @param string $keySeparator
* @param array $defaultContext
*/
public function __construct($delimiter = ',', $enclosure = '"', $escapeChar = '', $keySeparator = '.')
public function __construct($defaultContext = [], string $enclosure = '"', string $escapeChar = '', string $keySeparator = '.', bool $escapeFormulas = false)
{
if ('' === $escapeChar && \PHP_VERSION_ID < 70400) {
$escapeChar = '\\';
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::DELIMITER_KEY => (string) $defaultContext,
self::ENCLOSURE_KEY => $enclosure,
self::ESCAPE_CHAR_KEY => $escapeChar,
self::KEY_SEPARATOR_KEY => $keySeparator,
self::ESCAPE_FORMULAS_KEY => $escapeFormulas,
];
}
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
$this->escapeChar = $escapeChar;
$this->keySeparator = $keySeparator;
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
if (\PHP_VERSION_ID < 70400 && '' === $this->defaultContext[self::ESCAPE_CHAR_KEY]) {
$this->defaultContext[self::ESCAPE_CHAR_KEY] = '\\';
}
}
/**
@@ -58,7 +80,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
{
$handle = fopen('php://temp,', 'w+');
if (!\is_array($data)) {
if (!is_iterable($data)) {
$data = [[$data]];
} elseif (empty($data)) {
$data = [[]];
@@ -75,18 +97,20 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
}
}
list($delimiter, $enclosure, $escapeChar, $keySeparator, $headers) = $this->getCsvOptions($context);
[$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $outputBom] = $this->getCsvOptions($context);
foreach ($data as &$value) {
$flattened = [];
$this->flatten($value, $flattened, $keySeparator);
$this->flatten($value, $flattened, $keySeparator, '', $escapeFormulas);
$value = $flattened;
}
unset($value);
$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
}
$headers = array_fill_keys($headers, '');
foreach ($data as $row) {
@@ -97,6 +121,14 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$value = stream_get_contents($handle);
fclose($handle);
if ($outputBom) {
if (!preg_match('//u', $value)) {
throw new UnexpectedValueException('You are trying to add a UTF-8 BOM to a non UTF-8 text.');
}
$value = self::UTF8_BOM.$value;
}
return $value;
}
@@ -117,12 +149,16 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
fwrite($handle, $data);
rewind($handle);
if (str_starts_with($data, self::UTF8_BOM)) {
fseek($handle, \strlen(self::UTF8_BOM));
}
$headers = null;
$nbHeaders = 0;
$headerCount = [];
$result = [];
list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
[$delimiter, $enclosure, $escapeChar, $keySeparator] = $this->getCsvOptions($context);
while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
$nbCols = \count($cols);
@@ -130,13 +166,20 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
if (null === $headers) {
$nbHeaders = $nbCols;
foreach ($cols as $col) {
$header = explode($keySeparator, $col);
$headers[] = $header;
$headerCount[] = \count($header);
}
if ($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY]) {
for ($i = 0; $i < $nbCols; ++$i) {
$headers[] = [$i];
}
$headerCount = array_fill(0, $nbCols, 1);
} else {
foreach ($cols as $col) {
$header = explode($keySeparator, $col);
$headers[] = $header;
$headerCount[] = \count($header);
}
continue;
continue;
}
}
$item = [];
@@ -163,10 +206,18 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
}
fclose($handle);
if ($context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY]) {
return $result;
}
if (empty($result) || isset($result[1])) {
return $result;
}
if (!isset($context['as_collection'])) {
@trigger_error('Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0.', \E_USER_DEPRECATED);
}
// If there is only one data line in the document, return it (the line), the result is not considered as a collection
return $result[0];
}
@@ -181,41 +232,44 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
/**
* Flattens an array and generates keys including the path.
*
* @param string $keySeparator
* @param string $parentKey
*/
private function flatten(array $array, array &$result, $keySeparator, $parentKey = '')
private function flatten(iterable $array, array &$result, string $keySeparator, string $parentKey = '', bool $escapeFormulas = false)
{
foreach ($array as $key => $value) {
if (\is_array($value)) {
$this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator);
if (is_iterable($value)) {
$this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator, $escapeFormulas);
} else {
// Ensures an actual value is used when dealing with true and false
$result[$parentKey.$key] = false === $value ? 0 : (true === $value ? 1 : $value);
if ($escapeFormulas && \in_array(substr((string) $value, 0, 1), self::FORMULAS_START_CHARACTERS, true)) {
$result[$parentKey.$key] = "'".$value;
} else {
// Ensures an actual value is used when dealing with true and false
$result[$parentKey.$key] = false === $value ? 0 : (true === $value ? 1 : $value);
}
}
}
}
private function getCsvOptions(array $context)
private function getCsvOptions(array $context): array
{
$delimiter = isset($context[self::DELIMITER_KEY]) ? $context[self::DELIMITER_KEY] : $this->delimiter;
$enclosure = isset($context[self::ENCLOSURE_KEY]) ? $context[self::ENCLOSURE_KEY] : $this->enclosure;
$escapeChar = isset($context[self::ESCAPE_CHAR_KEY]) ? $context[self::ESCAPE_CHAR_KEY] : $this->escapeChar;
$keySeparator = isset($context[self::KEY_SEPARATOR_KEY]) ? $context[self::KEY_SEPARATOR_KEY] : $this->keySeparator;
$headers = isset($context[self::HEADERS_KEY]) ? $context[self::HEADERS_KEY] : [];
$delimiter = $context[self::DELIMITER_KEY] ?? $this->defaultContext[self::DELIMITER_KEY];
$enclosure = $context[self::ENCLOSURE_KEY] ?? $this->defaultContext[self::ENCLOSURE_KEY];
$escapeChar = $context[self::ESCAPE_CHAR_KEY] ?? $this->defaultContext[self::ESCAPE_CHAR_KEY];
$keySeparator = $context[self::KEY_SEPARATOR_KEY] ?? $this->defaultContext[self::KEY_SEPARATOR_KEY];
$headers = $context[self::HEADERS_KEY] ?? $this->defaultContext[self::HEADERS_KEY];
$escapeFormulas = $context[self::ESCAPE_FORMULAS_KEY] ?? $this->defaultContext[self::ESCAPE_FORMULAS_KEY];
$outputBom = $context[self::OUTPUT_UTF8_BOM_KEY] ?? $this->defaultContext[self::OUTPUT_UTF8_BOM_KEY];
if (!\is_array($headers)) {
throw new InvalidArgumentException(sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, \gettype($headers)));
}
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers];
return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $outputBom];
}
/**
* @return string[]
*/
private function extractHeaders(array $data)
private function extractHeaders(iterable $data): array
{
$headers = [];
$flippedHeaders = [];
+1 -1
View File
@@ -25,7 +25,7 @@ interface EncoderInterface
* @param string $format Format name
* @param array $context Options that normalizers/encoders have access to
*
* @return string|int|float|bool
* @return string
*
* @throws UnexpectedValueException
*/
+34 -30
View File
@@ -22,19 +22,41 @@ class JsonDecode implements DecoderInterface
{
protected $serializer;
private $associative;
private $recursionDepth;
/**
* True to return the result as an associative array, false for a nested stdClass hierarchy.
*/
public const ASSOCIATIVE = 'json_decode_associative';
public const OPTIONS = 'json_decode_options';
/**
* Specifies the recursion depth.
*/
public const RECURSION_DEPTH = 'json_decode_recursion_depth';
private $defaultContext = [
self::ASSOCIATIVE => false,
self::OPTIONS => 0,
self::RECURSION_DEPTH => 512,
];
/**
* Constructs a new JsonDecode instance.
*
* @param bool $associative True to return the result associative array, false for a nested stdClass hierarchy
* @param int $depth Specifies the recursion depth
* @param array $defaultContext
*/
public function __construct($associative = false, $depth = 512)
public function __construct($defaultContext = [], int $depth = 512)
{
$this->associative = $associative;
$this->recursionDepth = (int) $depth;
if (!\is_array($defaultContext)) {
@trigger_error(sprintf('Using constructor parameters that are not a default context is deprecated since Symfony 4.2, use the "%s" and "%s" keys of the context instead.', self::ASSOCIATIVE, self::RECURSION_DEPTH), \E_USER_DEPRECATED);
$defaultContext = [
self::ASSOCIATIVE => (bool) $defaultContext,
self::RECURSION_DEPTH => $depth,
];
}
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
/**
@@ -47,7 +69,7 @@ class JsonDecode implements DecoderInterface
* The $context array is a simple key=>value array, with the following supported keys:
*
* json_decode_associative: boolean
* If true, returns the object as associative array.
* If true, returns the object as an associative array.
* If false, returns the object as nested stdClass
* If not specified, this method will use the default set in JsonDecode::__construct
*
@@ -56,7 +78,7 @@ class JsonDecode implements DecoderInterface
* If not specified, this method will use the default set in JsonDecode::__construct
*
* json_decode_options: integer
* Specifies additional options as per documentation for json_decode. Only supported with PHP 5.4.0 and higher
* Specifies additional options as per documentation for json_decode
*
* @return mixed
*
@@ -66,11 +88,9 @@ class JsonDecode implements DecoderInterface
*/
public function decode($data, $format, array $context = [])
{
$context = $this->resolveContext($context);
$associative = $context['json_decode_associative'];
$recursionDepth = $context['json_decode_recursion_depth'];
$options = $context['json_decode_options'];
$associative = $context[self::ASSOCIATIVE] ?? $this->defaultContext[self::ASSOCIATIVE];
$recursionDepth = $context[self::RECURSION_DEPTH] ?? $this->defaultContext[self::RECURSION_DEPTH];
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
try {
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
@@ -96,20 +116,4 @@ class JsonDecode implements DecoderInterface
{
return JsonEncoder::FORMAT === $format;
}
/**
* Merges the default options of the Json Decoder with the passed context.
*
* @return array
*/
private function resolveContext(array $context)
{
$defaultOptions = [
'json_decode_associative' => $this->associative,
'json_decode_recursion_depth' => $this->recursionDepth,
'json_decode_options' => 0,
];
return array_merge($defaultOptions, $context);
}
}
+17 -15
View File
@@ -20,11 +20,24 @@ use Symfony\Component\Serializer\Exception\NotEncodableValueException;
*/
class JsonEncode implements EncoderInterface
{
private $options;
public const OPTIONS = 'json_encode_options';
public function __construct($bitmask = 0)
private $defaultContext = [
self::OPTIONS => 0,
];
/**
* @param array $defaultContext
*/
public function __construct($defaultContext = [])
{
$this->options = $bitmask;
if (!\is_array($defaultContext)) {
@trigger_error(sprintf('Passing an integer as first parameter of the "%s()" method is deprecated since Symfony 4.2, use the "json_encode_options" key of the context instead.', __METHOD__), \E_USER_DEPRECATED);
$this->defaultContext[self::OPTIONS] = (int) $defaultContext;
} else {
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
}
/**
@@ -34,8 +47,7 @@ class JsonEncode implements EncoderInterface
*/
public function encode($data, $format, array $context = [])
{
$context = $this->resolveContext($context);
$options = $context['json_encode_options'];
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
try {
$encodedJson = json_encode($data, $options);
@@ -61,14 +73,4 @@ class JsonEncode implements EncoderInterface
{
return JsonEncoder::FORMAT === $format;
}
/**
* Merge default json encode options with context.
*
* @return array
*/
private function resolveContext(array $context = [])
{
return array_merge(['json_encode_options' => $this->options], $context);
}
}
+3 -3
View File
@@ -18,15 +18,15 @@ namespace Symfony\Component\Serializer\Encoder;
*/
class JsonEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'json';
public const FORMAT = 'json';
protected $encodingImpl;
protected $decodingImpl;
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
{
$this->encodingImpl = $encodingImpl ?: new JsonEncode();
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
$this->encodingImpl = $encodingImpl ?? new JsonEncode();
$this->decodingImpl = $decodingImpl ?? new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
}
/**
@@ -1,27 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\SerializerAwareInterface;
use Symfony\Component\Serializer\SerializerAwareTrait;
/**
* SerializerAware Encoder implementation.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @deprecated since version 3.2, to be removed in 4.0. Use the SerializerAwareTrait instead.
*/
abstract class SerializerAwareEncoder implements SerializerAwareInterface
{
use SerializerAwareTrait;
}
+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;
}
}
+13 -5
View File
@@ -14,6 +14,7 @@ namespace Symfony\Component\Serializer\Encoder;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Yaml;
/**
* Encodes YAML data.
@@ -22,7 +23,10 @@ use Symfony\Component\Yaml\Parser;
*/
class YamlEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'yaml';
public const FORMAT = 'yaml';
private const ALTERNATIVE_FORMAT = 'yml';
public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects';
private $dumper;
private $parser;
@@ -34,8 +38,8 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Install "symfony/yaml" to use it.');
}
$this->dumper = $dumper ?: new Dumper();
$this->parser = $parser ?: new Parser();
$this->dumper = $dumper ?? new Dumper();
$this->parser = $parser ?? new Parser();
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
@@ -46,6 +50,10 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
{
$context = array_merge($this->defaultContext, $context);
if (isset($context[self::PRESERVE_EMPTY_OBJECTS])) {
$context['yaml_flags'] |= Yaml::DUMP_OBJECT_AS_MAP;
}
return $this->dumper->dump($data, $context['yaml_inline'], $context['yaml_indent'], $context['yaml_flags']);
}
@@ -54,7 +62,7 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
*/
public function supportsEncoding($format)
{
return self::FORMAT === $format;
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
/**
@@ -72,6 +80,6 @@ class YamlEncoder implements EncoderInterface, DecoderInterface
*/
public function supportsDecoding($format)
{
return self::FORMAT === $format;
return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
}
}