Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663
This commit is contained in:
parent
eb34d130a8
commit
f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions
82
vendor/symfony/serializer/Encoder/ChainDecoder.php
vendored
Normal file
82
vendor/symfony/serializer/Encoder/ChainDecoder.php
vendored
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?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>
|
||||
*/
|
||||
class ChainDecoder implements DecoderInterface
|
||||
{
|
||||
protected $decoders = array();
|
||||
protected $decoderByFormat = array();
|
||||
|
||||
public function __construct(array $decoders = array())
|
||||
{
|
||||
$this->decoders = $decoders;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
final public function decode($data, $format, array $context = array())
|
||||
{
|
||||
return $this->getDecoder($format)->decode($data, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
try {
|
||||
$this->getDecoder($format);
|
||||
} catch (RuntimeException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decoder supporting the format.
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @return DecoderInterface
|
||||
*
|
||||
* @throws RuntimeException if no decoder is found
|
||||
*/
|
||||
private function getDecoder($format)
|
||||
{
|
||||
if (isset($this->decoderByFormat[$format])
|
||||
&& isset($this->decoders[$this->decoderByFormat[$format]])
|
||||
) {
|
||||
return $this->decoders[$this->decoderByFormat[$format]];
|
||||
}
|
||||
|
||||
foreach ($this->decoders as $i => $decoder) {
|
||||
if ($decoder->supportsDecoding($format)) {
|
||||
$this->decoderByFormat[$format] = $i;
|
||||
|
||||
return $decoder;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf('No decoder found for format "%s".', $format));
|
||||
}
|
||||
}
|
104
vendor/symfony/serializer/Encoder/ChainEncoder.php
vendored
Normal file
104
vendor/symfony/serializer/Encoder/ChainEncoder.php
vendored
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*/
|
||||
class ChainEncoder implements EncoderInterface
|
||||
{
|
||||
protected $encoders = array();
|
||||
protected $encoderByFormat = array();
|
||||
|
||||
public function __construct(array $encoders = array())
|
||||
{
|
||||
$this->encoders = $encoders;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
final public function encode($data, $format, array $context = array())
|
||||
{
|
||||
return $this->getEncoder($format)->encode($data, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
try {
|
||||
$this->getEncoder($format);
|
||||
} catch (RuntimeException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the normalization is needed for the given format.
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function needsNormalization($format)
|
||||
{
|
||||
$encoder = $this->getEncoder($format);
|
||||
|
||||
if (!$encoder instanceof NormalizationAwareInterface) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($encoder instanceof self) {
|
||||
return $encoder->needsNormalization($format);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the encoder supporting the format.
|
||||
*
|
||||
* @param string $format
|
||||
*
|
||||
* @return EncoderInterface
|
||||
*
|
||||
* @throws RuntimeException if no encoder is found
|
||||
*/
|
||||
private function getEncoder($format)
|
||||
{
|
||||
if (isset($this->encoderByFormat[$format])
|
||||
&& isset($this->encoders[$this->encoderByFormat[$format]])
|
||||
) {
|
||||
return $this->encoders[$this->encoderByFormat[$format]];
|
||||
}
|
||||
|
||||
foreach ($this->encoders as $i => $encoder) {
|
||||
if ($encoder->supportsEncoding($format)) {
|
||||
$this->encoderByFormat[$format] = $i;
|
||||
|
||||
return $encoder;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
|
||||
}
|
||||
}
|
49
vendor/symfony/serializer/Encoder/DecoderInterface.php
vendored
Normal file
49
vendor/symfony/serializer/Encoder/DecoderInterface.php
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Defines the interface of decoders.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
public function decode($data, $format, array $context = array());
|
||||
|
||||
/**
|
||||
* Checks whether the deserializer can decode from given format.
|
||||
*
|
||||
* @param string $format format name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsDecoding($format);
|
||||
}
|
44
vendor/symfony/serializer/Encoder/EncoderInterface.php
vendored
Normal file
44
vendor/symfony/serializer/Encoder/EncoderInterface.php
vendored
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Defines the interface of encoders.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @return scalar
|
||||
*
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
public function encode($data, $format, array $context = array());
|
||||
|
||||
/**
|
||||
* Checks whether the serializer can encode to given format.
|
||||
*
|
||||
* @param string $format format name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsEncoding($format);
|
||||
}
|
142
vendor/symfony/serializer/Encoder/JsonDecode.php
vendored
Normal file
142
vendor/symfony/serializer/Encoder/JsonDecode.php
vendored
Normal file
|
@ -0,0 +1,142 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Decodes JSON data.
|
||||
*
|
||||
* @author Sander Coolen <sander@jibber.nl>
|
||||
*/
|
||||
class JsonDecode implements DecoderInterface
|
||||
{
|
||||
/**
|
||||
* Specifies if the returned result should be an associative array or a nested stdClass object hierarchy.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $associative;
|
||||
|
||||
/**
|
||||
* Specifies the recursion depth.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $recursionDepth;
|
||||
|
||||
private $lastError = JSON_ERROR_NONE;
|
||||
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public function __construct($associative = false, $depth = 512)
|
||||
{
|
||||
$this->associative = $associative;
|
||||
$this->recursionDepth = (int) $depth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last decoding error (if any).
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @deprecated since version 2.5, to be removed in 3.0.
|
||||
* The {@self decode()} method throws an exception if error found.
|
||||
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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. Only supported with PHP 5.4.0 and higher
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws UnexpectedValueException
|
||||
*
|
||||
* @see http://php.net/json_decode json_decode
|
||||
*/
|
||||
public function decode($data, $format, array $context = array())
|
||||
{
|
||||
$context = $this->resolveContext($context);
|
||||
|
||||
$associative = $context['json_decode_associative'];
|
||||
$recursionDepth = $context['json_decode_recursion_depth'];
|
||||
$options = $context['json_decode_options'];
|
||||
|
||||
if (PHP_VERSION_ID >= 50400) {
|
||||
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
|
||||
} else {
|
||||
$decodedData = json_decode($data, $associative, $recursionDepth);
|
||||
}
|
||||
|
||||
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
|
||||
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
|
||||
}
|
||||
|
||||
return $decodedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return JsonEncoder::FORMAT === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the default options of the Json Decoder with the passed context.
|
||||
*
|
||||
* @param array $context
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function resolveContext(array $context)
|
||||
{
|
||||
$defaultOptions = array(
|
||||
'json_decode_associative' => $this->associative,
|
||||
'json_decode_recursion_depth' => $this->recursionDepth,
|
||||
'json_decode_options' => 0,
|
||||
);
|
||||
|
||||
return array_merge($defaultOptions, $context);
|
||||
}
|
||||
}
|
84
vendor/symfony/serializer/Encoder/JsonEncode.php
vendored
Normal file
84
vendor/symfony/serializer/Encoder/JsonEncode.php
vendored
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Encodes JSON data.
|
||||
*
|
||||
* @author Sander Coolen <sander@jibber.nl>
|
||||
*/
|
||||
class JsonEncode implements EncoderInterface
|
||||
{
|
||||
private $options;
|
||||
private $lastError = JSON_ERROR_NONE;
|
||||
|
||||
public function __construct($bitmask = 0)
|
||||
{
|
||||
$this->options = $bitmask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last encoding error (if any).
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @deprecated since version 2.5, to be removed in 3.0.
|
||||
* The {@self encode()} throws an exception if error found.
|
||||
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
|
||||
*/
|
||||
public function getLastError()
|
||||
{
|
||||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the encode() method instead to get the last JSON encoding error.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->lastError;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes PHP data to a JSON string.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data, $format, array $context = array())
|
||||
{
|
||||
$context = $this->resolveContext($context);
|
||||
|
||||
$encodedJson = json_encode($data, $context['json_encode_options']);
|
||||
|
||||
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
|
||||
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage());
|
||||
}
|
||||
|
||||
return $encodedJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return JsonEncoder::FORMAT === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge default json encode options with context.
|
||||
*
|
||||
* @param array $context
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function resolveContext(array $context = array())
|
||||
{
|
||||
return array_merge(array('json_encode_options' => $this->options), $context);
|
||||
}
|
||||
}
|
125
vendor/symfony/serializer/Encoder/JsonEncoder.php
vendored
Normal file
125
vendor/symfony/serializer/Encoder/JsonEncoder.php
vendored
Normal file
|
@ -0,0 +1,125 @@
|
|||
<?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
|
||||
{
|
||||
const FORMAT = 'json';
|
||||
|
||||
/**
|
||||
* @var JsonEncode
|
||||
*/
|
||||
protected $encodingImpl;
|
||||
|
||||
/**
|
||||
* @var JsonDecode
|
||||
*/
|
||||
protected $decodingImpl;
|
||||
|
||||
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
|
||||
{
|
||||
$this->encodingImpl = $encodingImpl ?: new JsonEncode();
|
||||
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last encoding error (if any).
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @deprecated since version 2.5, to be removed in 3.0. JsonEncode throws exception if an error is found.
|
||||
*/
|
||||
public function getLastEncodingError()
|
||||
{
|
||||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\Component\Serializer\Encoder\JsonEncode::encode() method instead to get the last JSON encoding error.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->encodingImpl->getLastError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last decoding error (if any).
|
||||
*
|
||||
* @return int
|
||||
*
|
||||
* @deprecated since version 2.5, to be removed in 3.0. JsonDecode throws exception if an error is found.
|
||||
*/
|
||||
public function getLastDecodingError()
|
||||
{
|
||||
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0. Catch the exception raised by the Symfony\Component\Serializer\Encoder\JsonDecode::decode() method instead to get the last JSON decoding error.', E_USER_DEPRECATED);
|
||||
|
||||
return $this->decodingImpl->getLastError();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data, $format, array $context = array())
|
||||
{
|
||||
return $this->encodingImpl->encode($data, self::FORMAT, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data, $format, array $context = array())
|
||||
{
|
||||
return $this->decodingImpl->decode($data, self::FORMAT, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return self::FORMAT === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return self::FORMAT === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves json_last_error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getLastErrorMessage()
|
||||
{
|
||||
if (function_exists('json_last_error_msg')) {
|
||||
return json_last_error_msg();
|
||||
}
|
||||
|
||||
switch (json_last_error()) {
|
||||
case JSON_ERROR_DEPTH:
|
||||
return 'Maximum stack depth exceeded';
|
||||
case JSON_ERROR_STATE_MISMATCH:
|
||||
return 'Underflow or the modes mismatch';
|
||||
case JSON_ERROR_CTRL_CHAR:
|
||||
return 'Unexpected control character found';
|
||||
case JSON_ERROR_SYNTAX:
|
||||
return 'Syntax error, malformed JSON';
|
||||
case JSON_ERROR_UTF8:
|
||||
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
default:
|
||||
return 'Unknown error';
|
||||
}
|
||||
}
|
||||
}
|
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
|
||||
{
|
||||
}
|
33
vendor/symfony/serializer/Encoder/SerializerAwareEncoder.php
vendored
Normal file
33
vendor/symfony/serializer/Encoder/SerializerAwareEncoder.php
vendored
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?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\SerializerInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||
|
||||
/**
|
||||
* SerializerAware Encoder implementation.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
abstract class SerializerAwareEncoder implements SerializerAwareInterface
|
||||
{
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setSerializer(SerializerInterface $serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
}
|
536
vendor/symfony/serializer/Encoder/XmlEncoder.php
vendored
Normal file
536
vendor/symfony/serializer/Encoder/XmlEncoder.php
vendored
Normal file
|
@ -0,0 +1,536 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*/
|
||||
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface, NormalizationAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var \DOMDocument
|
||||
*/
|
||||
private $dom;
|
||||
private $format;
|
||||
private $context;
|
||||
private $rootNodeName = 'response';
|
||||
|
||||
/**
|
||||
* Construct new XmlEncoder and allow to change the root node element name.
|
||||
*
|
||||
* @param string $rootNodeName
|
||||
*/
|
||||
public function __construct($rootNodeName = 'response')
|
||||
{
|
||||
$this->rootNodeName = $rootNodeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data, $format, array $context = array())
|
||||
{
|
||||
if ($data instanceof \DOMDocument) {
|
||||
return $data->saveXML();
|
||||
}
|
||||
|
||||
$xmlRootNodeName = $this->resolveXmlRootName($context);
|
||||
|
||||
$this->dom = $this->createDomDocument($context);
|
||||
$this->format = $format;
|
||||
$this->context = $context;
|
||||
|
||||
if (null !== $data && !is_scalar($data)) {
|
||||
$root = $this->dom->createElement($xmlRootNodeName);
|
||||
$this->dom->appendChild($root);
|
||||
$this->buildXml($root, $data, $xmlRootNodeName);
|
||||
} else {
|
||||
$this->appendNode($this->dom, $data, $xmlRootNodeName);
|
||||
}
|
||||
|
||||
return $this->dom->saveXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data, $format, array $context = array())
|
||||
{
|
||||
if ('' === trim($data)) {
|
||||
throw new UnexpectedValueException('Invalid XML data, it can not be empty.');
|
||||
}
|
||||
|
||||
$internalErrors = libxml_use_internal_errors(true);
|
||||
$disableEntities = libxml_disable_entity_loader(true);
|
||||
libxml_clear_errors();
|
||||
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadXML($data, LIBXML_NONET | LIBXML_NOBLANKS);
|
||||
|
||||
libxml_use_internal_errors($internalErrors);
|
||||
libxml_disable_entity_loader($disableEntities);
|
||||
|
||||
if ($error = libxml_get_last_error()) {
|
||||
libxml_clear_errors();
|
||||
|
||||
throw new UnexpectedValueException($error->message);
|
||||
}
|
||||
|
||||
foreach ($dom->childNodes as $child) {
|
||||
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
|
||||
throw new UnexpectedValueException('Document types are not allowed.');
|
||||
}
|
||||
}
|
||||
|
||||
$rootNode = $dom->firstChild;
|
||||
|
||||
// todo: throw an exception if the root node name is not correctly configured (bc)
|
||||
|
||||
if ($rootNode->hasChildNodes()) {
|
||||
$xpath = new \DOMXPath($dom);
|
||||
$data = array();
|
||||
foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) {
|
||||
$data['@'.$nsNode->nodeName] = $nsNode->nodeValue;
|
||||
}
|
||||
|
||||
unset($data['@xmlns:xml']);
|
||||
|
||||
if (empty($data)) {
|
||||
return $this->parseXml($rootNode);
|
||||
}
|
||||
|
||||
return array_merge($data, (array) $this->parseXml($rootNode));
|
||||
}
|
||||
|
||||
if (!$rootNode->hasAttributes()) {
|
||||
return $rootNode->nodeValue;
|
||||
}
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach ($rootNode->attributes as $attrKey => $attr) {
|
||||
$data['@'.$attrKey] = $attr->nodeValue;
|
||||
}
|
||||
|
||||
$data['#'] = $rootNode->nodeValue;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return 'xml' === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return 'xml' === $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the root node name.
|
||||
*
|
||||
* @param string $name root node name
|
||||
*/
|
||||
public function setRootNodeName($name)
|
||||
{
|
||||
$this->rootNodeName = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root node name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRootNodeName()
|
||||
{
|
||||
return $this->rootNodeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DOMNode $node
|
||||
* @param string $val
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final protected function appendXMLString(\DOMNode $node, $val)
|
||||
{
|
||||
if (strlen($val) > 0) {
|
||||
$frag = $this->dom->createDocumentFragment();
|
||||
$frag->appendXML($val);
|
||||
$node->appendChild($frag);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DOMNode $node
|
||||
* @param string $val
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final protected function appendText(\DOMNode $node, $val)
|
||||
{
|
||||
$nodeText = $this->dom->createTextNode($val);
|
||||
$node->appendChild($nodeText);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DOMNode $node
|
||||
* @param string $val
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final protected function appendCData(\DOMNode $node, $val)
|
||||
{
|
||||
$nodeText = $this->dom->createCDATASection($val);
|
||||
$node->appendChild($nodeText);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DOMNode $node
|
||||
* @param \DOMDocumentFragment $fragment
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final protected function appendDocumentFragment(\DOMNode $node, $fragment)
|
||||
{
|
||||
if ($fragment instanceof \DOMDocumentFragment) {
|
||||
$node->appendChild($fragment);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the name is a valid xml element name.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
final protected function isElementNameValid($name)
|
||||
{
|
||||
return $name &&
|
||||
false === strpos($name, ' ') &&
|
||||
preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the input DOMNode into an array or a string.
|
||||
*
|
||||
* @param \DOMNode $node xml to parse
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
private function parseXml(\DOMNode $node)
|
||||
{
|
||||
$data = $this->parseXmlAttributes($node);
|
||||
|
||||
$value = $this->parseXmlValue($node);
|
||||
|
||||
if (!count($data)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (!is_array($value)) {
|
||||
$data['#'] = $value;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
if (1 === count($value) && key($value)) {
|
||||
$data[key($value)] = current($value);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach ($value as $key => $val) {
|
||||
$data[$key] = $val;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the input DOMNode attributes into an array.
|
||||
*
|
||||
* @param \DOMNode $node xml to parse
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function parseXmlAttributes(\DOMNode $node)
|
||||
{
|
||||
if (!$node->hasAttributes()) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$data = array();
|
||||
|
||||
foreach ($node->attributes as $attr) {
|
||||
if (ctype_digit($attr->nodeValue)) {
|
||||
$data['@'.$attr->nodeName] = (int) $attr->nodeValue;
|
||||
} else {
|
||||
$data['@'.$attr->nodeName] = $attr->nodeValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the input DOMNode value (content and children) into an array or a string.
|
||||
*
|
||||
* @param \DOMNode $node xml to parse
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
private function parseXmlValue(\DOMNode $node)
|
||||
{
|
||||
if (!$node->hasChildNodes()) {
|
||||
return $node->nodeValue;
|
||||
}
|
||||
|
||||
if (1 === $node->childNodes->length && in_array($node->firstChild->nodeType, array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE))) {
|
||||
return $node->firstChild->nodeValue;
|
||||
}
|
||||
|
||||
$value = array();
|
||||
|
||||
foreach ($node->childNodes as $subnode) {
|
||||
$val = $this->parseXml($subnode);
|
||||
|
||||
if ('item' === $subnode->nodeName && isset($val['@key'])) {
|
||||
if (isset($val['#'])) {
|
||||
$value[$val['@key']] = $val['#'];
|
||||
} else {
|
||||
$value[$val['@key']] = $val;
|
||||
}
|
||||
} else {
|
||||
$value[$subnode->nodeName][] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($value as $key => $val) {
|
||||
if (is_array($val) && 1 === count($val)) {
|
||||
$value[$key] = current($val);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the data and convert it to DOMElements.
|
||||
*
|
||||
* @param \DOMNode $parentNode
|
||||
* @param array|object $data
|
||||
* @param string|null $xmlRootNodeName
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
|
||||
{
|
||||
$append = true;
|
||||
|
||||
if (is_array($data) || $data instanceof \Traversable) {
|
||||
foreach ($data as $key => $data) {
|
||||
//Ah this is the magic @ attribute types.
|
||||
if (0 === strpos($key, '@') && is_scalar($data) && $this->isElementNameValid($attributeName = substr($key, 1))) {
|
||||
$parentNode->setAttribute($attributeName, $data);
|
||||
} elseif ($key === '#') {
|
||||
$append = $this->selectNodeType($parentNode, $data);
|
||||
} elseif (is_array($data) && false === is_numeric($key)) {
|
||||
// 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 array("item" => array(0,1));.
|
||||
*/
|
||||
foreach ($data as $subData) {
|
||||
$append = $this->appendNode($parentNode, $subData, $key);
|
||||
}
|
||||
} else {
|
||||
$append = $this->appendNode($parentNode, $data, $key);
|
||||
}
|
||||
} elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
|
||||
$append = $this->appendNode($parentNode, $data, 'item', $key);
|
||||
} else {
|
||||
$append = $this->appendNode($parentNode, $data, $key);
|
||||
}
|
||||
}
|
||||
|
||||
return $append;
|
||||
}
|
||||
|
||||
if (is_object($data)) {
|
||||
$data = $this->serializer->normalize($data, $this->format, $this->context);
|
||||
if (null !== $data && !is_scalar($data)) {
|
||||
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
|
||||
}
|
||||
|
||||
// top level data object was normalized into a scalar
|
||||
if (!$parentNode->parentNode->parentNode) {
|
||||
$root = $parentNode->parentNode;
|
||||
$root->removeChild($parentNode);
|
||||
|
||||
return $this->appendNode($root, $data, $xmlRootNodeName);
|
||||
}
|
||||
|
||||
return $this->appendNode($parentNode, $data, 'data');
|
||||
}
|
||||
|
||||
throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the type of node to create and appends it to the parent.
|
||||
*
|
||||
* @param \DOMNode $parentNode
|
||||
* @param array|object $data
|
||||
* @param string $nodeName
|
||||
* @param string $key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function appendNode(\DOMNode $parentNode, $data, $nodeName, $key = null)
|
||||
{
|
||||
$node = $this->dom->createElement($nodeName);
|
||||
if (null !== $key) {
|
||||
$node->setAttribute('key', $key);
|
||||
}
|
||||
$appendNode = $this->selectNodeType($node, $data);
|
||||
// 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.
|
||||
*
|
||||
* @param string $val
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function needsCdataWrapping($val)
|
||||
{
|
||||
return preg_match('/[<>&]/', $val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the value being passed and decide what sort of element to create.
|
||||
*
|
||||
* @param \DOMNode $node
|
||||
* @param mixed $val
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function selectNodeType(\DOMNode $node, $val)
|
||||
{
|
||||
if (is_array($val)) {
|
||||
return $this->buildXml($node, $val);
|
||||
} elseif ($val instanceof \SimpleXMLElement) {
|
||||
$child = $this->dom->importNode(dom_import_simplexml($val), true);
|
||||
$node->appendChild($child);
|
||||
} elseif ($val instanceof \Traversable) {
|
||||
$this->buildXml($node, $val);
|
||||
} elseif (is_object($val)) {
|
||||
return $this->buildXml($node, $this->serializer->normalize($val, $this->format, $this->context));
|
||||
} elseif (is_numeric($val)) {
|
||||
return $this->appendText($node, (string) $val);
|
||||
} elseif (is_string($val) && $this->needsCdataWrapping($val)) {
|
||||
return $this->appendCData($node, $val);
|
||||
} elseif (is_string($val)) {
|
||||
return $this->appendText($node, $val);
|
||||
} elseif (is_bool($val)) {
|
||||
return $this->appendText($node, (int) $val);
|
||||
} elseif ($val instanceof \DOMNode) {
|
||||
$child = $this->dom->importNode($val, true);
|
||||
$node->appendChild($child);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get real XML root node name, taking serializer options into account.
|
||||
*
|
||||
* @param array $context
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function resolveXmlRootName(array $context = array())
|
||||
{
|
||||
return isset($context['xml_root_node_name'])
|
||||
? $context['xml_root_node_name']
|
||||
: $this->rootNodeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a DOM document, taking serializer options into account.
|
||||
*
|
||||
* @param array $context options that the encoder has access to.
|
||||
*
|
||||
* @return \DOMDocument
|
||||
*/
|
||||
private function createDomDocument(array $context)
|
||||
{
|
||||
$document = new \DOMDocument();
|
||||
|
||||
// Set an attribute on the DOM document specifying, as part of the XML declaration,
|
||||
$xmlOptions = array(
|
||||
// nicely formats output with indentation and extra space
|
||||
'xml_format_output' => 'formatOutput',
|
||||
// the version number of the document
|
||||
'xml_version' => 'xmlVersion',
|
||||
// the encoding of the document
|
||||
'xml_encoding' => 'encoding',
|
||||
// whether the document is standalone
|
||||
'xml_standalone' => 'xmlStandalone',
|
||||
);
|
||||
foreach ($xmlOptions as $xmlOption => $documentProperty) {
|
||||
if (isset($context[$xmlOption])) {
|
||||
$document->$documentProperty = $context[$xmlOption];
|
||||
}
|
||||
}
|
||||
|
||||
return $document;
|
||||
}
|
||||
}
|
Reference in a new issue