Initial Drupal 11 with DDEV setup
This commit is contained in:
		
							
								
								
									
										83
									
								
								vendor/symfony/serializer/Encoder/ChainDecoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								vendor/symfony/serializer/Encoder/ChainDecoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,83 @@
 | 
			
		||||
<?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\Exception\RuntimeException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decoder delegating the decoding to a chain of decoders.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jordi Boggiano <j.boggiano@seld.be>
 | 
			
		||||
 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 | 
			
		||||
 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
 | 
			
		||||
 *
 | 
			
		||||
 * @final
 | 
			
		||||
 */
 | 
			
		||||
class ChainDecoder implements ContextAwareDecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var array<string, array-key>
 | 
			
		||||
     */
 | 
			
		||||
    private array $decoderByFormat = [];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param array<DecoderInterface> $decoders
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        private readonly array $decoders = [],
 | 
			
		||||
    ) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final public function decode(string $data, string $format, array $context = []): mixed
 | 
			
		||||
    {
 | 
			
		||||
        return $this->getDecoder($format, $context)->decode($data, $format, $context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsDecoding(string $format, array $context = []): bool
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $this->getDecoder($format, $context);
 | 
			
		||||
        } catch (RuntimeException) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the decoder supporting the format.
 | 
			
		||||
     *
 | 
			
		||||
     * @throws RuntimeException if no decoder is found
 | 
			
		||||
     */
 | 
			
		||||
    private function getDecoder(string $format, array $context): DecoderInterface
 | 
			
		||||
    {
 | 
			
		||||
        if (isset($this->decoderByFormat[$format])
 | 
			
		||||
            && isset($this->decoders[$this->decoderByFormat[$format]])
 | 
			
		||||
        ) {
 | 
			
		||||
            return $this->decoders[$this->decoderByFormat[$format]];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $cache = true;
 | 
			
		||||
        foreach ($this->decoders as $i => $decoder) {
 | 
			
		||||
            $cache = $cache && !$decoder instanceof ContextAwareDecoderInterface;
 | 
			
		||||
            if ($decoder->supportsDecoding($format, $context)) {
 | 
			
		||||
                if ($cache) {
 | 
			
		||||
                    $this->decoderByFormat[$format] = $i;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return $decoder;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new RuntimeException(\sprintf('No decoder found for format "%s".', $format));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										106
									
								
								vendor/symfony/serializer/Encoder/ChainEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								vendor/symfony/serializer/Encoder/ChainEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,106 @@
 | 
			
		||||
<?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\Debug\TraceableEncoder;
 | 
			
		||||
use Symfony\Component\Serializer\Exception\RuntimeException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encoder delegating the decoding to a chain of encoders.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jordi Boggiano <j.boggiano@seld.be>
 | 
			
		||||
 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
 | 
			
		||||
 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
 | 
			
		||||
 *
 | 
			
		||||
 * @final
 | 
			
		||||
 */
 | 
			
		||||
class ChainEncoder implements ContextAwareEncoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var array<string, array-key>
 | 
			
		||||
     */
 | 
			
		||||
    private array $encoderByFormat = [];
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param array<EncoderInterface> $encoders
 | 
			
		||||
     */
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        private readonly array $encoders = [],
 | 
			
		||||
    ) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final public function encode(mixed $data, string $format, array $context = []): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->getEncoder($format, $context)->encode($data, $format, $context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsEncoding(string $format, array $context = []): bool
 | 
			
		||||
    {
 | 
			
		||||
        try {
 | 
			
		||||
            $this->getEncoder($format, $context);
 | 
			
		||||
        } catch (RuntimeException) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether the normalization is needed for the given format.
 | 
			
		||||
     */
 | 
			
		||||
    public function needsNormalization(string $format, array $context = []): bool
 | 
			
		||||
    {
 | 
			
		||||
        $encoder = $this->getEncoder($format, $context);
 | 
			
		||||
 | 
			
		||||
        if ($encoder instanceof TraceableEncoder) {
 | 
			
		||||
            return $encoder->needsNormalization();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!$encoder instanceof NormalizationAwareInterface) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($encoder instanceof self) {
 | 
			
		||||
            return $encoder->needsNormalization($format, $context);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the encoder supporting the format.
 | 
			
		||||
     *
 | 
			
		||||
     * @throws RuntimeException if no encoder is found
 | 
			
		||||
     */
 | 
			
		||||
    private function getEncoder(string $format, array $context): EncoderInterface
 | 
			
		||||
    {
 | 
			
		||||
        if (isset($this->encoderByFormat[$format])
 | 
			
		||||
            && isset($this->encoders[$this->encoderByFormat[$format]])
 | 
			
		||||
        ) {
 | 
			
		||||
            return $this->encoders[$this->encoderByFormat[$format]];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $cache = true;
 | 
			
		||||
        foreach ($this->encoders as $i => $encoder) {
 | 
			
		||||
            $cache = $cache && !$encoder instanceof ContextAwareEncoderInterface;
 | 
			
		||||
            if ($encoder->supportsEncoding($format, $context)) {
 | 
			
		||||
                if ($cache) {
 | 
			
		||||
                    $this->encoderByFormat[$format] = $i;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                return $encoder;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new RuntimeException(\sprintf('No encoder found for format "%s".', $format));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								vendor/symfony/serializer/Encoder/ContextAwareDecoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								vendor/symfony/serializer/Encoder/ContextAwareDecoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
<?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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Adds the support of an extra $context parameter for the supportsDecoding method.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Kévin Dunglas <dunglas@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
interface ContextAwareDecoderInterface extends DecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @param array $context options that decoders have access to
 | 
			
		||||
     */
 | 
			
		||||
    public function supportsDecoding(string $format, array $context = []): bool;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										25
									
								
								vendor/symfony/serializer/Encoder/ContextAwareEncoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								vendor/symfony/serializer/Encoder/ContextAwareEncoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
<?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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Adds the support of an extra $context parameter for the supportsEncoding method.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Kévin Dunglas <dunglas@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
interface ContextAwareEncoderInterface extends EncoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @param array $context options that encoders have access to
 | 
			
		||||
     */
 | 
			
		||||
    public function supportsEncoding(string $format, array $context = []): bool;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										296
									
								
								vendor/symfony/serializer/Encoder/CsvEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										296
									
								
								vendor/symfony/serializer/Encoder/CsvEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,296 @@
 | 
			
		||||
<?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\Exception\InvalidArgumentException;
 | 
			
		||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encodes CSV data.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Kévin Dunglas <dunglas@gmail.com>
 | 
			
		||||
 * @author Oliver Hoff <oliver@hofff.com>
 | 
			
		||||
 */
 | 
			
		||||
class CsvEncoder implements EncoderInterface, DecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    public const FORMAT = 'csv';
 | 
			
		||||
    public const DELIMITER_KEY = 'csv_delimiter';
 | 
			
		||||
    public const ENCLOSURE_KEY = 'csv_enclosure';
 | 
			
		||||
    /**
 | 
			
		||||
     * @deprecated since Symfony 7.2, to be removed in 8.0
 | 
			
		||||
     */
 | 
			
		||||
    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 END_OF_LINE = 'csv_end_of_line';
 | 
			
		||||
    public const OUTPUT_UTF8_BOM_KEY = 'output_utf8_bom';
 | 
			
		||||
 | 
			
		||||
    private const UTF8_BOM = "\xEF\xBB\xBF";
 | 
			
		||||
 | 
			
		||||
    private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];
 | 
			
		||||
 | 
			
		||||
    private array $defaultContext = [
 | 
			
		||||
        self::DELIMITER_KEY => ',',
 | 
			
		||||
        self::ENCLOSURE_KEY => '"',
 | 
			
		||||
        self::ESCAPE_CHAR_KEY => '',
 | 
			
		||||
        self::END_OF_LINE => "\n",
 | 
			
		||||
        self::ESCAPE_FORMULAS_KEY => false,
 | 
			
		||||
        self::HEADERS_KEY => [],
 | 
			
		||||
        self::KEY_SEPARATOR_KEY => '.',
 | 
			
		||||
        self::NO_HEADERS_KEY => false,
 | 
			
		||||
        self::AS_COLLECTION_KEY => true,
 | 
			
		||||
        self::OUTPUT_UTF8_BOM_KEY => false,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    public function __construct(array $defaultContext = [])
 | 
			
		||||
    {
 | 
			
		||||
        if (\array_key_exists(self::ESCAPE_CHAR_KEY, $defaultContext)) {
 | 
			
		||||
            trigger_deprecation('symfony/serializer', '7.2', 'Setting the "csv_escape_char" option is deprecated. The option will be removed in 8.0.');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function encode(mixed $data, string $format, array $context = []): string
 | 
			
		||||
    {
 | 
			
		||||
        $handle = fopen('php://temp,', 'w+');
 | 
			
		||||
 | 
			
		||||
        if (!is_iterable($data)) {
 | 
			
		||||
            $data = [[$data]];
 | 
			
		||||
        } elseif (!$data) {
 | 
			
		||||
            $data = [[]];
 | 
			
		||||
        } else {
 | 
			
		||||
            if ($data instanceof \Traversable) {
 | 
			
		||||
                // Generators can only be iterated once — convert to array to allow multiple traversals
 | 
			
		||||
                $data = iterator_to_array($data);
 | 
			
		||||
            }
 | 
			
		||||
            // Sequential arrays of arrays are considered as collections
 | 
			
		||||
            $i = 0;
 | 
			
		||||
            foreach ($data as $key => $value) {
 | 
			
		||||
                if ($i !== $key || !\is_array($value)) {
 | 
			
		||||
                    $data = [$data];
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                ++$i;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $outputBom] = $this->getCsvOptions($context);
 | 
			
		||||
 | 
			
		||||
        foreach ($data as &$value) {
 | 
			
		||||
            $flattened = [];
 | 
			
		||||
            $this->flatten($value, $flattened, $keySeparator, '', $escapeFormulas);
 | 
			
		||||
            $value = $flattened;
 | 
			
		||||
        }
 | 
			
		||||
        unset($value);
 | 
			
		||||
 | 
			
		||||
        $headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
 | 
			
		||||
        $endOfLine = $context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE];
 | 
			
		||||
 | 
			
		||||
        if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
 | 
			
		||||
            fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
 | 
			
		||||
            if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
 | 
			
		||||
                fwrite($handle, $endOfLine);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $headers = array_fill_keys($headers, '');
 | 
			
		||||
        foreach ($data as $row) {
 | 
			
		||||
            fputcsv($handle, array_replace($headers, $row), $delimiter, $enclosure, $escapeChar);
 | 
			
		||||
            if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
 | 
			
		||||
                fwrite($handle, $endOfLine);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        rewind($handle);
 | 
			
		||||
        $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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsEncoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function decode(string $data, string $format, array $context = []): mixed
 | 
			
		||||
    {
 | 
			
		||||
        $handle = fopen('php://temp', 'r+');
 | 
			
		||||
        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 = [];
 | 
			
		||||
 | 
			
		||||
        [$delimiter, $enclosure, $escapeChar, $keySeparator, , , , $asCollection] = $this->getCsvOptions($context);
 | 
			
		||||
 | 
			
		||||
        while (false !== ($cols = fgetcsv($handle, 0, $delimiter, $enclosure, $escapeChar))) {
 | 
			
		||||
            $nbCols = \count($cols);
 | 
			
		||||
 | 
			
		||||
            if (null === $headers) {
 | 
			
		||||
                $nbHeaders = $nbCols;
 | 
			
		||||
 | 
			
		||||
                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;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $item = [];
 | 
			
		||||
            for ($i = 0; ($i < $nbCols) && ($i < $nbHeaders); ++$i) {
 | 
			
		||||
                $depth = $headerCount[$i];
 | 
			
		||||
                $arr = &$item;
 | 
			
		||||
                for ($j = 0; $j < $depth; ++$j) {
 | 
			
		||||
                    $headerName = $headers[$i][$j];
 | 
			
		||||
 | 
			
		||||
                    if ('' === $headerName) {
 | 
			
		||||
                        $headerName = $i;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Handle nested arrays
 | 
			
		||||
                    if ($j === ($depth - 1)) {
 | 
			
		||||
                        $arr[$headerName] = $cols[$i];
 | 
			
		||||
 | 
			
		||||
                        continue;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if (!isset($arr[$headerName])) {
 | 
			
		||||
                        $arr[$headerName] = [];
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    $arr = &$arr[$headerName];
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $result[] = $item;
 | 
			
		||||
        }
 | 
			
		||||
        fclose($handle);
 | 
			
		||||
 | 
			
		||||
        if ($asCollection) {
 | 
			
		||||
            return $result;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!$result || isset($result[1])) {
 | 
			
		||||
            return $result;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 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];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsDecoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Flattens an array and generates keys including the path.
 | 
			
		||||
     */
 | 
			
		||||
    private function flatten(iterable $array, array &$result, string $keySeparator, string $parentKey = '', bool $escapeFormulas = false): void
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($array as $key => $value) {
 | 
			
		||||
            if (is_iterable($value)) {
 | 
			
		||||
                $this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator, $escapeFormulas);
 | 
			
		||||
            } else {
 | 
			
		||||
                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): array
 | 
			
		||||
    {
 | 
			
		||||
        $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];
 | 
			
		||||
        $asCollection = $context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY];
 | 
			
		||||
 | 
			
		||||
        if (!\is_array($headers)) {
 | 
			
		||||
            throw new InvalidArgumentException(\sprintf('The "%s" context variable must be an array or null, given "%s".', self::HEADERS_KEY, get_debug_type($headers)));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return [$delimiter, $enclosure, $escapeChar, $keySeparator, $headers, $escapeFormulas, $outputBom, $asCollection];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return string[]
 | 
			
		||||
     */
 | 
			
		||||
    private function extractHeaders(iterable $data): array
 | 
			
		||||
    {
 | 
			
		||||
        $headers = [];
 | 
			
		||||
        $flippedHeaders = [];
 | 
			
		||||
 | 
			
		||||
        foreach ($data as $row) {
 | 
			
		||||
            $previousHeader = null;
 | 
			
		||||
 | 
			
		||||
            foreach ($row as $header => $_) {
 | 
			
		||||
                if (isset($flippedHeaders[$header])) {
 | 
			
		||||
                    $previousHeader = $header;
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if (null === $previousHeader) {
 | 
			
		||||
                    $n = \count($headers);
 | 
			
		||||
                } else {
 | 
			
		||||
                    $n = $flippedHeaders[$previousHeader] + 1;
 | 
			
		||||
 | 
			
		||||
                    for ($j = \count($headers); $j > $n; --$j) {
 | 
			
		||||
                        ++$flippedHeaders[$headers[$j] = $headers[$j - 1]];
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                $headers[$n] = $header;
 | 
			
		||||
                $flippedHeaders[$header] = $n;
 | 
			
		||||
                $previousHeader = $header;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $headers;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								vendor/symfony/serializer/Encoder/DecoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								vendor/symfony/serializer/Encoder/DecoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
<?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\Exception\UnexpectedValueException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Jordi Boggiano <j.boggiano@seld.be>
 | 
			
		||||
 */
 | 
			
		||||
interface DecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Decodes a string into PHP data.
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $data    Data to decode
 | 
			
		||||
     * @param string $format  Format name
 | 
			
		||||
     * @param array  $context Options that decoders have access to
 | 
			
		||||
     *
 | 
			
		||||
     * The format parameter specifies which format the data is in; valid values
 | 
			
		||||
     * depend on the specific implementation. Authors implementing this interface
 | 
			
		||||
     * are encouraged to document which formats they support in a non-inherited
 | 
			
		||||
     * phpdoc comment.
 | 
			
		||||
     *
 | 
			
		||||
     * @throws UnexpectedValueException
 | 
			
		||||
     */
 | 
			
		||||
    public function decode(string $data, string $format, array $context = []): mixed;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether the deserializer can decode from given format.
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $format Format name
 | 
			
		||||
     */
 | 
			
		||||
    public function supportsDecoding(string $format): bool;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								vendor/symfony/serializer/Encoder/EncoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								vendor/symfony/serializer/Encoder/EncoderInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
<?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\Exception\UnexpectedValueException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @author Jordi Boggiano <j.boggiano@seld.be>
 | 
			
		||||
 */
 | 
			
		||||
interface EncoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Encodes data into the given format.
 | 
			
		||||
     *
 | 
			
		||||
     * @param mixed  $data    Data to encode
 | 
			
		||||
     * @param string $format  Format name
 | 
			
		||||
     * @param array  $context Options that normalizers/encoders have access to
 | 
			
		||||
     *
 | 
			
		||||
     * @throws UnexpectedValueException
 | 
			
		||||
     */
 | 
			
		||||
    public function encode(mixed $data, string $format, array $context = []): string;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks whether the serializer can encode to given format.
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $format Format name
 | 
			
		||||
     */
 | 
			
		||||
    public function supportsEncoding(string $format): bool;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										119
									
								
								vendor/symfony/serializer/Encoder/JsonDecode.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								vendor/symfony/serializer/Encoder/JsonDecode.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,119 @@
 | 
			
		||||
<?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 Seld\JsonLint\JsonParser;
 | 
			
		||||
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
 | 
			
		||||
use Symfony\Component\Serializer\Exception\UnsupportedException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Decodes JSON data.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Sander Coolen <sander@jibber.nl>
 | 
			
		||||
 */
 | 
			
		||||
class JsonDecode implements DecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * True to return the result as an associative array, false for a nested stdClass hierarchy.
 | 
			
		||||
     */
 | 
			
		||||
    public const ASSOCIATIVE = 'json_decode_associative';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * True to enable seld/jsonlint as a source for more specific error messages when json_decode fails.
 | 
			
		||||
     */
 | 
			
		||||
    public const DETAILED_ERROR_MESSAGES = 'json_decode_detailed_errors';
 | 
			
		||||
 | 
			
		||||
    public const OPTIONS = 'json_decode_options';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Specifies the recursion depth.
 | 
			
		||||
     */
 | 
			
		||||
    public const RECURSION_DEPTH = 'json_decode_recursion_depth';
 | 
			
		||||
 | 
			
		||||
    private array $defaultContext = [
 | 
			
		||||
        self::ASSOCIATIVE => false,
 | 
			
		||||
        self::DETAILED_ERROR_MESSAGES => false,
 | 
			
		||||
        self::OPTIONS => 0,
 | 
			
		||||
        self::RECURSION_DEPTH => 512,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    public function __construct(array $defaultContext = [])
 | 
			
		||||
    {
 | 
			
		||||
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Decodes data.
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $data    The encoded JSON string to decode
 | 
			
		||||
     * @param string $format  Must be set to JsonEncoder::FORMAT
 | 
			
		||||
     * @param array  $context An optional set of options for the JSON decoder; see below
 | 
			
		||||
     *
 | 
			
		||||
     * The $context array is a simple key=>value array, with the following supported keys:
 | 
			
		||||
     *
 | 
			
		||||
     * json_decode_associative: boolean
 | 
			
		||||
     *      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
 | 
			
		||||
     *
 | 
			
		||||
     * json_decode_recursion_depth: integer
 | 
			
		||||
     *      Specifies the maximum recursion depth
 | 
			
		||||
     *      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
 | 
			
		||||
     *
 | 
			
		||||
     * json_decode_detailed_errors: bool
 | 
			
		||||
     *      If true, enables seld/jsonlint as a source for more specific error messages when json_decode fails.
 | 
			
		||||
     *      If false or not specified, this method will use default error messages from PHP's json_decode
 | 
			
		||||
     *
 | 
			
		||||
     * @throws NotEncodableValueException
 | 
			
		||||
     *
 | 
			
		||||
     * @see https://php.net/json_decode
 | 
			
		||||
     */
 | 
			
		||||
    public function decode(string $data, string $format, array $context = []): mixed
 | 
			
		||||
    {
 | 
			
		||||
        $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);
 | 
			
		||||
        } catch (\JsonException $e) {
 | 
			
		||||
            throw new NotEncodableValueException($e->getMessage(), 0, $e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (\JSON_THROW_ON_ERROR & $options) {
 | 
			
		||||
            return $decodedData;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (\JSON_ERROR_NONE === json_last_error()) {
 | 
			
		||||
            return $decodedData;
 | 
			
		||||
        }
 | 
			
		||||
        $errorMessage = json_last_error_msg();
 | 
			
		||||
 | 
			
		||||
        if (!($context[self::DETAILED_ERROR_MESSAGES] ?? $this->defaultContext[self::DETAILED_ERROR_MESSAGES])) {
 | 
			
		||||
            throw new NotEncodableValueException($errorMessage);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!class_exists(JsonParser::class)) {
 | 
			
		||||
            throw new UnsupportedException(\sprintf('Enabling "%s" serializer option requires seld/jsonlint. Try running "composer require seld/jsonlint".', self::DETAILED_ERROR_MESSAGES));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new NotEncodableValueException((new JsonParser())->lint($data)?->getMessage() ?: $errorMessage);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsDecoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return JsonEncoder::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										62
									
								
								vendor/symfony/serializer/Encoder/JsonEncode.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								vendor/symfony/serializer/Encoder/JsonEncode.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,62 @@
 | 
			
		||||
<?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\Exception\NotEncodableValueException;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encodes JSON data.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Sander Coolen <sander@jibber.nl>
 | 
			
		||||
 */
 | 
			
		||||
class JsonEncode implements EncoderInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Configure the JSON flags bitmask.
 | 
			
		||||
     */
 | 
			
		||||
    public const OPTIONS = 'json_encode_options';
 | 
			
		||||
 | 
			
		||||
    private array $defaultContext = [
 | 
			
		||||
        self::OPTIONS => \JSON_PRESERVE_ZERO_FRACTION,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    public function __construct(array $defaultContext = [])
 | 
			
		||||
    {
 | 
			
		||||
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function encode(mixed $data, string $format, array $context = []): string
 | 
			
		||||
    {
 | 
			
		||||
        $options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            $encodedJson = json_encode($data, $options);
 | 
			
		||||
        } catch (\JsonException $e) {
 | 
			
		||||
            throw new NotEncodableValueException($e->getMessage(), 0, $e);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (\JSON_THROW_ON_ERROR & $options) {
 | 
			
		||||
            return $encodedJson;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (\JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($options & \JSON_PARTIAL_OUTPUT_ON_ERROR))) {
 | 
			
		||||
            throw new NotEncodableValueException(json_last_error_msg());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $encodedJson;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsEncoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return JsonEncoder::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										60
									
								
								vendor/symfony/serializer/Encoder/JsonEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								vendor/symfony/serializer/Encoder/JsonEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
<?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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encodes JSON data.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jordi Boggiano <j.boggiano@seld.be>
 | 
			
		||||
 */
 | 
			
		||||
class JsonEncoder implements EncoderInterface, DecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    public const FORMAT = 'json';
 | 
			
		||||
 | 
			
		||||
    protected JsonEncode $encodingImpl;
 | 
			
		||||
    protected JsonDecode $decodingImpl;
 | 
			
		||||
 | 
			
		||||
    private array $defaultContext = [
 | 
			
		||||
        JsonDecode::ASSOCIATIVE => true,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    public function __construct(?JsonEncode $encodingImpl = null, ?JsonDecode $decodingImpl = null, array $defaultContext = [])
 | 
			
		||||
    {
 | 
			
		||||
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
 | 
			
		||||
        $this->encodingImpl = $encodingImpl ?? new JsonEncode($this->defaultContext);
 | 
			
		||||
        $this->decodingImpl = $decodingImpl ?? new JsonDecode($this->defaultContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function encode(mixed $data, string $format, array $context = []): string
 | 
			
		||||
    {
 | 
			
		||||
        $context = array_merge($this->defaultContext, $context);
 | 
			
		||||
 | 
			
		||||
        return $this->encodingImpl->encode($data, self::FORMAT, $context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function decode(string $data, string $format, array $context = []): mixed
 | 
			
		||||
    {
 | 
			
		||||
        $context = array_merge($this->defaultContext, $context);
 | 
			
		||||
 | 
			
		||||
        return $this->decodingImpl->decode($data, self::FORMAT, $context);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsEncoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsDecoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								vendor/symfony/serializer/Encoder/NormalizationAwareInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								vendor/symfony/serializer/Encoder/NormalizationAwareInterface.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
<?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;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Defines the interface of encoders that will normalize data themselves.
 | 
			
		||||
 *
 | 
			
		||||
 * Implementing this interface essentially just tells the Serializer that the
 | 
			
		||||
 * data should not be pre-normalized before being passed to this Encoder.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Jordi Boggiano <j.boggiano@seld.be>
 | 
			
		||||
 */
 | 
			
		||||
interface NormalizationAwareInterface
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										530
									
								
								vendor/symfony/serializer/Encoder/XmlEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										530
									
								
								vendor/symfony/serializer/Encoder/XmlEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,530 @@
 | 
			
		||||
<?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\Exception\BadMethodCallException;
 | 
			
		||||
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
 | 
			
		||||
use Symfony\Component\Serializer\SerializerAwareInterface;
 | 
			
		||||
use Symfony\Component\Serializer\SerializerAwareTrait;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @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 implements EncoderInterface, DecoderInterface, NormalizationAwareInterface, SerializerAwareInterface
 | 
			
		||||
{
 | 
			
		||||
    use SerializerAwareTrait;
 | 
			
		||||
 | 
			
		||||
    public const FORMAT = 'xml';
 | 
			
		||||
 | 
			
		||||
    public const AS_COLLECTION = 'as_collection';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * An array of ignored XML node types while decoding, each one of the DOM Predefined XML_* constants.
 | 
			
		||||
     */
 | 
			
		||||
    public const DECODER_IGNORED_NODE_TYPES = 'decoder_ignored_node_types';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * An array of ignored XML node types while encoding, each one of the DOM Predefined XML_* constants.
 | 
			
		||||
     */
 | 
			
		||||
    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 for loading XML documents.
 | 
			
		||||
     */
 | 
			
		||||
    public const LOAD_OPTIONS = 'load_options';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A bit field of LIBXML_* constants for saving XML documents.
 | 
			
		||||
     */
 | 
			
		||||
    public const SAVE_OPTIONS = 'save_options';
 | 
			
		||||
 | 
			
		||||
    public const REMOVE_EMPTY_TAGS = 'remove_empty_tags';
 | 
			
		||||
    public const ROOT_NODE_NAME = 'xml_root_node_name';
 | 
			
		||||
    public const STANDALONE = 'xml_standalone';
 | 
			
		||||
    public const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
 | 
			
		||||
    public const VERSION = 'xml_version';
 | 
			
		||||
    public const CDATA_WRAPPING = 'cdata_wrapping';
 | 
			
		||||
    public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern';
 | 
			
		||||
    public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes';
 | 
			
		||||
 | 
			
		||||
    private array $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::SAVE_OPTIONS => 0,
 | 
			
		||||
        self::REMOVE_EMPTY_TAGS => false,
 | 
			
		||||
        self::ROOT_NODE_NAME => 'response',
 | 
			
		||||
        self::TYPE_CAST_ATTRIBUTES => true,
 | 
			
		||||
        self::CDATA_WRAPPING => true,
 | 
			
		||||
        self::CDATA_WRAPPING_PATTERN => '/[<>&]/',
 | 
			
		||||
        self::IGNORE_EMPTY_ATTRIBUTES => false,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    public function __construct(array $defaultContext = [])
 | 
			
		||||
    {
 | 
			
		||||
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function encode(mixed $data, string $format, array $context = []): string
 | 
			
		||||
    {
 | 
			
		||||
        $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 $this->saveXml($data, $ignorePiNode ? $data->documentElement : null);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
 | 
			
		||||
 | 
			
		||||
        $dom = $this->createDomDocument($context);
 | 
			
		||||
 | 
			
		||||
        if (null !== $data && !\is_scalar($data)) {
 | 
			
		||||
            $root = $dom->createElement($xmlRootNodeName);
 | 
			
		||||
            $dom->appendChild($root);
 | 
			
		||||
            $this->buildXml($root, $data, $format, $context, $xmlRootNodeName);
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->appendNode($dom, $data, $format, $context, $xmlRootNodeName);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->saveXml($dom, $ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function decode(string $data, string $format, array $context = []): mixed
 | 
			
		||||
    {
 | 
			
		||||
        if ('' === trim($data)) {
 | 
			
		||||
            throw new NotEncodableValueException('Invalid XML data, it cannot be empty.');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $internalErrors = libxml_use_internal_errors(true);
 | 
			
		||||
        libxml_clear_errors();
 | 
			
		||||
 | 
			
		||||
        $dom = new \DOMDocument();
 | 
			
		||||
        $dom->loadXML($data, $context[self::LOAD_OPTIONS] ?? $this->defaultContext[self::LOAD_OPTIONS]);
 | 
			
		||||
 | 
			
		||||
        libxml_use_internal_errors($internalErrors);
 | 
			
		||||
 | 
			
		||||
        if ($error = libxml_get_last_error()) {
 | 
			
		||||
            libxml_clear_errors();
 | 
			
		||||
 | 
			
		||||
            throw new NotEncodableValueException($error->message);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $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) {
 | 
			
		||||
                $rootNode = $child;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // todo: throw an exception if the root node name is not correctly configured (bc)
 | 
			
		||||
 | 
			
		||||
        if ($rootNode->hasChildNodes()) {
 | 
			
		||||
            $data = $this->parseXml($rootNode, $context);
 | 
			
		||||
            if (\is_array($data)) {
 | 
			
		||||
                $data = $this->addXmlNamespaces($data, $rootNode, $dom);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return $data;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!$rootNode->hasAttributes()) {
 | 
			
		||||
            return $rootNode->nodeValue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $data = array_merge($this->parseXmlAttributes($rootNode, $context), ['#' => $rootNode->nodeValue]);
 | 
			
		||||
 | 
			
		||||
        return $this->addXmlNamespaces($data, $rootNode, $dom);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsEncoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsDecoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final protected function appendXMLString(\DOMNode $node, string $val): bool
 | 
			
		||||
    {
 | 
			
		||||
        if ('' !== $val) {
 | 
			
		||||
            $frag = $node->ownerDocument->createDocumentFragment();
 | 
			
		||||
            $frag->appendXML($val);
 | 
			
		||||
            $node->appendChild($frag);
 | 
			
		||||
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final protected function appendText(\DOMNode $node, string $val): bool
 | 
			
		||||
    {
 | 
			
		||||
        $nodeText = $node->ownerDocument->createTextNode($val);
 | 
			
		||||
        $node->appendChild($nodeText);
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final protected function appendCData(\DOMNode $node, string $val): bool
 | 
			
		||||
    {
 | 
			
		||||
        $nodeText = $node->ownerDocument->createCDATASection($val);
 | 
			
		||||
        $node->appendChild($nodeText);
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    final protected function appendDocumentFragment(\DOMNode $node, \DOMDocumentFragment $fragment): bool
 | 
			
		||||
    {
 | 
			
		||||
        $node->appendChild($fragment);
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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.
 | 
			
		||||
     */
 | 
			
		||||
    final protected function isElementNameValid(string $name): bool
 | 
			
		||||
    {
 | 
			
		||||
        return $name
 | 
			
		||||
            && !str_contains($name, ' ')
 | 
			
		||||
            && preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Parse the input DOMNode into an array or a string.
 | 
			
		||||
     */
 | 
			
		||||
    private function parseXml(\DOMNode $node, array $context = []): array|string
 | 
			
		||||
    {
 | 
			
		||||
        $data = $this->parseXmlAttributes($node, $context);
 | 
			
		||||
 | 
			
		||||
        $value = $this->parseXmlValue($node, $context);
 | 
			
		||||
 | 
			
		||||
        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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Parse the input DOMNode attributes into an array.
 | 
			
		||||
     */
 | 
			
		||||
    private function parseXmlAttributes(\DOMNode $node, array $context = []): array
 | 
			
		||||
    {
 | 
			
		||||
        if (!$node->hasAttributes()) {
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $data = [];
 | 
			
		||||
        $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])) {
 | 
			
		||||
                $data['@'.$attr->nodeName] = $attr->nodeValue;
 | 
			
		||||
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (false !== $val = filter_var($attr->nodeValue, \FILTER_VALIDATE_INT)) {
 | 
			
		||||
                $data['@'.$attr->nodeName] = $val;
 | 
			
		||||
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $data['@'.$attr->nodeName] = (float) $attr->nodeValue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $data;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Parse the input DOMNode value (content and children) into an array or a string.
 | 
			
		||||
     */
 | 
			
		||||
    private function parseXmlValue(\DOMNode $node, array $context = []): array|string
 | 
			
		||||
    {
 | 
			
		||||
        if (!$node->hasChildNodes()) {
 | 
			
		||||
            return $node->nodeValue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, [\XML_TEXT_NODE, \XML_CDATA_SECTION_NODE])) {
 | 
			
		||||
            return $node->firstChild->nodeValue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $value = [];
 | 
			
		||||
        $decoderIgnoredNodeTypes = $context[self::DECODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::DECODER_IGNORED_NODE_TYPES];
 | 
			
		||||
        foreach ($node->childNodes as $subnode) {
 | 
			
		||||
            if (\in_array($subnode->nodeType, $decoderIgnoredNodeTypes, true)) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $val = $this->parseXml($subnode, $context);
 | 
			
		||||
 | 
			
		||||
            if ('item' === $subnode->nodeName && isset($val['@key'])) {
 | 
			
		||||
                $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 (!$asCollection && \is_array($val) && 1 === \count($val)) {
 | 
			
		||||
                $value[$key] = current($val);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function addXmlNamespaces(array $data, \DOMNode $node, \DOMDocument $document): array
 | 
			
		||||
    {
 | 
			
		||||
        $xpath = new \DOMXPath($document);
 | 
			
		||||
 | 
			
		||||
        foreach ($xpath->query('namespace::*', $node) as $nsNode) {
 | 
			
		||||
            $data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        unset($data['@xmlns:xml']);
 | 
			
		||||
 | 
			
		||||
        return $data;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Parse the data and convert it to DOMElements.
 | 
			
		||||
     *
 | 
			
		||||
     * @throws NotEncodableValueException
 | 
			
		||||
     */
 | 
			
		||||
    private function buildXml(\DOMNode $parentNode, mixed $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, $format)))) {
 | 
			
		||||
            foreach ($data as $key => $data) {
 | 
			
		||||
                // 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;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if ($context[self::IGNORE_EMPTY_ATTRIBUTES] ?? $this->defaultContext[self::IGNORE_EMPTY_ATTRIBUTES]) {
 | 
			
		||||
                        if (null === $data || '' === $data) {
 | 
			
		||||
                            continue;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    $parentNode->setAttribute($attributeName, $data);
 | 
			
		||||
                } elseif ('#' === $key) {
 | 
			
		||||
                    $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)))) {
 | 
			
		||||
                        /*
 | 
			
		||||
                         * Create nodes to append to $parentNode based on the $key of this array
 | 
			
		||||
                         * Produces <xml><item>0</item><item>1</item></xml>
 | 
			
		||||
                         * From ["item" => [0,1]];.
 | 
			
		||||
                         */
 | 
			
		||||
                        foreach ($data as $subData) {
 | 
			
		||||
                            $append = $this->appendNode($parentNode, $subData, $format, $context, $key);
 | 
			
		||||
                        }
 | 
			
		||||
                    } else {
 | 
			
		||||
                        $append = $this->appendNode($parentNode, $data, $format, $context, $key);
 | 
			
		||||
                    }
 | 
			
		||||
                } elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
 | 
			
		||||
                    $append = $this->appendNode($parentNode, $data, $format, $context, 'item', $key);
 | 
			
		||||
                } elseif (null !== $data || !$removeEmptyTags) {
 | 
			
		||||
                    $append = $this->appendNode($parentNode, $data, $format, $context, $key);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return $append;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (\is_object($data)) {
 | 
			
		||||
            if (null === $this->serializer) {
 | 
			
		||||
                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, $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
 | 
			
		||||
            if (!$parentNode->parentNode->parentNode) {
 | 
			
		||||
                $root = $parentNode->parentNode;
 | 
			
		||||
                $root->removeChild($parentNode);
 | 
			
		||||
 | 
			
		||||
                return $this->appendNode($root, $data, $format, $context, $xmlRootNodeName);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            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))));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Selects the type of node to create and appends it to the parent.
 | 
			
		||||
     */
 | 
			
		||||
    private function appendNode(\DOMNode $parentNode, mixed $data, string $format, array $context, string $nodeName, ?string $key = null): bool
 | 
			
		||||
    {
 | 
			
		||||
        $dom = $parentNode instanceof \DOMDocument ? $parentNode : $parentNode->ownerDocument;
 | 
			
		||||
        $node = $dom->createElement($nodeName);
 | 
			
		||||
        if (null !== $key) {
 | 
			
		||||
            $node->setAttribute('key', $key);
 | 
			
		||||
        }
 | 
			
		||||
        $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);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $appendNode;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Checks if a value contains any characters which would require CDATA wrapping.
 | 
			
		||||
     */
 | 
			
		||||
    private function needsCdataWrapping(string $val, array $context): bool
 | 
			
		||||
    {
 | 
			
		||||
        return ($context[self::CDATA_WRAPPING] ?? $this->defaultContext[self::CDATA_WRAPPING]) && preg_match($context[self::CDATA_WRAPPING_PATTERN] ?? $this->defaultContext[self::CDATA_WRAPPING_PATTERN], $val);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Tests the value being passed and decide what sort of element to create.
 | 
			
		||||
     *
 | 
			
		||||
     * @throws NotEncodableValueException
 | 
			
		||||
     */
 | 
			
		||||
    private function selectNodeType(\DOMNode $node, mixed $val, string $format, array $context): bool
 | 
			
		||||
    {
 | 
			
		||||
        if (\is_array($val)) {
 | 
			
		||||
            return $this->buildXml($node, $val, $format, $context);
 | 
			
		||||
        } elseif ($val instanceof \SimpleXMLElement) {
 | 
			
		||||
            $child = $node->ownerDocument->importNode(dom_import_simplexml($val), true);
 | 
			
		||||
            $node->appendChild($child);
 | 
			
		||||
        } elseif ($val instanceof \Traversable) {
 | 
			
		||||
            $this->buildXml($node, $val, $format, $context);
 | 
			
		||||
        } elseif ($val instanceof \DOMNode) {
 | 
			
		||||
            $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, $format, $context), $format, $context);
 | 
			
		||||
        } elseif (is_numeric($val)) {
 | 
			
		||||
            return $this->appendText($node, (string) $val);
 | 
			
		||||
        } elseif (\is_string($val) && $this->needsCdataWrapping($val, $context)) {
 | 
			
		||||
            return $this->appendCData($node, $val);
 | 
			
		||||
        } elseif (\is_string($val)) {
 | 
			
		||||
            return $this->appendText($node, $val);
 | 
			
		||||
        } elseif (\is_bool($val)) {
 | 
			
		||||
            return $this->appendText($node, (int) $val);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a DOM document, taking serializer options into account.
 | 
			
		||||
     */
 | 
			
		||||
    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
 | 
			
		||||
            self::FORMAT_OUTPUT => 'formatOutput',
 | 
			
		||||
            // the version number of the document
 | 
			
		||||
            self::VERSION => 'xmlVersion',
 | 
			
		||||
            // the encoding of the document
 | 
			
		||||
            self::ENCODING => 'encoding',
 | 
			
		||||
            // whether the document is standalone
 | 
			
		||||
            self::STANDALONE => 'xmlStandalone',
 | 
			
		||||
        ];
 | 
			
		||||
        foreach ($xmlOptions as $xmlOption => $documentProperty) {
 | 
			
		||||
            if ($contextOption = $context[$xmlOption] ?? $this->defaultContext[$xmlOption] ?? false) {
 | 
			
		||||
                $document->$documentProperty = $contextOption;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $document;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @throws NotEncodableValueException
 | 
			
		||||
     */
 | 
			
		||||
    private function saveXml(\DOMDocument $document, ?\DOMNode $node = null, ?int $options = null): string
 | 
			
		||||
    {
 | 
			
		||||
        $prevErrorHandler = set_error_handler(static function ($type, $message, $file, $line, $context = []) use (&$prevErrorHandler) {
 | 
			
		||||
            if (\E_ERROR === $type || \E_WARNING === $type) {
 | 
			
		||||
                throw new NotEncodableValueException($message);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return $prevErrorHandler ? $prevErrorHandler($type, $message, $file, $line, $context) : false;
 | 
			
		||||
        });
 | 
			
		||||
        try {
 | 
			
		||||
            return $document->saveXML($node, $options);
 | 
			
		||||
        } finally {
 | 
			
		||||
            restore_error_handler();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										101
									
								
								vendor/symfony/serializer/Encoder/YamlEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										101
									
								
								vendor/symfony/serializer/Encoder/YamlEncoder.php
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,101 @@
 | 
			
		||||
<?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\Exception\NotEncodableValueException;
 | 
			
		||||
use Symfony\Component\Serializer\Exception\RuntimeException;
 | 
			
		||||
use Symfony\Component\Yaml\Dumper;
 | 
			
		||||
use Symfony\Component\Yaml\Exception\ParseException;
 | 
			
		||||
use Symfony\Component\Yaml\Parser;
 | 
			
		||||
use Symfony\Component\Yaml\Yaml;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Encodes YAML data.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Kévin Dunglas <dunglas@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
class YamlEncoder implements EncoderInterface, DecoderInterface
 | 
			
		||||
{
 | 
			
		||||
    public const FORMAT = 'yaml';
 | 
			
		||||
    private const ALTERNATIVE_FORMAT = 'yml';
 | 
			
		||||
 | 
			
		||||
    public const PRESERVE_EMPTY_OBJECTS = 'preserve_empty_objects';
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Override the amount of spaces to use for indentation of nested nodes.
 | 
			
		||||
     *
 | 
			
		||||
     * This option only works in the constructor, not in calls to `encode`.
 | 
			
		||||
     */
 | 
			
		||||
    public const YAML_INDENTATION = 'yaml_indentation';
 | 
			
		||||
 | 
			
		||||
    public const YAML_INLINE = 'yaml_inline';
 | 
			
		||||
    /**
 | 
			
		||||
     * Initial indentation for root element.
 | 
			
		||||
     */
 | 
			
		||||
    public const YAML_INDENT = 'yaml_indent';
 | 
			
		||||
    public const YAML_FLAGS = 'yaml_flags';
 | 
			
		||||
 | 
			
		||||
    private readonly Dumper $dumper;
 | 
			
		||||
    private readonly Parser $parser;
 | 
			
		||||
    private array $defaultContext = [
 | 
			
		||||
        self::YAML_INLINE => 0,
 | 
			
		||||
        self::YAML_INDENT => 0,
 | 
			
		||||
        self::YAML_FLAGS => 0,
 | 
			
		||||
    ];
 | 
			
		||||
 | 
			
		||||
    public function __construct(?Dumper $dumper = null, ?Parser $parser = null, array $defaultContext = [])
 | 
			
		||||
    {
 | 
			
		||||
        if (!class_exists(Dumper::class)) {
 | 
			
		||||
            throw new RuntimeException('The YamlEncoder class requires the "Yaml" component. Try running "composer require symfony/yaml".');
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!$dumper) {
 | 
			
		||||
            $dumper = \array_key_exists(self::YAML_INDENTATION, $defaultContext) ? new Dumper($defaultContext[self::YAML_INDENTATION]) : new Dumper();
 | 
			
		||||
        }
 | 
			
		||||
        $this->dumper = $dumper;
 | 
			
		||||
        $this->parser = $parser ?? new Parser();
 | 
			
		||||
        unset($defaultContext[self::YAML_INDENTATION]);
 | 
			
		||||
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function encode(mixed $data, string $format, array $context = []): string
 | 
			
		||||
    {
 | 
			
		||||
        $context = array_merge($this->defaultContext, $context);
 | 
			
		||||
 | 
			
		||||
        if ($context[self::PRESERVE_EMPTY_OBJECTS] ?? false) {
 | 
			
		||||
            $context[self::YAML_FLAGS] |= Yaml::DUMP_OBJECT_AS_MAP;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->dumper->dump($data, $context[self::YAML_INLINE], $context[self::YAML_INDENT], $context[self::YAML_FLAGS]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsEncoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function decode(string $data, string $format, array $context = []): mixed
 | 
			
		||||
    {
 | 
			
		||||
        $context = array_merge($this->defaultContext, $context);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            return $this->parser->parse($data, $context[self::YAML_FLAGS]);
 | 
			
		||||
        } catch (ParseException $e) {
 | 
			
		||||
            throw new NotEncodableValueException($e->getMessage(), $e->getCode(), $e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function supportsDecoding(string $format): bool
 | 
			
		||||
    {
 | 
			
		||||
        return self::FORMAT === $format || self::ALTERNATIVE_FORMAT === $format;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user