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
3
vendor/symfony/serializer/.gitignore
vendored
Normal file
3
vendor/symfony/serializer/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
vendor/
|
||||
composer.lock
|
||||
phpunit.xml
|
64
vendor/symfony/serializer/Annotation/Groups.php
vendored
Normal file
64
vendor/symfony/serializer/Annotation/Groups.php
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?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\Annotation;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Annotation class for @Groups().
|
||||
*
|
||||
* @Annotation
|
||||
* @Target({"PROPERTY", "METHOD"})
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class Groups
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $groups;
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function __construct(array $data)
|
||||
{
|
||||
if (!isset($data['value']) || !$data['value']) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
|
||||
}
|
||||
|
||||
if (!is_array($data['value'])) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
|
||||
}
|
||||
|
||||
foreach ($data['value'] as $group) {
|
||||
if (!is_string($group)) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
|
||||
}
|
||||
}
|
||||
|
||||
$this->groups = $data['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets groups.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getGroups()
|
||||
{
|
||||
return $this->groups;
|
||||
}
|
||||
}
|
96
vendor/symfony/serializer/CHANGELOG.md
vendored
Normal file
96
vendor/symfony/serializer/CHANGELOG.md
vendored
Normal file
|
@ -0,0 +1,96 @@
|
|||
CHANGELOG
|
||||
=========
|
||||
|
||||
2.7.0
|
||||
-----
|
||||
|
||||
* added support for serialization and deserialization groups including
|
||||
annotations, XML and YAML mapping.
|
||||
* added `AbstractNormalizer` to factorise code and ease normalizers development
|
||||
* added circular references handling for `PropertyNormalizer`
|
||||
* added support for a context key called `object_to_populate` in `AbstractNormalizer`
|
||||
to reuse existing objects in the deserialization process
|
||||
* added `NameConverterInterface` and `CamelCaseToSnakeCaseNameConverter`
|
||||
* [DEPRECATION] `GetSetMethodNormalizer::setCamelizedAttributes()` and
|
||||
`PropertyNormalizer::setCamelizedAttributes()` are replaced by
|
||||
`CamelCaseToSnakeCaseNameConverter`
|
||||
* [DEPRECATION] the `Exception` interface has been renamed to `ExceptionInterface`
|
||||
* added `ObjectNormalizer` leveraging the `PropertyAccess` component to normalize
|
||||
objects containing both properties and getters / setters / issers / hassers methods.
|
||||
|
||||
2.6.0
|
||||
-----
|
||||
|
||||
* added a new serializer: `PropertyNormalizer`. Like `GetSetMethodNormalizer`,
|
||||
this normalizer will map an object's properties to an array.
|
||||
* added circular references handling for `GetSetMethodNormalizer`
|
||||
|
||||
2.5.0
|
||||
-----
|
||||
|
||||
* added support for `is.*` getters in `GetSetMethodNormalizer`
|
||||
|
||||
2.4.0
|
||||
-----
|
||||
|
||||
* added `$context` support for XMLEncoder.
|
||||
* [DEPRECATION] JsonEncode and JsonDecode where modified to throw
|
||||
an exception if error found. No need for get*Error() functions
|
||||
|
||||
2.3.0
|
||||
-----
|
||||
|
||||
* added `GetSetMethodNormalizer::setCamelizedAttributes` to allow calling
|
||||
camel cased methods for underscored properties
|
||||
|
||||
2.2.0
|
||||
-----
|
||||
|
||||
* [BC BREAK] All Serializer, Normalizer and Encoder interfaces have been
|
||||
modified to include an optional `$context` array parameter.
|
||||
* The XML Root name can now be configured with the `xml_root_name`
|
||||
parameter in the context option to the `XmlEncoder`.
|
||||
* Options to `json_encode` and `json_decode` can be passed through
|
||||
the context options of `JsonEncode` and `JsonDecode` encoder/decoders.
|
||||
|
||||
2.1.0
|
||||
-----
|
||||
|
||||
* added DecoderInterface::supportsDecoding(),
|
||||
EncoderInterface::supportsEncoding()
|
||||
* removed NormalizableInterface::denormalize(),
|
||||
NormalizerInterface::denormalize(),
|
||||
NormalizerInterface::supportsDenormalization()
|
||||
* removed normalize() denormalize() encode() decode() supportsSerialization()
|
||||
supportsDeserialization() supportsEncoding() supportsDecoding()
|
||||
getEncoder() from SerializerInterface
|
||||
* Serializer now implements NormalizerInterface, DenormalizerInterface,
|
||||
EncoderInterface, DecoderInterface in addition to SerializerInterface
|
||||
* added DenormalizableInterface and DenormalizerInterface
|
||||
* [BC BREAK] changed `GetSetMethodNormalizer`'s key names from all lowercased
|
||||
to camelCased (e.g. `mypropertyvalue` to `myPropertyValue`)
|
||||
* [BC BREAK] convert the `item` XML tag to an array
|
||||
|
||||
``` xml
|
||||
<?xml version="1.0"?>
|
||||
<response>
|
||||
<item><title><![CDATA[title1]]></title></item><item><title><![CDATA[title2]]></title></item>
|
||||
</response>
|
||||
```
|
||||
|
||||
Before:
|
||||
|
||||
Array()
|
||||
|
||||
After:
|
||||
|
||||
Array(
|
||||
[item] => Array(
|
||||
[0] => Array(
|
||||
[title] => title1
|
||||
)
|
||||
[1] => Array(
|
||||
[title] => title2
|
||||
)
|
||||
)
|
||||
)
|
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;
|
||||
}
|
||||
}
|
21
vendor/symfony/serializer/Exception/CircularReferenceException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/CircularReferenceException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* CircularReferenceException.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class CircularReferenceException extends RuntimeException
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/Exception.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/Exception.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* Base exception.
|
||||
*
|
||||
* @deprecated since version 2.7, to be removed in 3.0. Use ExceptionInterface instead.
|
||||
*/
|
||||
interface Exception
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/ExceptionInterface.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/ExceptionInterface.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* Base exception interface.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
interface ExceptionInterface extends Exception
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/InvalidArgumentException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/InvalidArgumentException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* InvalidArgumentException.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/LogicException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/LogicException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* LogicException.
|
||||
*
|
||||
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
|
||||
*/
|
||||
class LogicException extends \LogicException implements ExceptionInterface
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/MappingException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/MappingException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* MappingException.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class MappingException extends RuntimeException
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/RuntimeException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/RuntimeException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* RuntimeException.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class RuntimeException extends \RuntimeException implements ExceptionInterface
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/UnexpectedValueException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/UnexpectedValueException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* UnexpectedValueException.
|
||||
*
|
||||
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
|
||||
*/
|
||||
class UnexpectedValueException extends \UnexpectedValueException implements ExceptionInterface
|
||||
{
|
||||
}
|
21
vendor/symfony/serializer/Exception/UnsupportedException.php
vendored
Normal file
21
vendor/symfony/serializer/Exception/UnsupportedException.php
vendored
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?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\Exception;
|
||||
|
||||
/**
|
||||
* UnsupportedException.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class UnsupportedException extends InvalidArgumentException
|
||||
{
|
||||
}
|
19
vendor/symfony/serializer/LICENSE
vendored
Normal file
19
vendor/symfony/serializer/LICENSE
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
Copyright (c) 2004-2015 Fabien Potencier
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
94
vendor/symfony/serializer/Mapping/AttributeMetadata.php
vendored
Normal file
94
vendor/symfony/serializer/Mapping/AttributeMetadata.php
vendored
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?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\Mapping;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class AttributeMetadata implements AttributeMetadataInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @internal This property is public in order to reduce the size of the
|
||||
* class' serialized representation. Do not access it. Use
|
||||
* {@link getName()} instead.
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
* @internal This property is public in order to reduce the size of the
|
||||
* class' serialized representation. Do not access it. Use
|
||||
* {@link getGroups()} instead.
|
||||
*/
|
||||
public $groups = array();
|
||||
|
||||
/**
|
||||
* Constructs a metadata for the given attribute.
|
||||
*
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addGroup($group)
|
||||
{
|
||||
if (!in_array($group, $this->groups)) {
|
||||
$this->groups[] = $group;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getGroups()
|
||||
{
|
||||
return $this->groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function merge(AttributeMetadataInterface $attributeMetadata)
|
||||
{
|
||||
foreach ($attributeMetadata->getGroups() as $group) {
|
||||
$this->addGroup($group);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the names of the properties that should be serialized.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
return array('name', 'groups');
|
||||
}
|
||||
}
|
50
vendor/symfony/serializer/Mapping/AttributeMetadataInterface.php
vendored
Normal file
50
vendor/symfony/serializer/Mapping/AttributeMetadataInterface.php
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?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\Mapping;
|
||||
|
||||
/**
|
||||
* Stores metadata needed for serializing and deserializing attributes.
|
||||
*
|
||||
* Primarily, the metadata stores serialization groups.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface AttributeMetadataInterface
|
||||
{
|
||||
/**
|
||||
* Gets the attribute name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Adds this attribute to the given group.
|
||||
*
|
||||
* @param string $group
|
||||
*/
|
||||
public function addGroup($group);
|
||||
|
||||
/**
|
||||
* Gets groups of this attribute.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getGroups();
|
||||
|
||||
/**
|
||||
* Merges an {@see AttributeMetadataInterface} with in the current one.
|
||||
*
|
||||
* @param AttributeMetadataInterface $attributeMetadata
|
||||
*/
|
||||
public function merge(AttributeMetadataInterface $attributeMetadata);
|
||||
}
|
116
vendor/symfony/serializer/Mapping/ClassMetadata.php
vendored
Normal file
116
vendor/symfony/serializer/Mapping/ClassMetadata.php
vendored
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?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\Mapping;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ClassMetadata implements ClassMetadataInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @internal This property is public in order to reduce the size of the
|
||||
* class' serialized representation. Do not access it. Use
|
||||
* {@link getName()} instead.
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var AttributeMetadataInterface[]
|
||||
*
|
||||
* @internal This property is public in order to reduce the size of the
|
||||
* class' serialized representation. Do not access it. Use
|
||||
* {@link getAttributesMetadata()} instead.
|
||||
*/
|
||||
public $attributesMetadata = array();
|
||||
|
||||
/**
|
||||
* @var \ReflectionClass
|
||||
*/
|
||||
private $reflClass;
|
||||
|
||||
/**
|
||||
* Constructs a metadata for the given class.
|
||||
*
|
||||
* @param string $class
|
||||
*/
|
||||
public function __construct($class)
|
||||
{
|
||||
$this->name = $class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addAttributeMetadata(AttributeMetadataInterface $attributeMetadata)
|
||||
{
|
||||
$this->attributesMetadata[$attributeMetadata->getName()] = $attributeMetadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributesMetadata()
|
||||
{
|
||||
return $this->attributesMetadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function merge(ClassMetadataInterface $classMetadata)
|
||||
{
|
||||
foreach ($classMetadata->getAttributesMetadata() as $attributeMetadata) {
|
||||
if (isset($this->attributesMetadata[$attributeMetadata->getName()])) {
|
||||
$this->attributesMetadata[$attributeMetadata->getName()]->merge($attributeMetadata);
|
||||
} else {
|
||||
$this->addAttributeMetadata($attributeMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getReflectionClass()
|
||||
{
|
||||
if (!$this->reflClass) {
|
||||
$this->reflClass = new \ReflectionClass($this->getName());
|
||||
}
|
||||
|
||||
return $this->reflClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the names of the properties that should be serialized.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
return array(
|
||||
'name',
|
||||
'attributesMetadata',
|
||||
);
|
||||
}
|
||||
}
|
57
vendor/symfony/serializer/Mapping/ClassMetadataInterface.php
vendored
Normal file
57
vendor/symfony/serializer/Mapping/ClassMetadataInterface.php
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?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\Mapping;
|
||||
|
||||
/**
|
||||
* Stores metadata needed for serializing and deserializing objects of specific class.
|
||||
*
|
||||
* Primarily, the metadata stores the list of attributes to serialize or deserialize.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface ClassMetadataInterface
|
||||
{
|
||||
/**
|
||||
* Returns the name of the backing PHP class.
|
||||
*
|
||||
* @return string The name of the backing class.
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Adds an {@link AttributeMetadataInterface}.
|
||||
*
|
||||
* @param AttributeMetadataInterface $attributeMetadata
|
||||
*/
|
||||
public function addAttributeMetadata(AttributeMetadataInterface $attributeMetadata);
|
||||
|
||||
/**
|
||||
* Gets the list of {@link AttributeMetadataInterface}.
|
||||
*
|
||||
* @return AttributeMetadataInterface[]
|
||||
*/
|
||||
public function getAttributesMetadata();
|
||||
|
||||
/**
|
||||
* Merges a {@link ClassMetadataInterface} in the current one.
|
||||
*
|
||||
* @param ClassMetadataInterface $classMetadata
|
||||
*/
|
||||
public function merge(ClassMetadataInterface $classMetadata);
|
||||
|
||||
/**
|
||||
* Returns a {@link \ReflectionClass} instance for this class.
|
||||
*
|
||||
* @return \ReflectionClass
|
||||
*/
|
||||
public function getReflectionClass();
|
||||
}
|
118
vendor/symfony/serializer/Mapping/Factory/ClassMetadataFactory.php
vendored
Normal file
118
vendor/symfony/serializer/Mapping/Factory/ClassMetadataFactory.php
vendored
Normal file
|
@ -0,0 +1,118 @@
|
|||
<?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\Mapping\Factory;
|
||||
|
||||
use Doctrine\Common\Cache\Cache;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
|
||||
|
||||
/**
|
||||
* Returns a {@link ClassMetadata}.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ClassMetadataFactory implements ClassMetadataFactoryInterface
|
||||
{
|
||||
/**
|
||||
* @var LoaderInterface
|
||||
*/
|
||||
private $loader;
|
||||
/**
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $loadedClasses;
|
||||
|
||||
/**
|
||||
* @param LoaderInterface $loader
|
||||
* @param Cache|null $cache
|
||||
*/
|
||||
public function __construct(LoaderInterface $loader, Cache $cache = null)
|
||||
{
|
||||
$this->loader = $loader;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadataFor($value)
|
||||
{
|
||||
$class = $this->getClass($value);
|
||||
if (!$class) {
|
||||
throw new InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: "%s"', gettype($value)));
|
||||
}
|
||||
|
||||
if (isset($this->loadedClasses[$class])) {
|
||||
return $this->loadedClasses[$class];
|
||||
}
|
||||
|
||||
if ($this->cache && ($this->loadedClasses[$class] = $this->cache->fetch($class))) {
|
||||
return $this->loadedClasses[$class];
|
||||
}
|
||||
|
||||
if (!class_exists($class) && !interface_exists($class)) {
|
||||
throw new InvalidArgumentException(sprintf('The class or interface "%s" does not exist.', $class));
|
||||
}
|
||||
|
||||
$classMetadata = new ClassMetadata($class);
|
||||
$this->loader->loadClassMetadata($classMetadata);
|
||||
|
||||
$reflectionClass = $classMetadata->getReflectionClass();
|
||||
|
||||
// Include metadata from the parent class
|
||||
if ($parent = $reflectionClass->getParentClass()) {
|
||||
$classMetadata->merge($this->getMetadataFor($parent->name));
|
||||
}
|
||||
|
||||
// Include metadata from all implemented interfaces
|
||||
foreach ($reflectionClass->getInterfaces() as $interface) {
|
||||
$classMetadata->merge($this->getMetadataFor($interface->name));
|
||||
}
|
||||
|
||||
if ($this->cache) {
|
||||
$this->cache->save($class, $classMetadata);
|
||||
}
|
||||
|
||||
return $this->loadedClasses[$class] = $classMetadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function hasMetadataFor($value)
|
||||
{
|
||||
$class = $this->getClass($value);
|
||||
|
||||
return class_exists($class) || interface_exists($class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a class name for a given class or instance.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
private function getClass($value)
|
||||
{
|
||||
if (!is_object($value) && !is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ltrim(is_object($value) ? get_class($value) : $value, '\\');
|
||||
}
|
||||
}
|
53
vendor/symfony/serializer/Mapping/Factory/ClassMetadataFactoryInterface.php
vendored
Normal file
53
vendor/symfony/serializer/Mapping/Factory/ClassMetadataFactoryInterface.php
vendored
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?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\Mapping\Factory;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
|
||||
/**
|
||||
* Returns a {@see ClassMetadataInterface}.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface ClassMetadataFactoryInterface
|
||||
{
|
||||
/**
|
||||
* If the method was called with the same class name (or an object of that
|
||||
* class) before, the same metadata instance is returned.
|
||||
*
|
||||
* If the factory was configured with a cache, this method will first look
|
||||
* for an existing metadata instance in the cache. If an existing instance
|
||||
* is found, it will be returned without further ado.
|
||||
*
|
||||
* Otherwise, a new metadata instance is created. If the factory was
|
||||
* configured with a loader, the metadata is passed to the
|
||||
* {@link \Symfony\Component\Serializer\Mapping\Loader\LoaderInterface::loadClassMetadata()} method for further
|
||||
* configuration. At last, the new object is returned.
|
||||
*
|
||||
* @param string|object $value
|
||||
*
|
||||
* @return ClassMetadataInterface
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getMetadataFor($value);
|
||||
|
||||
/**
|
||||
* Checks if class has metadata.
|
||||
*
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMetadataFor($value);
|
||||
}
|
99
vendor/symfony/serializer/Mapping/Loader/AnnotationLoader.php
vendored
Normal file
99
vendor/symfony/serializer/Mapping/Loader/AnnotationLoader.php
vendored
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Doctrine\Common\Annotations\Reader;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Serializer\Exception\MappingException;
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
|
||||
/**
|
||||
* Annotation loader.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class AnnotationLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* @var Reader
|
||||
*/
|
||||
private $reader;
|
||||
|
||||
/**
|
||||
* @param Reader $reader
|
||||
*/
|
||||
public function __construct(Reader $reader)
|
||||
{
|
||||
$this->reader = $reader;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
|
||||
{
|
||||
$reflectionClass = $classMetadata->getReflectionClass();
|
||||
$className = $reflectionClass->name;
|
||||
$loaded = false;
|
||||
|
||||
$attributesMetadata = $classMetadata->getAttributesMetadata();
|
||||
|
||||
foreach ($reflectionClass->getProperties() as $property) {
|
||||
if (!isset($attributeMetadata[$property->name])) {
|
||||
$attributesMetadata[$property->name] = new AttributeMetadata($property->name);
|
||||
$classMetadata->addAttributeMetadata($attributesMetadata[$property->name]);
|
||||
}
|
||||
|
||||
if ($property->getDeclaringClass()->name === $className) {
|
||||
foreach ($this->reader->getPropertyAnnotations($property) as $groups) {
|
||||
if ($groups instanceof Groups) {
|
||||
foreach ($groups->getGroups() as $group) {
|
||||
$attributesMetadata[$property->name]->addGroup($group);
|
||||
}
|
||||
}
|
||||
|
||||
$loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($reflectionClass->getMethods() as $method) {
|
||||
if ($method->getDeclaringClass()->name === $className) {
|
||||
foreach ($this->reader->getMethodAnnotations($method) as $groups) {
|
||||
if ($groups instanceof Groups) {
|
||||
if (preg_match('/^(get|is|has|set)(.+)$/i', $method->name, $matches)) {
|
||||
$attributeName = lcfirst($matches[2]);
|
||||
|
||||
if (isset($attributesMetadata[$attributeName])) {
|
||||
$attributeMetadata = $attributesMetadata[$attributeName];
|
||||
} else {
|
||||
$attributesMetadata[$attributeName] = $attributeMetadata = new AttributeMetadata($attributeName);
|
||||
$classMetadata->addAttributeMetadata($attributeMetadata);
|
||||
}
|
||||
|
||||
foreach ($groups->getGroups() as $group) {
|
||||
$attributeMetadata->addGroup($group);
|
||||
}
|
||||
} else {
|
||||
throw new MappingException(sprintf('Groups on "%s::%s" cannot be added. Groups can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name));
|
||||
}
|
||||
}
|
||||
|
||||
$loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $loaded;
|
||||
}
|
||||
}
|
47
vendor/symfony/serializer/Mapping/Loader/FileLoader.php
vendored
Normal file
47
vendor/symfony/serializer/Mapping/Loader/FileLoader.php
vendored
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\MappingException;
|
||||
|
||||
/**
|
||||
* Base class for all file based loaders.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
abstract class FileLoader implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $file;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $file The mapping file to load
|
||||
*
|
||||
* @throws MappingException if the mapping file does not exist or is not readable
|
||||
*/
|
||||
public function __construct($file)
|
||||
{
|
||||
if (!is_file($file)) {
|
||||
throw new MappingException(sprintf('The mapping file %s does not exist', $file));
|
||||
}
|
||||
|
||||
if (!is_readable($file)) {
|
||||
throw new MappingException(sprintf('The mapping file %s is not readable', $file));
|
||||
}
|
||||
|
||||
$this->file = $file;
|
||||
}
|
||||
}
|
66
vendor/symfony/serializer/Mapping/Loader/LoaderChain.php
vendored
Normal file
66
vendor/symfony/serializer/Mapping/Loader/LoaderChain.php
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\MappingException;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
|
||||
/**
|
||||
* Calls multiple {@link LoaderInterface} instances in a chain.
|
||||
*
|
||||
* This class accepts multiple instances of LoaderInterface to be passed to the
|
||||
* constructor. When {@link loadClassMetadata()} is called, the same method is called
|
||||
* in <em>all</em> of these loaders, regardless of whether any of them was
|
||||
* successful or not.
|
||||
*
|
||||
* @author Bernhard Schussek <bschussek@gmail.com>
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class LoaderChain implements LoaderInterface
|
||||
{
|
||||
/**
|
||||
* @var LoaderInterface[]
|
||||
*/
|
||||
private $loaders;
|
||||
|
||||
/**
|
||||
* Accepts a list of LoaderInterface instances.
|
||||
*
|
||||
* @param LoaderInterface[] $loaders An array of LoaderInterface instances
|
||||
*
|
||||
* @throws MappingException If any of the loaders does not implement LoaderInterface
|
||||
*/
|
||||
public function __construct(array $loaders)
|
||||
{
|
||||
foreach ($loaders as $loader) {
|
||||
if (!$loader instanceof LoaderInterface) {
|
||||
throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', get_class($loader)));
|
||||
}
|
||||
}
|
||||
|
||||
$this->loaders = $loaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadClassMetadata(ClassMetadataInterface $metadata)
|
||||
{
|
||||
$success = false;
|
||||
|
||||
foreach ($this->loaders as $loader) {
|
||||
$success = $loader->loadClassMetadata($metadata) || $success;
|
||||
}
|
||||
|
||||
return $success;
|
||||
}
|
||||
}
|
31
vendor/symfony/serializer/Mapping/Loader/LoaderInterface.php
vendored
Normal file
31
vendor/symfony/serializer/Mapping/Loader/LoaderInterface.php
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
|
||||
/**
|
||||
* Loads {@link ClassMetadataInterface}.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface LoaderInterface
|
||||
{
|
||||
/**
|
||||
* Load class metadata.
|
||||
*
|
||||
* @param ClassMetadataInterface $classMetadata A metadata
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function loadClassMetadata(ClassMetadataInterface $classMetadata);
|
||||
}
|
92
vendor/symfony/serializer/Mapping/Loader/XmlFileLoader.php
vendored
Normal file
92
vendor/symfony/serializer/Mapping/Loader/XmlFileLoader.php
vendored
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Config\Util\XmlUtils;
|
||||
use Symfony\Component\Serializer\Exception\MappingException;
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
|
||||
/**
|
||||
* Loads XML mapping files.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class XmlFileLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* An array of {@class \SimpleXMLElement} instances.
|
||||
*
|
||||
* @var \SimpleXMLElement[]|null
|
||||
*/
|
||||
private $classes;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
|
||||
{
|
||||
if (null === $this->classes) {
|
||||
$this->classes = array();
|
||||
$xml = $this->parseFile($this->file);
|
||||
|
||||
foreach ($xml->class as $class) {
|
||||
$this->classes[(string) $class['name']] = $class;
|
||||
}
|
||||
}
|
||||
|
||||
$attributesMetadata = $classMetadata->getAttributesMetadata();
|
||||
|
||||
if (isset($this->classes[$classMetadata->getName()])) {
|
||||
$xml = $this->classes[$classMetadata->getName()];
|
||||
|
||||
foreach ($xml->attribute as $attribute) {
|
||||
$attributeName = (string) $attribute['name'];
|
||||
|
||||
if (isset($attributesMetadata[$attributeName])) {
|
||||
$attributeMetadata = $attributesMetadata[$attributeName];
|
||||
} else {
|
||||
$attributeMetadata = new AttributeMetadata($attributeName);
|
||||
$classMetadata->addAttributeMetadata($attributeMetadata);
|
||||
}
|
||||
|
||||
foreach ($attribute->group as $group) {
|
||||
$attributeMetadata->addGroup((string) $group);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a XML File.
|
||||
*
|
||||
* @param string $file Path of file
|
||||
*
|
||||
* @return \SimpleXMLElement
|
||||
*
|
||||
* @throws MappingException
|
||||
*/
|
||||
private function parseFile($file)
|
||||
{
|
||||
try {
|
||||
$dom = XmlUtils::loadFile($file, __DIR__.'/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd');
|
||||
} catch (\Exception $e) {
|
||||
throw new MappingException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
return simplexml_import_dom($dom);
|
||||
}
|
||||
}
|
89
vendor/symfony/serializer/Mapping/Loader/YamlFileLoader.php
vendored
Normal file
89
vendor/symfony/serializer/Mapping/Loader/YamlFileLoader.php
vendored
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?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\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\MappingException;
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
|
||||
/**
|
||||
* YAML File Loader.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class YamlFileLoader extends FileLoader
|
||||
{
|
||||
private $yamlParser;
|
||||
|
||||
/**
|
||||
* An array of YAML class descriptions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $classes = null;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
|
||||
{
|
||||
if (null === $this->classes) {
|
||||
if (!stream_is_local($this->file)) {
|
||||
throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
|
||||
}
|
||||
|
||||
if (null === $this->yamlParser) {
|
||||
$this->yamlParser = new Parser();
|
||||
}
|
||||
|
||||
$classes = $this->yamlParser->parse(file_get_contents($this->file));
|
||||
|
||||
if (empty($classes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// not an array
|
||||
if (!is_array($classes)) {
|
||||
throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
|
||||
}
|
||||
|
||||
$this->classes = $classes;
|
||||
}
|
||||
|
||||
if (isset($this->classes[$classMetadata->getName()])) {
|
||||
$yaml = $this->classes[$classMetadata->getName()];
|
||||
|
||||
if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
|
||||
$attributesMetadata = $classMetadata->getAttributesMetadata();
|
||||
foreach ($yaml['attributes'] as $attribute => $data) {
|
||||
if (isset($attributesMetadata[$attribute])) {
|
||||
$attributeMetadata = $attributesMetadata[$attribute];
|
||||
} else {
|
||||
$attributeMetadata = new AttributeMetadata($attribute);
|
||||
$classMetadata->addAttributeMetadata($attributeMetadata);
|
||||
}
|
||||
|
||||
if (isset($data['groups'])) {
|
||||
foreach ($data['groups'] as $group) {
|
||||
$attributeMetadata->addGroup($group);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
<?xml version="1.0" ?>
|
||||
|
||||
<xsd:schema xmlns="http://symfony.com/schema/dic/serializer-mapping"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://symfony.com/schema/dic/serializer-mapping"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Symfony Serializer Mapping Schema, version 1.0
|
||||
Authors: Kévin Dunglas
|
||||
|
||||
A serializer mapping connects attributes with serialization groups.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
|
||||
<xsd:element name="serializer" type="serializer" />
|
||||
|
||||
<xsd:complexType name="serializer">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The root element of the serializer mapping definition.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:choice minOccurs="0" maxOccurs="unbounded">
|
||||
<xsd:element name="class" type="class" />
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="class">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Contains serialization groups for a single class.
|
||||
|
||||
Nested elements may be class property and/or getter definitions.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:choice minOccurs="0" maxOccurs="unbounded">
|
||||
<xsd:element name="attribute" type="attribute" minOccurs="0" maxOccurs="unbounded" />
|
||||
</xsd:choice>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="attribute">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Contains serialization groups for a attributes. The name of the attribute should be given in the "name" option.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="group" type="xsd:string" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
|
||||
</xsd:schema>
|
82
vendor/symfony/serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.php
vendored
Normal file
82
vendor/symfony/serializer/NameConverter/CamelCaseToSnakeCaseNameConverter.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\NameConverter;
|
||||
|
||||
/**
|
||||
* CamelCase to Underscore name converter.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
|
||||
{
|
||||
/**
|
||||
* @var array|null
|
||||
*/
|
||||
private $attributes;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $lowerCamelCase;
|
||||
|
||||
/**
|
||||
* @param null|array $attributes The list of attributes to rename or null for all attributes.
|
||||
* @param bool $lowerCamelCase Use lowerCamelCase style.
|
||||
*/
|
||||
public function __construct(array $attributes = null, $lowerCamelCase = true)
|
||||
{
|
||||
$this->attributes = $attributes;
|
||||
$this->lowerCamelCase = $lowerCamelCase;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($propertyName)
|
||||
{
|
||||
if (null === $this->attributes || in_array($propertyName, $this->attributes)) {
|
||||
$snakeCasedName = '';
|
||||
|
||||
$len = strlen($propertyName);
|
||||
for ($i = 0; $i < $len; ++$i) {
|
||||
if (ctype_upper($propertyName[$i])) {
|
||||
$snakeCasedName .= '_'.strtolower($propertyName[$i]);
|
||||
} else {
|
||||
$snakeCasedName .= strtolower($propertyName[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
return $snakeCasedName;
|
||||
}
|
||||
|
||||
return $propertyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($propertyName)
|
||||
{
|
||||
$camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
|
||||
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
|
||||
}, $propertyName);
|
||||
|
||||
if ($this->lowerCamelCase) {
|
||||
$camelCasedName = lcfirst($camelCasedName);
|
||||
}
|
||||
|
||||
if (null === $this->attributes || in_array($camelCasedName, $this->attributes)) {
|
||||
return $this->lowerCamelCase ? lcfirst($camelCasedName) : $camelCasedName;
|
||||
}
|
||||
|
||||
return $propertyName;
|
||||
}
|
||||
}
|
38
vendor/symfony/serializer/NameConverter/NameConverterInterface.php
vendored
Normal file
38
vendor/symfony/serializer/NameConverter/NameConverterInterface.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\NameConverter;
|
||||
|
||||
/**
|
||||
* Defines the interface for property name converters.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface NameConverterInterface
|
||||
{
|
||||
/**
|
||||
* Converts a property name to its normalized value.
|
||||
*
|
||||
* @param string $propertyName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function normalize($propertyName);
|
||||
|
||||
/**
|
||||
* Converts a property name to its denormalized value.
|
||||
*
|
||||
* @param string $propertyName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function denormalize($propertyName);
|
||||
}
|
345
vendor/symfony/serializer/Normalizer/AbstractNormalizer.php
vendored
Normal file
345
vendor/symfony/serializer/Normalizer/AbstractNormalizer.php
vendored
Normal file
|
@ -0,0 +1,345 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
|
||||
|
||||
/**
|
||||
* Normalizer implementation.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
abstract class AbstractNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $circularReferenceLimit = 1;
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
protected $circularReferenceHandler;
|
||||
/**
|
||||
* @var ClassMetadataFactoryInterface|null
|
||||
*/
|
||||
protected $classMetadataFactory;
|
||||
/**
|
||||
* @var NameConverterInterface|null
|
||||
*/
|
||||
protected $nameConverter;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $callbacks = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $ignoredAttributes = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $camelizedAttributes = array();
|
||||
|
||||
/**
|
||||
* Sets the {@link ClassMetadataFactoryInterface} to use.
|
||||
*
|
||||
* @param ClassMetadataFactoryInterface|null $classMetadataFactory
|
||||
* @param NameConverterInterface|null $nameConverter
|
||||
*/
|
||||
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null)
|
||||
{
|
||||
$this->classMetadataFactory = $classMetadataFactory;
|
||||
$this->nameConverter = $nameConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set circular reference limit.
|
||||
*
|
||||
* @param int $circularReferenceLimit limit of iterations for the same object
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setCircularReferenceLimit($circularReferenceLimit)
|
||||
{
|
||||
$this->circularReferenceLimit = $circularReferenceLimit;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set circular reference handler.
|
||||
*
|
||||
* @param callable $circularReferenceHandler
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function setCircularReferenceHandler($circularReferenceHandler)
|
||||
{
|
||||
if (!is_callable($circularReferenceHandler)) {
|
||||
throw new InvalidArgumentException('The given circular reference handler is not callable.');
|
||||
}
|
||||
|
||||
$this->circularReferenceHandler = $circularReferenceHandler;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set normalization callbacks.
|
||||
*
|
||||
* @param array $callbacks help normalize the result
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
* @throws InvalidArgumentException if a non-callable callback is set
|
||||
*/
|
||||
public function setCallbacks(array $callbacks)
|
||||
{
|
||||
foreach ($callbacks as $attribute => $callback) {
|
||||
if (!is_callable($callback)) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'The given callback for attribute "%s" is not callable.',
|
||||
$attribute
|
||||
));
|
||||
}
|
||||
}
|
||||
$this->callbacks = $callbacks;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ignored attributes for normalization and denormalization.
|
||||
*
|
||||
* @param array $ignoredAttributes
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setIgnoredAttributes(array $ignoredAttributes)
|
||||
{
|
||||
$this->ignoredAttributes = $ignoredAttributes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attributes to be camelized on denormalize.
|
||||
*
|
||||
* @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
|
||||
*
|
||||
* @param array $camelizedAttributes
|
||||
*
|
||||
* @return self
|
||||
*
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function setCamelizedAttributes(array $camelizedAttributes)
|
||||
{
|
||||
@trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) {
|
||||
throw new LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__));
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
foreach ($camelizedAttributes as $camelizedAttribute) {
|
||||
$attributes[] = lcfirst(preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
|
||||
return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
|
||||
}, $camelizedAttribute));
|
||||
}
|
||||
|
||||
$this->nameConverter = new CamelCaseToSnakeCaseNameConverter($attributes);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects if the configured circular reference limit is reached.
|
||||
*
|
||||
* @param object $object
|
||||
* @param array $context
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws CircularReferenceException
|
||||
*/
|
||||
protected function isCircularReference($object, &$context)
|
||||
{
|
||||
$objectHash = spl_object_hash($object);
|
||||
|
||||
if (isset($context['circular_reference_limit'][$objectHash])) {
|
||||
if ($context['circular_reference_limit'][$objectHash] >= $this->circularReferenceLimit) {
|
||||
unset($context['circular_reference_limit'][$objectHash]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
++$context['circular_reference_limit'][$objectHash];
|
||||
} else {
|
||||
$context['circular_reference_limit'][$objectHash] = 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a circular reference.
|
||||
*
|
||||
* If a circular reference handler is set, it will be called. Otherwise, a
|
||||
* {@class CircularReferenceException} will be thrown.
|
||||
*
|
||||
* @param object $object
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws CircularReferenceException
|
||||
*/
|
||||
protected function handleCircularReference($object)
|
||||
{
|
||||
if ($this->circularReferenceHandler) {
|
||||
return call_user_func($this->circularReferenceHandler, $object);
|
||||
}
|
||||
|
||||
throw new CircularReferenceException(sprintf('A circular reference has been detected (configured limit: %d).', $this->circularReferenceLimit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an attribute name, for example to convert a snake_case name to camelCase.
|
||||
*
|
||||
* @deprecated Deprecated since version 2.7, to be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.
|
||||
*
|
||||
* @param string $attributeName
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function formatAttribute($attributeName)
|
||||
{
|
||||
@trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
|
||||
|
||||
return $this->nameConverter ? $this->nameConverter->normalize($attributeName) : $attributeName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets attributes to normalize using groups.
|
||||
*
|
||||
* @param string|object $classOrObject
|
||||
* @param array $context
|
||||
* @param bool $attributesAsString If false, return an array of {@link AttributeMetadataInterface}
|
||||
*
|
||||
* @return string[]|AttributeMetadataInterface[]|bool
|
||||
*/
|
||||
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
|
||||
{
|
||||
if (!$this->classMetadataFactory || !isset($context['groups']) || !is_array($context['groups'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$allowedAttributes = array();
|
||||
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
|
||||
if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
|
||||
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($allowedAttributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes the given data to an array. It's particularly useful during
|
||||
* the denormalization process.
|
||||
*
|
||||
* @param object|array $data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareForDenormalization($data)
|
||||
{
|
||||
return (array) $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates an object using constructor parameters when needed.
|
||||
*
|
||||
* This method also allows to denormalize data into an existing object if
|
||||
* it is present in the context with the object_to_populate key.
|
||||
*
|
||||
* @param array $data
|
||||
* @param string $class
|
||||
* @param array $context
|
||||
* @param \ReflectionClass $reflectionClass
|
||||
* @param array|bool $allowedAttributes
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function instantiateObject(array &$data, $class, array &$context, \ReflectionClass $reflectionClass, $allowedAttributes)
|
||||
{
|
||||
if (
|
||||
isset($context['object_to_populate']) &&
|
||||
is_object($context['object_to_populate']) &&
|
||||
$class === get_class($context['object_to_populate'])
|
||||
) {
|
||||
return $context['object_to_populate'];
|
||||
}
|
||||
|
||||
$constructor = $reflectionClass->getConstructor();
|
||||
if ($constructor) {
|
||||
$constructorParameters = $constructor->getParameters();
|
||||
|
||||
$params = array();
|
||||
foreach ($constructorParameters as $constructorParameter) {
|
||||
$paramName = $constructorParameter->name;
|
||||
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
|
||||
|
||||
$allowed = $allowedAttributes === false || in_array($paramName, $allowedAttributes);
|
||||
$ignored = in_array($paramName, $this->ignoredAttributes);
|
||||
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
|
||||
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
||||
if (!is_array($data[$paramName])) {
|
||||
throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
|
||||
}
|
||||
|
||||
$params = array_merge($params, $data[$paramName]);
|
||||
}
|
||||
} elseif ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
|
||||
$params[] = $data[$key];
|
||||
// don't run set for a parameter passed to the constructor
|
||||
unset($data[$key]);
|
||||
} elseif ($constructorParameter->isDefaultValueAvailable()) {
|
||||
$params[] = $constructorParameter->getDefaultValue();
|
||||
} else {
|
||||
throw new RuntimeException(
|
||||
sprintf(
|
||||
'Cannot create an instance of %s from serialized data because its constructor requires parameter "%s" to be present.',
|
||||
$class,
|
||||
$constructorParameter->name
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $reflectionClass->newInstanceArgs($params);
|
||||
}
|
||||
|
||||
return new $class();
|
||||
}
|
||||
}
|
66
vendor/symfony/serializer/Normalizer/CustomNormalizer.php
vendored
Normal file
66
vendor/symfony/serializer/Normalizer/CustomNormalizer.php
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
class CustomNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = array())
|
||||
{
|
||||
return $object->normalize($this->serializer, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = null, array $context = array())
|
||||
{
|
||||
$object = new $class();
|
||||
$object->denormalize($this->serializer, $data, $format, $context);
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given class implements the NormalizableInterface.
|
||||
*
|
||||
* @param mixed $data Data to normalize.
|
||||
* @param string $format The format being (de-)serialized from or into.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return $data instanceof NormalizableInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given class implements the NormalizableInterface.
|
||||
*
|
||||
* @param mixed $data Data to denormalize from.
|
||||
* @param string $type The class to which the data should be denormalized.
|
||||
* @param string $format The format being deserialized from.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
$class = new \ReflectionClass($type);
|
||||
|
||||
return $class->isSubclassOf('Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
|
||||
}
|
||||
}
|
38
vendor/symfony/serializer/Normalizer/DenormalizableInterface.php
vendored
Normal file
38
vendor/symfony/serializer/Normalizer/DenormalizableInterface.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\Normalizer;
|
||||
|
||||
/**
|
||||
* Defines the most basic interface a class must implement to be denormalizable.
|
||||
*
|
||||
* If a denormalizer is registered for the class and it doesn't implement
|
||||
* the Denormalizable interfaces, the normalizer will be used instead
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface DenormalizableInterface
|
||||
{
|
||||
/**
|
||||
* Denormalizes the object back from an array of scalars|arrays.
|
||||
*
|
||||
* It is important to understand that the denormalize() call should denormalize
|
||||
* recursively all child objects of the implementor.
|
||||
*
|
||||
* @param DenormalizerInterface $denormalizer The denormalizer is given so that you
|
||||
* can use it to denormalize objects contained within this object
|
||||
* @param array|scalar $data The data from which to re-create the object.
|
||||
* @param string|null $format The format is optionally given to be able to denormalize differently
|
||||
* based on different input formats
|
||||
* @param array $context options for denormalizing
|
||||
*/
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array());
|
||||
}
|
43
vendor/symfony/serializer/Normalizer/DenormalizerInterface.php
vendored
Normal file
43
vendor/symfony/serializer/Normalizer/DenormalizerInterface.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\Normalizer;
|
||||
|
||||
/**
|
||||
* Defines the interface of denormalizers.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface DenormalizerInterface
|
||||
{
|
||||
/**
|
||||
* Denormalizes data back into an object of the given class.
|
||||
*
|
||||
* @param mixed $data data to restore
|
||||
* @param string $class the expected class to instantiate
|
||||
* @param string $format format the given data was extracted from
|
||||
* @param array $context options available to the denormalizer
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function denormalize($data, $class, $format = null, array $context = array());
|
||||
|
||||
/**
|
||||
* Checks whether the given class is supported for denormalization by this normalizer.
|
||||
*
|
||||
* @param mixed $data Data to denormalize from.
|
||||
* @param string $type The class to which the data should be denormalized.
|
||||
* @param string $format The format being deserialized from.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null);
|
||||
}
|
179
vendor/symfony/serializer/Normalizer/GetSetMethodNormalizer.php
vendored
Normal file
179
vendor/symfony/serializer/Normalizer/GetSetMethodNormalizer.php
vendored
Normal file
|
@ -0,0 +1,179 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Converts between objects with getter and setter methods and arrays.
|
||||
*
|
||||
* The normalization process looks at all public methods and calls the ones
|
||||
* which have a name starting with get and take no parameters. The result is a
|
||||
* map from property names (method name stripped of the get prefix and converted
|
||||
* to lower case) to property values. Property values are normalized through the
|
||||
* serializer.
|
||||
*
|
||||
* The denormalization first looks at the constructor of the given class to see
|
||||
* if any of the parameters have the same name as one of the properties. The
|
||||
* constructor is then called with all parameters or an exception is thrown if
|
||||
* any required parameters were not present as properties. Then the denormalizer
|
||||
* walks through the given map of property names to property values to see if a
|
||||
* setter method exists for any of the properties. If a setter exists it is
|
||||
* called with the property value. No automatic denormalization of the value
|
||||
* takes place.
|
||||
*
|
||||
* @author Nils Adermann <naderman@naderman.de>
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GetSetMethodNormalizer extends AbstractNormalizer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws LogicException
|
||||
* @throws CircularReferenceException
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = array())
|
||||
{
|
||||
if ($this->isCircularReference($object, $context)) {
|
||||
return $this->handleCircularReference($object);
|
||||
}
|
||||
|
||||
$reflectionObject = new \ReflectionObject($object);
|
||||
$reflectionMethods = $reflectionObject->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
|
||||
|
||||
$attributes = array();
|
||||
foreach ($reflectionMethods as $method) {
|
||||
if ($this->isGetMethod($method)) {
|
||||
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
|
||||
if (in_array($attributeName, $this->ignoredAttributes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false !== $allowedAttributes && !in_array($attributeName, $allowedAttributes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributeValue = $method->invoke($object);
|
||||
if (isset($this->callbacks[$attributeName])) {
|
||||
$attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
|
||||
}
|
||||
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
||||
if (!$this->serializer instanceof NormalizerInterface) {
|
||||
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
|
||||
}
|
||||
|
||||
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
|
||||
}
|
||||
|
||||
if ($this->nameConverter) {
|
||||
$attributeName = $this->nameConverter->normalize($attributeName);
|
||||
}
|
||||
|
||||
$attributes[$attributeName] = $attributeValue;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function denormalize($data, $class, $format = null, array $context = array())
|
||||
{
|
||||
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
|
||||
$normalizedData = $this->prepareForDenormalization($data);
|
||||
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
|
||||
|
||||
$classMethods = get_class_methods($object);
|
||||
foreach ($normalizedData as $attribute => $value) {
|
||||
if ($this->nameConverter) {
|
||||
$attribute = $this->nameConverter->denormalize($attribute);
|
||||
}
|
||||
|
||||
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
|
||||
$ignored = in_array($attribute, $this->ignoredAttributes);
|
||||
|
||||
if ($allowed && !$ignored) {
|
||||
$setter = 'set'.ucfirst($attribute);
|
||||
|
||||
if (in_array($setter, $classMethods)) {
|
||||
$object->$setter($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
return $this->supports($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given class has any get{Property} method.
|
||||
*
|
||||
* @param string $class
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function supports($class)
|
||||
{
|
||||
$class = new \ReflectionClass($class);
|
||||
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||
foreach ($methods as $method) {
|
||||
if ($this->isGetMethod($method)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a method's name is get.* or is.*, and can be called without parameters.
|
||||
*
|
||||
* @param \ReflectionMethod $method the method to check
|
||||
*
|
||||
* @return bool whether the method is a getter or boolean getter.
|
||||
*/
|
||||
private function isGetMethod(\ReflectionMethod $method)
|
||||
{
|
||||
$methodLength = strlen($method->name);
|
||||
|
||||
return (
|
||||
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
|
||||
(0 === strpos($method->name, 'is') && 2 < $methodLength)) &&
|
||||
0 === $method->getNumberOfRequiredParameters()
|
||||
);
|
||||
}
|
||||
}
|
39
vendor/symfony/serializer/Normalizer/NormalizableInterface.php
vendored
Normal file
39
vendor/symfony/serializer/Normalizer/NormalizableInterface.php
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
/**
|
||||
* Defines the most basic interface a class must implement to be normalizable.
|
||||
*
|
||||
* If a normalizer is registered for the class and it doesn't implement
|
||||
* the Normalizable interfaces, the normalizer will be used instead.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface NormalizableInterface
|
||||
{
|
||||
/**
|
||||
* Normalizes the object into an array of scalars|arrays.
|
||||
*
|
||||
* It is important to understand that the normalize() call should normalize
|
||||
* recursively all child objects of the implementor.
|
||||
*
|
||||
* @param NormalizerInterface $normalizer The normalizer is given so that you
|
||||
* can use it to normalize objects contained within this object.
|
||||
* @param string|null $format The format is optionally given to be able to normalize differently
|
||||
* based on different output formats.
|
||||
* @param array $context Options for normalizing this object
|
||||
*
|
||||
* @return array|string|bool|int|float|null
|
||||
*/
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array());
|
||||
}
|
41
vendor/symfony/serializer/Normalizer/NormalizerInterface.php
vendored
Normal file
41
vendor/symfony/serializer/Normalizer/NormalizerInterface.php
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
/**
|
||||
* Defines the interface of normalizers.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface NormalizerInterface
|
||||
{
|
||||
/**
|
||||
* Normalizes an object into a set of arrays/scalars.
|
||||
*
|
||||
* @param object $object object to normalize
|
||||
* @param string $format format the normalization result will be encoded as
|
||||
* @param array $context Context options for the normalizer
|
||||
*
|
||||
* @return array|string|bool|int|float|null
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = array());
|
||||
|
||||
/**
|
||||
* Checks whether the given class is supported for normalization by this normalizer.
|
||||
*
|
||||
* @param mixed $data Data to normalize.
|
||||
* @param string $format The format being (de-)serialized from or into.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null);
|
||||
}
|
162
vendor/symfony/serializer/Normalizer/ObjectNormalizer.php
vendored
Normal file
162
vendor/symfony/serializer/Normalizer/ObjectNormalizer.php
vendored
Normal file
|
@ -0,0 +1,162 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
|
||||
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
|
||||
|
||||
/**
|
||||
* Converts between objects and arrays using the PropertyAccess component.
|
||||
*
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ObjectNormalizer extends AbstractNormalizer
|
||||
{
|
||||
/**
|
||||
* @var PropertyAccessorInterface
|
||||
*/
|
||||
protected $propertyAccessor;
|
||||
|
||||
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null)
|
||||
{
|
||||
parent::__construct($classMetadataFactory, $nameConverter);
|
||||
|
||||
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return is_object($data) && !$data instanceof \Traversable;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws CircularReferenceException
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = array())
|
||||
{
|
||||
if ($this->isCircularReference($object, $context)) {
|
||||
return $this->handleCircularReference($object);
|
||||
}
|
||||
|
||||
$data = array();
|
||||
$attributes = $this->getAllowedAttributes($object, $context, true);
|
||||
|
||||
// If not using groups, detect manually
|
||||
if (false === $attributes) {
|
||||
$attributes = array();
|
||||
|
||||
// methods
|
||||
$reflClass = new \ReflectionClass($object);
|
||||
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
|
||||
if (
|
||||
!$reflMethod->isConstructor() &&
|
||||
!$reflMethod->isDestructor() &&
|
||||
0 === $reflMethod->getNumberOfRequiredParameters()
|
||||
) {
|
||||
$name = $reflMethod->getName();
|
||||
|
||||
if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {
|
||||
// getters and hassers
|
||||
$attributes[lcfirst(substr($name, 3))] = true;
|
||||
} elseif (strpos($name, 'is') === 0) {
|
||||
// issers
|
||||
$attributes[lcfirst(substr($name, 2))] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// properties
|
||||
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
|
||||
$attributes[$reflProperty->getName()] = true;
|
||||
}
|
||||
|
||||
$attributes = array_keys($attributes);
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute) {
|
||||
if (in_array($attribute, $this->ignoredAttributes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
|
||||
|
||||
if (isset($this->callbacks[$attribute])) {
|
||||
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
|
||||
}
|
||||
|
||||
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
||||
if (!$this->serializer instanceof NormalizerInterface) {
|
||||
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attribute));
|
||||
}
|
||||
|
||||
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
|
||||
}
|
||||
|
||||
if ($this->nameConverter) {
|
||||
$attribute = $this->nameConverter->normalize($attribute);
|
||||
}
|
||||
|
||||
$data[$attribute] = $attributeValue;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
return class_exists($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = null, array $context = array())
|
||||
{
|
||||
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
|
||||
$normalizedData = $this->prepareForDenormalization($data);
|
||||
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes);
|
||||
|
||||
foreach ($normalizedData as $attribute => $value) {
|
||||
if ($this->nameConverter) {
|
||||
$attribute = $this->nameConverter->denormalize($attribute);
|
||||
}
|
||||
|
||||
$allowed = $allowedAttributes === false || in_array($attribute, $allowedAttributes);
|
||||
$ignored = in_array($attribute, $this->ignoredAttributes);
|
||||
|
||||
if ($allowed && !$ignored) {
|
||||
try {
|
||||
$this->propertyAccessor->setValue($object, $attribute, $value);
|
||||
} catch (NoSuchPropertyException $exception) {
|
||||
// Properties not found are ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
161
vendor/symfony/serializer/Normalizer/PropertyNormalizer.php
vendored
Normal file
161
vendor/symfony/serializer/Normalizer/PropertyNormalizer.php
vendored
Normal file
|
@ -0,0 +1,161 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\CircularReferenceException;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Converts between objects and arrays by mapping properties.
|
||||
*
|
||||
* The normalization process looks for all the object's properties (public and private).
|
||||
* The result is a map from property names to property values. Property values
|
||||
* are normalized through the serializer.
|
||||
*
|
||||
* The denormalization first looks at the constructor of the given class to see
|
||||
* if any of the parameters have the same name as one of the properties. The
|
||||
* constructor is then called with all parameters or an exception is thrown if
|
||||
* any required parameters were not present as properties. Then the denormalizer
|
||||
* walks through the given map of property names to property values to see if a
|
||||
* property with the corresponding name exists. If found, the property gets the value.
|
||||
*
|
||||
* @author Matthieu Napoli <matthieu@mnapoli.fr>
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertyNormalizer extends AbstractNormalizer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws CircularReferenceException
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = array())
|
||||
{
|
||||
if ($this->isCircularReference($object, $context)) {
|
||||
return $this->handleCircularReference($object);
|
||||
}
|
||||
|
||||
$reflectionObject = new \ReflectionObject($object);
|
||||
$attributes = array();
|
||||
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
|
||||
|
||||
foreach ($reflectionObject->getProperties() as $property) {
|
||||
if (in_array($property->name, $this->ignoredAttributes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Override visibility
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(true);
|
||||
}
|
||||
|
||||
$attributeValue = $property->getValue($object);
|
||||
|
||||
if (isset($this->callbacks[$property->name])) {
|
||||
$attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue);
|
||||
}
|
||||
if (null !== $attributeValue && !is_scalar($attributeValue)) {
|
||||
if (!$this->serializer instanceof NormalizerInterface) {
|
||||
throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $property->name));
|
||||
}
|
||||
|
||||
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
|
||||
}
|
||||
|
||||
$propertyName = $property->name;
|
||||
if ($this->nameConverter) {
|
||||
$propertyName = $this->nameConverter->normalize($propertyName);
|
||||
}
|
||||
|
||||
$attributes[$propertyName] = $attributeValue;
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function denormalize($data, $class, $format = null, array $context = array())
|
||||
{
|
||||
$allowedAttributes = $this->getAllowedAttributes($class, $context, true);
|
||||
$data = $this->prepareForDenormalization($data);
|
||||
|
||||
$reflectionClass = new \ReflectionClass($class);
|
||||
$object = $this->instantiateObject($data, $class, $context, $reflectionClass, $allowedAttributes);
|
||||
|
||||
foreach ($data as $propertyName => $value) {
|
||||
if ($this->nameConverter) {
|
||||
$propertyName = $this->nameConverter->denormalize($propertyName);
|
||||
}
|
||||
|
||||
$allowed = $allowedAttributes === false || in_array($propertyName, $allowedAttributes);
|
||||
$ignored = in_array($propertyName, $this->ignoredAttributes);
|
||||
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
|
||||
$property = $reflectionClass->getProperty($propertyName);
|
||||
|
||||
// Override visibility
|
||||
if (!$property->isPublic()) {
|
||||
$property->setAccessible(true);
|
||||
}
|
||||
|
||||
$property->setValue($object, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
return $this->supports($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given class has any non-static property.
|
||||
*
|
||||
* @param string $class
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function supports($class)
|
||||
{
|
||||
$class = new \ReflectionClass($class);
|
||||
|
||||
// We look for at least one non-static property
|
||||
foreach ($class->getProperties() as $property) {
|
||||
if (!$property->isStatic()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
36
vendor/symfony/serializer/Normalizer/SerializerAwareNormalizer.php
vendored
Normal file
36
vendor/symfony/serializer/Normalizer/SerializerAwareNormalizer.php
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?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\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||
|
||||
/**
|
||||
* SerializerAware Normalizer implementation.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
abstract class SerializerAwareNormalizer implements SerializerAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setSerializer(SerializerInterface $serializer)
|
||||
{
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
}
|
15
vendor/symfony/serializer/README.md
vendored
Normal file
15
vendor/symfony/serializer/README.md
vendored
Normal file
|
@ -0,0 +1,15 @@
|
|||
Serializer Component
|
||||
====================
|
||||
|
||||
With the Serializer component it's possible to handle serializing data structures,
|
||||
including object graphs, into array structures or other formats like XML and JSON.
|
||||
It can also handle deserializing XML and JSON back to object graphs.
|
||||
|
||||
Resources
|
||||
---------
|
||||
|
||||
You can run the unit tests with the following command:
|
||||
|
||||
$ cd path/to/Symfony/Component/Serializer/
|
||||
$ composer install
|
||||
$ phpunit
|
310
vendor/symfony/serializer/Serializer.php
vendored
Normal file
310
vendor/symfony/serializer/Serializer.php
vendored
Normal file
|
@ -0,0 +1,310 @@
|
|||
<?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;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\ChainDecoder;
|
||||
use Symfony\Component\Serializer\Encoder\ChainEncoder;
|
||||
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
||||
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
use Symfony\Component\Serializer\Exception\LogicException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Serializer serializes and deserializes data.
|
||||
*
|
||||
* objects are turned into arrays by normalizers.
|
||||
* arrays are turned into various output formats by encoders.
|
||||
*
|
||||
* $serializer->serialize($obj, 'xml')
|
||||
* $serializer->decode($data, 'xml')
|
||||
* $serializer->denormalize($data, 'Class', 'xml')
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface
|
||||
{
|
||||
/**
|
||||
* @var Encoder\ChainEncoder
|
||||
*/
|
||||
protected $encoder;
|
||||
/**
|
||||
* @var Encoder\ChainDecoder
|
||||
*/
|
||||
protected $decoder;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $normalizers = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $normalizerCache = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $denormalizerCache = array();
|
||||
|
||||
public function __construct(array $normalizers = array(), array $encoders = array())
|
||||
{
|
||||
foreach ($normalizers as $normalizer) {
|
||||
if ($normalizer instanceof SerializerAwareInterface) {
|
||||
$normalizer->setSerializer($this);
|
||||
}
|
||||
}
|
||||
$this->normalizers = $normalizers;
|
||||
|
||||
$decoders = array();
|
||||
$realEncoders = array();
|
||||
foreach ($encoders as $encoder) {
|
||||
if ($encoder instanceof SerializerAwareInterface) {
|
||||
$encoder->setSerializer($this);
|
||||
}
|
||||
if ($encoder instanceof DecoderInterface) {
|
||||
$decoders[] = $encoder;
|
||||
}
|
||||
if ($encoder instanceof EncoderInterface) {
|
||||
$realEncoders[] = $encoder;
|
||||
}
|
||||
}
|
||||
$this->encoder = new ChainEncoder($realEncoders);
|
||||
$this->decoder = new ChainDecoder($decoders);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
final public function serialize($data, $format, array $context = array())
|
||||
{
|
||||
if (!$this->supportsEncoding($format)) {
|
||||
throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
|
||||
}
|
||||
|
||||
if ($this->encoder->needsNormalization($format)) {
|
||||
$data = $this->normalize($data, $format, $context);
|
||||
}
|
||||
|
||||
return $this->encode($data, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
final public function deserialize($data, $type, $format, array $context = array())
|
||||
{
|
||||
if (!$this->supportsDecoding($format)) {
|
||||
throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
|
||||
}
|
||||
|
||||
$data = $this->decode($data, $format, $context);
|
||||
|
||||
return $this->denormalize($data, $type, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($data, $format = null, array $context = array())
|
||||
{
|
||||
// If a normalizer supports the given data, use it
|
||||
if ($normalizer = $this->getNormalizer($data, $format)) {
|
||||
return $normalizer->normalize($data, $format, $context);
|
||||
}
|
||||
|
||||
if (null === $data || is_scalar($data)) {
|
||||
return $data;
|
||||
}
|
||||
if (is_object($data) && $this->supportsNormalization($data, $format)) {
|
||||
return $this->normalizeObject($data, $format, $context);
|
||||
}
|
||||
if ($data instanceof \Traversable) {
|
||||
$normalized = array();
|
||||
foreach ($data as $key => $val) {
|
||||
$normalized[$key] = $this->normalize($val, $format, $context);
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
if (is_object($data)) {
|
||||
return $this->normalizeObject($data, $format, $context);
|
||||
}
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $key => $val) {
|
||||
$data[$key] = $this->normalize($val, $format, $context);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $type, $format = null, array $context = array())
|
||||
{
|
||||
return $this->denormalizeObject($data, $type, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return null !== $this->getNormalizer($data, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
return null !== $this->getDenormalizer($data, $type, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a matching normalizer.
|
||||
*
|
||||
* @param mixed $data Data to get the serializer for
|
||||
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||
*
|
||||
* @return NormalizerInterface|null
|
||||
*/
|
||||
private function getNormalizer($data, $format)
|
||||
{
|
||||
if ($isObject = is_object($data)) {
|
||||
$class = get_class($data);
|
||||
if (isset($this->normalizerCache[$class][$format])) {
|
||||
return $this->normalizerCache[$class][$format];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->normalizers as $normalizer) {
|
||||
if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format)) {
|
||||
if ($isObject) {
|
||||
$this->normalizerCache[$class][$format] = $normalizer;
|
||||
}
|
||||
|
||||
return $normalizer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a matching denormalizer.
|
||||
*
|
||||
* @param mixed $data data to restore
|
||||
* @param string $class the expected class to instantiate
|
||||
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||
*
|
||||
* @return DenormalizerInterface|null
|
||||
*/
|
||||
private function getDenormalizer($data, $class, $format)
|
||||
{
|
||||
if (isset($this->denormalizerCache[$class][$format])) {
|
||||
return $this->denormalizerCache[$class][$format];
|
||||
}
|
||||
|
||||
foreach ($this->normalizers as $normalizer) {
|
||||
if ($normalizer instanceof DenormalizerInterface && $normalizer->supportsDenormalization($data, $class, $format)) {
|
||||
$this->denormalizerCache[$class][$format] = $normalizer;
|
||||
|
||||
return $normalizer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
final public function encode($data, $format, array $context = array())
|
||||
{
|
||||
return $this->encoder->encode($data, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
final public function decode($data, $format, array $context = array())
|
||||
{
|
||||
return $this->decoder->decode($data, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes an object into a set of arrays/scalars.
|
||||
*
|
||||
* @param object $object object to normalize
|
||||
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||
* @param array $context The context data for this particular normalization
|
||||
*
|
||||
* @return array|string|bool|int|float|null
|
||||
*
|
||||
* @throws LogicException
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
private function normalizeObject($object, $format, array $context = array())
|
||||
{
|
||||
if (!$this->normalizers) {
|
||||
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
|
||||
}
|
||||
|
||||
if ($normalizer = $this->getNormalizer($object, $format)) {
|
||||
return $normalizer->normalize($object, $format, $context);
|
||||
}
|
||||
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($object)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Denormalizes data back into an object of the given class.
|
||||
*
|
||||
* @param mixed $data data to restore
|
||||
* @param string $class the expected class to instantiate
|
||||
* @param string $format format name, present to give the option to normalizers to act differently based on formats
|
||||
* @param array $context The context data for this particular denormalization
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @throws LogicException
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
private function denormalizeObject($data, $class, $format, array $context = array())
|
||||
{
|
||||
if (!$this->normalizers) {
|
||||
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
||||
}
|
||||
|
||||
if ($normalizer = $this->getDenormalizer($data, $class, $format)) {
|
||||
return $normalizer->denormalize($data, $class, $format, $context);
|
||||
}
|
||||
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return $this->encoder->supportsEncoding($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format)
|
||||
{
|
||||
return $this->decoder->supportsDecoding($format);
|
||||
}
|
||||
}
|
27
vendor/symfony/serializer/SerializerAwareInterface.php
vendored
Normal file
27
vendor/symfony/serializer/SerializerAwareInterface.php
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Defines the interface of encoders.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface SerializerAwareInterface
|
||||
{
|
||||
/**
|
||||
* Sets the owning Serializer object.
|
||||
*
|
||||
* @param SerializerInterface $serializer
|
||||
*/
|
||||
public function setSerializer(SerializerInterface $serializer);
|
||||
}
|
43
vendor/symfony/serializer/SerializerInterface.php
vendored
Normal file
43
vendor/symfony/serializer/SerializerInterface.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;
|
||||
|
||||
/**
|
||||
* Defines the interface of the Serializer.
|
||||
*
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface SerializerInterface
|
||||
{
|
||||
/**
|
||||
* Serializes data in the appropriate format.
|
||||
*
|
||||
* @param mixed $data any data
|
||||
* @param string $format format name
|
||||
* @param array $context options normalizers/encoders have access to
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function serialize($data, $format, array $context = array());
|
||||
|
||||
/**
|
||||
* Deserializes data into the given type.
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param string $type
|
||||
* @param string $format
|
||||
* @param array $context
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function deserialize($data, $type, $format, array $context = array());
|
||||
}
|
52
vendor/symfony/serializer/Tests/Annotation/GroupsTest.php
vendored
Normal file
52
vendor/symfony/serializer/Tests/Annotation/GroupsTest.php
vendored
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?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\Tests\Annotation;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GroupsTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testEmptyGroupsParameter()
|
||||
{
|
||||
new Groups(array('value' => array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testNotAnArrayGroupsParameter()
|
||||
{
|
||||
new Groups(array('value' => 'coopTilleuls'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidGroupsParameter()
|
||||
{
|
||||
new Groups(array('value' => array('a', 1, new \stdClass())));
|
||||
}
|
||||
|
||||
public function testGroupsParameters()
|
||||
{
|
||||
$validData = array('a', 'b');
|
||||
|
||||
$groups = new Groups(array('value' => $validData));
|
||||
$this->assertEquals($validData, $groups->getGroups());
|
||||
}
|
||||
}
|
79
vendor/symfony/serializer/Tests/Encoder/JsonEncoderTest.php
vendored
Normal file
79
vendor/symfony/serializer/Tests/Encoder/JsonEncoderTest.php
vendored
Normal file
|
@ -0,0 +1,79 @@
|
|||
<?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\Tests\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
|
||||
class JsonEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encoder = new JsonEncoder();
|
||||
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
|
||||
}
|
||||
|
||||
public function testEncodeScalar()
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$obj->foo = 'foo';
|
||||
|
||||
$expected = '{"foo":"foo"}';
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
|
||||
}
|
||||
|
||||
public function testComplexObject()
|
||||
{
|
||||
$obj = $this->getObject();
|
||||
|
||||
$expected = $this->getJsonSource();
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
|
||||
}
|
||||
|
||||
public function testOptions()
|
||||
{
|
||||
$context = array('json_encode_options' => JSON_NUMERIC_CHECK);
|
||||
|
||||
$arr = array();
|
||||
$arr['foo'] = '3';
|
||||
|
||||
$expected = '{"foo":3}';
|
||||
|
||||
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json', $context));
|
||||
|
||||
$arr = array();
|
||||
$arr['foo'] = '3';
|
||||
|
||||
$expected = '{"foo":"3"}';
|
||||
|
||||
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
|
||||
}
|
||||
|
||||
protected function getJsonSource()
|
||||
{
|
||||
return '{"foo":"foo","bar":["a","b"],"baz":{"key":"val","key2":"val","A B":"bar","item":[{"title":"title1"},{"title":"title2"}],"Barry":{"FooBar":{"Baz":"Ed","@id":1}}},"qux":"1"}';
|
||||
}
|
||||
|
||||
protected function getObject()
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$obj->foo = 'foo';
|
||||
$obj->bar = array('a', 'b');
|
||||
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));
|
||||
$obj->qux = '1';
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
510
vendor/symfony/serializer/Tests/Encoder/XmlEncoderTest.php
vendored
Normal file
510
vendor/symfony/serializer/Tests/Encoder/XmlEncoderTest.php
vendored
Normal file
|
@ -0,0 +1,510 @@
|
|||
<?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\Tests\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
|
||||
class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $encoder;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encoder = new XmlEncoder();
|
||||
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
|
||||
$this->encoder->setSerializer($serializer);
|
||||
}
|
||||
|
||||
public function testEncodeScalar()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = 'foo';
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response>foo</response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testSetRootNodeName()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = 'foo';
|
||||
|
||||
$this->encoder->setRootNodeName('test');
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<test>foo</test>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
* @expectedExceptionMessage Document types are not allowed.
|
||||
*/
|
||||
public function testDocTypeIsNotAllowed()
|
||||
{
|
||||
$this->encoder->decode('<?xml version="1.0"?><!DOCTYPE foo><foo></foo>', 'foo');
|
||||
}
|
||||
|
||||
public function testAttributes()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = array(
|
||||
'foo-bar' => array(
|
||||
'@id' => 1,
|
||||
'@name' => 'Bar',
|
||||
),
|
||||
'Foo' => array(
|
||||
'Bar' => 'Test',
|
||||
'@Type' => 'test',
|
||||
),
|
||||
'föo_bär' => 'a',
|
||||
'Bar' => array(1,2,3),
|
||||
'a' => 'b',
|
||||
);
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<foo-bar id="1" name="Bar"/>'.
|
||||
'<Foo Type="test"><Bar>Test</Bar></Foo>'.
|
||||
'<föo_bär>a</föo_bär>'.
|
||||
'<Bar>1</Bar>'.
|
||||
'<Bar>2</Bar>'.
|
||||
'<Bar>3</Bar>'.
|
||||
'<a>b</a>'.
|
||||
'</response>'."\n";
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testElementNameValid()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = array(
|
||||
'foo-bar' => 'a',
|
||||
'foo_bar' => 'a',
|
||||
'föo_bär' => 'a',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<foo-bar>a</foo-bar>'.
|
||||
'<foo_bar>a</foo_bar>'.
|
||||
'<föo_bär>a</föo_bär>'.
|
||||
'</response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeSimpleXML()
|
||||
{
|
||||
$xml = simplexml_load_string('<firstname>Peter</firstname>');
|
||||
$array = array('person' => $xml);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response><person><firstname>Peter</firstname></person></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeXmlAttributes()
|
||||
{
|
||||
$xml = simplexml_load_string('<firstname>Peter</firstname>');
|
||||
$array = array('person' => $xml);
|
||||
|
||||
$expected = '<?xml version="1.1" encoding="utf-8" standalone="yes"?>'."\n".
|
||||
'<response><person><firstname>Peter</firstname></person></response>'."\n";
|
||||
|
||||
$context = array(
|
||||
'xml_version' => '1.1',
|
||||
'xml_encoding' => 'utf-8',
|
||||
'xml_standalone' => true,
|
||||
);
|
||||
|
||||
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
|
||||
}
|
||||
|
||||
public function testContext()
|
||||
{
|
||||
$array = array('person' => array('name' => 'George Abitbol'));
|
||||
$expected = <<<XML
|
||||
<?xml version="1.0"?>
|
||||
<response>
|
||||
<person>
|
||||
<name>George Abitbol</name>
|
||||
</person>
|
||||
</response>
|
||||
|
||||
XML;
|
||||
|
||||
$context = array(
|
||||
'xml_format_output' => true,
|
||||
);
|
||||
|
||||
$this->assertSame($expected, $this->encoder->encode($array, 'xml', $context));
|
||||
}
|
||||
|
||||
public function testEncodeScalarRootAttributes()
|
||||
{
|
||||
$array = array(
|
||||
'#' => 'Paul',
|
||||
'@gender' => 'm',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response gender="m">Paul</response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeRootAttributes()
|
||||
{
|
||||
$array = array(
|
||||
'firstname' => 'Paul',
|
||||
'@gender' => 'm',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response gender="m"><firstname>Paul</firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeCdataWrapping()
|
||||
{
|
||||
$array = array(
|
||||
'firstname' => 'Paul <or Me>',
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeScalarWithAttribute()
|
||||
{
|
||||
$array = array(
|
||||
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<response><person gender="M">Peter</person></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeScalar()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<response>foo</response>'."\n";
|
||||
|
||||
$this->assertEquals('foo', $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
$source = $this->getXmlSource();
|
||||
$obj = $this->getObject();
|
||||
|
||||
$this->assertEquals($source, $this->encoder->encode($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeWithNamespace()
|
||||
{
|
||||
$source = $this->getNamespacedXmlSource();
|
||||
$array = $this->getNamespacedArray();
|
||||
|
||||
$this->assertEquals($source, $this->encoder->encode($array, 'xml'));
|
||||
}
|
||||
|
||||
public function testEncodeSerializerXmlRootNodeNameOption()
|
||||
{
|
||||
$options = array('xml_root_node_name' => 'test');
|
||||
$this->encoder = new XmlEncoder();
|
||||
$serializer = new Serializer(array(), array('xml' => new XmlEncoder()));
|
||||
$this->encoder->setSerializer($serializer);
|
||||
|
||||
$array = array(
|
||||
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
||||
);
|
||||
|
||||
$expected = '<?xml version="1.0"?>'."\n".
|
||||
'<test><person gender="M">Peter</person></test>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $serializer->serialize($array, 'xml', $options));
|
||||
}
|
||||
|
||||
public function testDecode()
|
||||
{
|
||||
$source = $this->getXmlSource();
|
||||
$obj = $this->getObject();
|
||||
|
||||
$this->assertEquals(get_object_vars($obj), $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeCdataWrapping()
|
||||
{
|
||||
$expected = array(
|
||||
'firstname' => 'Paul <or Me>',
|
||||
);
|
||||
|
||||
$xml = '<?xml version="1.0"?>'."\n".
|
||||
'<response><firstname><![CDATA[Paul <or Me>]]></firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeCdataWrappingAndWhitespace()
|
||||
{
|
||||
$expected = array(
|
||||
'firstname' => 'Paul <or Me>',
|
||||
);
|
||||
|
||||
$xml = '<?xml version="1.0"?>'."\n".
|
||||
'<response><firstname>'."\n".
|
||||
'<![CDATA[Paul <or Me>]]></firstname></response>'."\n";
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeWithNamespace()
|
||||
{
|
||||
$source = $this->getNamespacedXmlSource();
|
||||
$array = $this->getNamespacedArray();
|
||||
|
||||
$this->assertEquals($array, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeScalarWithAttribute()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<response><person gender="M">Peter</person></response>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'person' => array('@gender' => 'M', '#' => 'Peter'),
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeScalarRootAttributes()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<person gender="M">Peter</person>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'#' => 'Peter',
|
||||
'@gender' => 'M',
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeRootAttributes()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<person gender="M"><firstname>Peter</firstname><lastname>Mac Calloway</lastname></person>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'firstname' => 'Peter',
|
||||
'lastname' => 'Mac Calloway',
|
||||
'@gender' => 'M',
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeArray()
|
||||
{
|
||||
$source = '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<people>'.
|
||||
'<person><firstname>Benjamin</firstname><lastname>Alexandre</lastname></person>'.
|
||||
'<person><firstname>Damien</firstname><lastname>Clay</lastname></person>'.
|
||||
'</people>'.
|
||||
'</response>'."\n";
|
||||
|
||||
$expected = array(
|
||||
'people' => array('person' => array(
|
||||
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
|
||||
array('firstname' => 'Damien', 'lastname' => 'Clay'),
|
||||
)),
|
||||
);
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeIgnoreWhiteSpace()
|
||||
{
|
||||
$source = <<<XML
|
||||
<?xml version="1.0"?>
|
||||
<people>
|
||||
<person>
|
||||
<firstname>Benjamin</firstname>
|
||||
<lastname>Alexandre</lastname>
|
||||
</person>
|
||||
<person>
|
||||
<firstname>Damien</firstname>
|
||||
<lastname>Clay</lastname>
|
||||
</person>
|
||||
</people>
|
||||
XML;
|
||||
$expected = array('person' => array(
|
||||
array('firstname' => 'Benjamin', 'lastname' => 'Alexandre'),
|
||||
array('firstname' => 'Damien', 'lastname' => 'Clay'),
|
||||
));
|
||||
|
||||
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
|
||||
}
|
||||
|
||||
public function testDecodeWithoutItemHash()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->xmlFoo = array(
|
||||
'foo-bar' => array(
|
||||
'@key' => 'value',
|
||||
'item' => array('@key' => 'key', 'key-val' => 'val'),
|
||||
),
|
||||
'Foo' => array(
|
||||
'Bar' => 'Test',
|
||||
'@Type' => 'test',
|
||||
),
|
||||
'föo_bär' => 'a',
|
||||
'Bar' => array(1,2,3),
|
||||
'a' => 'b',
|
||||
);
|
||||
$expected = array(
|
||||
'foo-bar' => array(
|
||||
'@key' => 'value',
|
||||
'key' => array('@key' => 'key', 'key-val' => 'val'),
|
||||
),
|
||||
'Foo' => array(
|
||||
'Bar' => 'Test',
|
||||
'@Type' => 'test',
|
||||
),
|
||||
'föo_bär' => 'a',
|
||||
'Bar' => array(1,2,3),
|
||||
'a' => 'b',
|
||||
);
|
||||
$xml = $this->encoder->encode($obj, 'xml');
|
||||
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDecodeInvalidXml()
|
||||
{
|
||||
$this->encoder->decode('<?xml version="1.0"?><invalid><xml>', 'xml');
|
||||
}
|
||||
|
||||
public function testPreventsComplexExternalEntities()
|
||||
{
|
||||
$oldCwd = getcwd();
|
||||
chdir(__DIR__);
|
||||
|
||||
try {
|
||||
$this->encoder->decode('<?xml version="1.0"?><!DOCTYPE scan[<!ENTITY test SYSTEM "php://filter/read=convert.base64-encode/resource=XmlEncoderTest.php">]><scan>&test;</scan>', 'xml');
|
||||
chdir($oldCwd);
|
||||
|
||||
$this->fail('No exception was thrown.');
|
||||
} catch (\Exception $e) {
|
||||
chdir($oldCwd);
|
||||
|
||||
if (!$e instanceof UnexpectedValueException) {
|
||||
$this->fail('Expected UnexpectedValueException');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testDecodeEmptyXml()
|
||||
{
|
||||
$this->setExpectedException('Symfony\Component\Serializer\Exception\UnexpectedValueException', 'Invalid XML data, it can not be empty.');
|
||||
$this->encoder->decode(' ', 'xml');
|
||||
}
|
||||
|
||||
protected function getXmlSource()
|
||||
{
|
||||
return '<?xml version="1.0"?>'."\n".
|
||||
'<response>'.
|
||||
'<foo>foo</foo>'.
|
||||
'<bar>a</bar><bar>b</bar>'.
|
||||
'<baz><key>val</key><key2>val</key2><item key="A B">bar</item>'.
|
||||
'<item><title>title1</title></item><item><title>title2</title></item>'.
|
||||
'<Barry><FooBar id="1"><Baz>Ed</Baz></FooBar></Barry></baz>'.
|
||||
'<qux>1</qux>'.
|
||||
'</response>'."\n";
|
||||
}
|
||||
|
||||
protected function getNamespacedXmlSource()
|
||||
{
|
||||
return '<?xml version="1.0"?>'."\n".
|
||||
'<response xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">'.
|
||||
'<qux>1</qux>'.
|
||||
'<app:foo>foo</app:foo>'.
|
||||
'<yt:bar>a</yt:bar><yt:bar>b</yt:bar>'.
|
||||
'<media:baz><media:key>val</media:key><media:key2>val</media:key2><item key="A B">bar</item>'.
|
||||
'<item><title>title1</title></item><item><title>title2</title></item>'.
|
||||
'<Barry size="large"><FooBar gd:id="1"><Baz>Ed</Baz></FooBar></Barry></media:baz>'.
|
||||
'</response>'."\n";
|
||||
}
|
||||
|
||||
protected function getNamespacedArray()
|
||||
{
|
||||
return array(
|
||||
'@xmlns' => 'http://www.w3.org/2005/Atom',
|
||||
'@xmlns:app' => 'http://www.w3.org/2007/app',
|
||||
'@xmlns:media' => 'http://search.yahoo.com/mrss/',
|
||||
'@xmlns:gd' => 'http://schemas.google.com/g/2005',
|
||||
'@xmlns:yt' => 'http://gdata.youtube.com/schemas/2007',
|
||||
'qux' => '1',
|
||||
'app:foo' => 'foo',
|
||||
'yt:bar' => array('a', 'b'),
|
||||
'media:baz' => array(
|
||||
'media:key' => 'val',
|
||||
'media:key2' => 'val',
|
||||
'A B' => 'bar',
|
||||
'item' => array(
|
||||
array(
|
||||
'title' => 'title1',
|
||||
),
|
||||
array(
|
||||
'title' => 'title2',
|
||||
),
|
||||
),
|
||||
'Barry' => array(
|
||||
'@size' => 'large',
|
||||
'FooBar' => array(
|
||||
'Baz' => 'Ed',
|
||||
'@gd:id' => 1,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getObject()
|
||||
{
|
||||
$obj = new Dummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->bar = array('a', 'b');
|
||||
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));
|
||||
$obj->qux = '1';
|
||||
|
||||
return $obj;
|
||||
}
|
||||
}
|
23
vendor/symfony/serializer/Tests/Fixtures/CircularReferenceDummy.php
vendored
Normal file
23
vendor/symfony/serializer/Tests/Fixtures/CircularReferenceDummy.php
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class CircularReferenceDummy
|
||||
{
|
||||
public function getMe()
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
22
vendor/symfony/serializer/Tests/Fixtures/DenormalizableDummy.php
vendored
Normal file
22
vendor/symfony/serializer/Tests/Fixtures/DenormalizableDummy.php
vendored
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class DenormalizableDummy implements DenormalizableInterface
|
||||
{
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
}
|
||||
}
|
43
vendor/symfony/serializer/Tests/Fixtures/Dummy.php
vendored
Normal file
43
vendor/symfony/serializer/Tests/Fixtures/Dummy.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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class Dummy implements NormalizableInterface, DenormalizableInterface
|
||||
{
|
||||
public $foo;
|
||||
public $bar;
|
||||
public $baz;
|
||||
public $qux;
|
||||
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array())
|
||||
{
|
||||
return array(
|
||||
'foo' => $this->foo,
|
||||
'bar' => $this->bar,
|
||||
'baz' => $this->baz,
|
||||
'qux' => $this->qux,
|
||||
);
|
||||
}
|
||||
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
$this->foo = $data['foo'];
|
||||
$this->bar = $data['bar'];
|
||||
$this->baz = $data['baz'];
|
||||
$this->qux = $data['qux'];
|
||||
}
|
||||
}
|
80
vendor/symfony/serializer/Tests/Fixtures/GroupDummy.php
vendored
Normal file
80
vendor/symfony/serializer/Tests/Fixtures/GroupDummy.php
vendored
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GroupDummy extends GroupDummyParent implements GroupDummyInterface
|
||||
{
|
||||
/**
|
||||
* @Groups({"a"})
|
||||
*/
|
||||
private $foo;
|
||||
/**
|
||||
* @Groups({"b", "c", "name_converter"})
|
||||
*/
|
||||
protected $bar;
|
||||
private $fooBar;
|
||||
private $symfony;
|
||||
|
||||
/**
|
||||
* @Groups({"b"})
|
||||
*/
|
||||
public function setBar($bar)
|
||||
{
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"c"})
|
||||
*/
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function setFooBar($fooBar)
|
||||
{
|
||||
$this->fooBar = $fooBar;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"a", "b", "name_converter"})
|
||||
*/
|
||||
public function isFooBar()
|
||||
{
|
||||
return $this->fooBar;
|
||||
}
|
||||
|
||||
public function setSymfony($symfony)
|
||||
{
|
||||
$this->symfony = $symfony;
|
||||
}
|
||||
|
||||
public function getSymfony()
|
||||
{
|
||||
return $this->symfony;
|
||||
}
|
||||
}
|
25
vendor/symfony/serializer/Tests/Fixtures/GroupDummyInterface.php
vendored
Normal file
25
vendor/symfony/serializer/Tests/Fixtures/GroupDummyInterface.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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
interface GroupDummyInterface
|
||||
{
|
||||
/**
|
||||
* @Groups({"a", "name_converter"})
|
||||
*/
|
||||
public function getSymfony();
|
||||
}
|
49
vendor/symfony/serializer/Tests/Fixtures/GroupDummyParent.php
vendored
Normal file
49
vendor/symfony/serializer/Tests/Fixtures/GroupDummyParent.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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class GroupDummyParent
|
||||
{
|
||||
/**
|
||||
* @Groups({"a"})
|
||||
*/
|
||||
private $kevin;
|
||||
private $coopTilleuls;
|
||||
|
||||
public function setKevin($kevin)
|
||||
{
|
||||
$this->kevin = $kevin;
|
||||
}
|
||||
|
||||
public function getKevin()
|
||||
{
|
||||
return $this->kevin;
|
||||
}
|
||||
|
||||
public function setCoopTilleuls($coopTilleuls)
|
||||
{
|
||||
$this->coopTilleuls = $coopTilleuls;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Groups({"a", "b"})
|
||||
*/
|
||||
public function getCoopTilleuls()
|
||||
{
|
||||
return $this->coopTilleuls;
|
||||
}
|
||||
}
|
36
vendor/symfony/serializer/Tests/Fixtures/NormalizableTraversableDummy.php
vendored
Normal file
36
vendor/symfony/serializer/Tests/Fixtures/NormalizableTraversableDummy.php
vendored
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class NormalizableTraversableDummy extends TraversableDummy implements NormalizableInterface, DenormalizableInterface
|
||||
{
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array())
|
||||
{
|
||||
return array(
|
||||
'foo' => 'normalizedFoo',
|
||||
'bar' => 'normalizedBar',
|
||||
);
|
||||
}
|
||||
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
return array(
|
||||
'foo' => 'denormalizedFoo',
|
||||
'bar' => 'denormalizedBar',
|
||||
);
|
||||
}
|
||||
}
|
25
vendor/symfony/serializer/Tests/Fixtures/PropertyCircularReferenceDummy.php
vendored
Normal file
25
vendor/symfony/serializer/Tests/Fixtures/PropertyCircularReferenceDummy.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\Tests\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertyCircularReferenceDummy
|
||||
{
|
||||
public $me;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->me = $this;
|
||||
}
|
||||
}
|
39
vendor/symfony/serializer/Tests/Fixtures/PropertySiblingHolder.php
vendored
Normal file
39
vendor/symfony/serializer/Tests/Fixtures/PropertySiblingHolder.php
vendored
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertySiblingHolder
|
||||
{
|
||||
public $sibling0;
|
||||
public $sibling1;
|
||||
public $sibling2;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$sibling = new PropertySibling();
|
||||
|
||||
$this->sibling0 = $sibling;
|
||||
$this->sibling1 = $sibling;
|
||||
$this->sibling2 = $sibling;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class PropertySibling
|
||||
{
|
||||
public $coopTilleuls = 'Les-Tilleuls.coop';
|
||||
}
|
37
vendor/symfony/serializer/Tests/Fixtures/ScalarDummy.php
vendored
Normal file
37
vendor/symfony/serializer/Tests/Fixtures/ScalarDummy.php
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizableInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
class ScalarDummy implements NormalizableInterface, DenormalizableInterface
|
||||
{
|
||||
public $foo;
|
||||
public $xmlFoo;
|
||||
|
||||
public function normalize(NormalizerInterface $normalizer, $format = null, array $context = array())
|
||||
{
|
||||
return $format === 'xml' ? $this->xmlFoo : $this->foo;
|
||||
}
|
||||
|
||||
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = array())
|
||||
{
|
||||
if ($format === 'xml') {
|
||||
$this->xmlFoo = $data;
|
||||
} else {
|
||||
$this->foo = $data;
|
||||
}
|
||||
}
|
||||
}
|
57
vendor/symfony/serializer/Tests/Fixtures/SiblingHolder.php
vendored
Normal file
57
vendor/symfony/serializer/Tests/Fixtures/SiblingHolder.php
vendored
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class SiblingHolder
|
||||
{
|
||||
private $sibling0;
|
||||
private $sibling1;
|
||||
private $sibling2;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$sibling = new Sibling();
|
||||
|
||||
$this->sibling0 = $sibling;
|
||||
$this->sibling1 = $sibling;
|
||||
$this->sibling2 = $sibling;
|
||||
}
|
||||
|
||||
public function getSibling0()
|
||||
{
|
||||
return $this->sibling0;
|
||||
}
|
||||
|
||||
public function getSibling1()
|
||||
{
|
||||
return $this->sibling1;
|
||||
}
|
||||
|
||||
public function getSibling2()
|
||||
{
|
||||
return $this->sibling2;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class Sibling
|
||||
{
|
||||
public function getCoopTilleuls()
|
||||
{
|
||||
return 'Les-Tilleuls.coop';
|
||||
}
|
||||
}
|
23
vendor/symfony/serializer/Tests/Fixtures/TraversableDummy.php
vendored
Normal file
23
vendor/symfony/serializer/Tests/Fixtures/TraversableDummy.php
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
class TraversableDummy implements \IteratorAggregate
|
||||
{
|
||||
public $foo = 'foo';
|
||||
public $bar = 'bar';
|
||||
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator(get_object_vars($this));
|
||||
}
|
||||
}
|
27
vendor/symfony/serializer/Tests/Fixtures/VariadicConstructorArgsDummy.php
vendored
Normal file
27
vendor/symfony/serializer/Tests/Fixtures/VariadicConstructorArgsDummy.php
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?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\Tests\Fixtures;
|
||||
|
||||
class VariadicConstructorArgsDummy
|
||||
{
|
||||
private $foo;
|
||||
|
||||
public function __construct(...$foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
}
|
0
vendor/symfony/serializer/Tests/Fixtures/empty-mapping.yml
vendored
Normal file
0
vendor/symfony/serializer/Tests/Fixtures/empty-mapping.yml
vendored
Normal file
1
vendor/symfony/serializer/Tests/Fixtures/invalid-mapping.yml
vendored
Normal file
1
vendor/symfony/serializer/Tests/Fixtures/invalid-mapping.yml
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
foo
|
18
vendor/symfony/serializer/Tests/Fixtures/serialization.xml
vendored
Normal file
18
vendor/symfony/serializer/Tests/Fixtures/serialization.xml
vendored
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" ?>
|
||||
|
||||
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping http://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd">
|
||||
|
||||
<class name="Symfony\Component\Serializer\Tests\Fixtures\GroupDummy">
|
||||
<attribute name="foo">
|
||||
<group>group1</group>
|
||||
<group>group2</group>
|
||||
</attribute>
|
||||
|
||||
<attribute name="bar">
|
||||
<group>group2</group>
|
||||
</attribute>
|
||||
</class>
|
||||
|
||||
</serializer>
|
6
vendor/symfony/serializer/Tests/Fixtures/serialization.yml
vendored
Normal file
6
vendor/symfony/serializer/Tests/Fixtures/serialization.yml
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
Symfony\Component\Serializer\Tests\Fixtures\GroupDummy:
|
||||
attributes:
|
||||
foo:
|
||||
groups: ['group1', 'group2']
|
||||
bar:
|
||||
groups: ['group2']
|
67
vendor/symfony/serializer/Tests/Mapping/AttributeMetadataTest.php
vendored
Normal file
67
vendor/symfony/serializer/Tests/Mapping/AttributeMetadataTest.php
vendored
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?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\Tests\Mapping;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class AttributeMetadataTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('name');
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface', $attributeMetadata);
|
||||
}
|
||||
|
||||
public function testGetName()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('name');
|
||||
$this->assertEquals('name', $attributeMetadata->getName());
|
||||
}
|
||||
|
||||
public function testGroups()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('group');
|
||||
$attributeMetadata->addGroup('a');
|
||||
$attributeMetadata->addGroup('a');
|
||||
$attributeMetadata->addGroup('b');
|
||||
|
||||
$this->assertEquals(array('a', 'b'), $attributeMetadata->getGroups());
|
||||
}
|
||||
|
||||
public function testMerge()
|
||||
{
|
||||
$attributeMetadata1 = new AttributeMetadata('a1');
|
||||
$attributeMetadata1->addGroup('a');
|
||||
$attributeMetadata1->addGroup('b');
|
||||
|
||||
$attributeMetadata2 = new AttributeMetadata('a2');
|
||||
$attributeMetadata2->addGroup('a');
|
||||
$attributeMetadata2->addGroup('c');
|
||||
|
||||
$attributeMetadata1->merge($attributeMetadata2);
|
||||
|
||||
$this->assertEquals(array('a', 'b', 'c'), $attributeMetadata1->getGroups());
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$attributeMetadata = new AttributeMetadata('attribute');
|
||||
$attributeMetadata->addGroup('a');
|
||||
$attributeMetadata->addGroup('b');
|
||||
|
||||
$serialized = serialize($attributeMetadata);
|
||||
$this->assertEquals($attributeMetadata, unserialize($serialized));
|
||||
}
|
||||
}
|
82
vendor/symfony/serializer/Tests/Mapping/ClassMetadataTest.php
vendored
Normal file
82
vendor/symfony/serializer/Tests/Mapping/ClassMetadataTest.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\Tests\Mapping;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ClassMetadataTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('name');
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\ClassMetadataInterface', $classMetadata);
|
||||
}
|
||||
|
||||
public function testAttributeMetadata()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('c');
|
||||
|
||||
$a1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a1->method('getName')->willReturn('a1');
|
||||
|
||||
$a2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a2->method('getName')->willReturn('a2');
|
||||
|
||||
$classMetadata->addAttributeMetadata($a1);
|
||||
$classMetadata->addAttributeMetadata($a2);
|
||||
|
||||
$this->assertEquals(array('a1' => $a1, 'a2' => $a2), $classMetadata->getAttributesMetadata());
|
||||
}
|
||||
|
||||
public function testMerge()
|
||||
{
|
||||
$classMetadata1 = new ClassMetadata('c1');
|
||||
$classMetadata2 = new ClassMetadata('c2');
|
||||
|
||||
$ac1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$ac1->method('getName')->willReturn('a1');
|
||||
$ac1->method('getGroups')->willReturn(array('a', 'b'));
|
||||
|
||||
$ac2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$ac2->method('getName')->willReturn('a1');
|
||||
$ac2->method('getGroups')->willReturn(array('b', 'c'));
|
||||
|
||||
$classMetadata1->addAttributeMetadata($ac1);
|
||||
$classMetadata2->addAttributeMetadata($ac2);
|
||||
|
||||
$classMetadata1->merge($classMetadata2);
|
||||
|
||||
$ac1->method('getGroups')->willReturn('a', 'b', 'c');
|
||||
|
||||
$this->assertEquals(array('a1' => $ac1), $classMetadata2->getAttributesMetadata());
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('a');
|
||||
|
||||
$a1 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a1->method('getName')->willReturn('b1');
|
||||
|
||||
$a2 = $this->getMock('Symfony\Component\Serializer\Mapping\AttributeMetadataInterface');
|
||||
$a2->method('getName')->willReturn('b2');
|
||||
|
||||
$classMetadata->addAttributeMetadata($a1);
|
||||
$classMetadata->addAttributeMetadata($a2);
|
||||
|
||||
$serialized = serialize($classMetadata);
|
||||
$this->assertEquals($classMetadata, unserialize($serialized));
|
||||
}
|
||||
}
|
78
vendor/symfony/serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php
vendored
Normal file
78
vendor/symfony/serializer/Tests/Mapping/Factory/ClassMetadataFactoryTest.php
vendored
Normal file
|
@ -0,0 +1,78 @@
|
|||
<?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\Tests\Mapping\Factory;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ClassMetadataFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$classMetadata = new ClassMetadataFactory(new LoaderChain(array()));
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory', $classMetadata);
|
||||
}
|
||||
|
||||
public function testGetMetadataFor()
|
||||
{
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$classMetadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $classMetadata);
|
||||
}
|
||||
|
||||
public function testHasMetadataFor()
|
||||
{
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'));
|
||||
$this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent'));
|
||||
$this->assertTrue($factory->hasMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyInterface'));
|
||||
$this->assertFalse($factory->hasMetadataFor('Dunglas\Entity'));
|
||||
}
|
||||
|
||||
public function testCacheExists()
|
||||
{
|
||||
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
|
||||
$cache
|
||||
->expects($this->once())
|
||||
->method('fetch')
|
||||
->will($this->returnValue('foo'))
|
||||
;
|
||||
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()), $cache);
|
||||
$this->assertEquals('foo', $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy'));
|
||||
}
|
||||
|
||||
public function testCacheNotExists()
|
||||
{
|
||||
$cache = $this->getMock('Doctrine\Common\Cache\Cache');
|
||||
$cache
|
||||
->method('fetch')
|
||||
->will($this->returnValue(false))
|
||||
;
|
||||
|
||||
$cache
|
||||
->method('save')
|
||||
;
|
||||
|
||||
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()), $cache);
|
||||
$metadata = $factory->getMetadataFor('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(true, true), $metadata);
|
||||
}
|
||||
}
|
66
vendor/symfony/serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php
vendored
Normal file
66
vendor/symfony/serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?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\Tests\Mapping\Loader;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class AnnotationLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var AnnotationLoader
|
||||
*/
|
||||
private $loader;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loader = new AnnotationLoader(new AnnotationReader());
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Loader\LoaderInterface', $this->loader);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsTrueIfSuccessful()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$this->assertTrue($this->loader->loadClassMetadata($classMetadata));
|
||||
}
|
||||
|
||||
public function testLoadClassMetadata()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
$this->loader->loadClassMetadata($classMetadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(), $classMetadata);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataAndMerge()
|
||||
{
|
||||
$classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
$parentClassMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummyParent');
|
||||
|
||||
$this->loader->loadClassMetadata($parentClassMetadata);
|
||||
$classMetadata->merge($parentClassMetadata);
|
||||
|
||||
$this->loader->loadClassMetadata($classMetadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createClassMetadata(true), $classMetadata);
|
||||
}
|
||||
}
|
54
vendor/symfony/serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php
vendored
Normal file
54
vendor/symfony/serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php
vendored
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?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\Tests\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var XmlFileLoader
|
||||
*/
|
||||
private $loader;
|
||||
/**
|
||||
* @var ClassMetadata
|
||||
*/
|
||||
private $metadata;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loader = new XmlFileLoader(__DIR__.'/../../Fixtures/serialization.xml');
|
||||
$this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Loader\LoaderInterface', $this->loader);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsTrueIfSuccessful()
|
||||
{
|
||||
$this->assertTrue($this->loader->loadClassMetadata($this->metadata));
|
||||
}
|
||||
|
||||
public function testLoadClassMetadata()
|
||||
{
|
||||
$this->loader->loadClassMetadata($this->metadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createXmlCLassMetadata(), $this->metadata);
|
||||
}
|
||||
}
|
69
vendor/symfony/serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php
vendored
Normal file
69
vendor/symfony/serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?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\Tests\Mapping\Loader;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var YamlFileLoader
|
||||
*/
|
||||
private $loader;
|
||||
/**
|
||||
* @var ClassMetadata
|
||||
*/
|
||||
private $metadata;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->loader = new YamlFileLoader(__DIR__.'/../../Fixtures/serialization.yml');
|
||||
$this->metadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Mapping\Loader\LoaderInterface', $this->loader);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsTrueIfSuccessful()
|
||||
{
|
||||
$this->assertTrue($this->loader->loadClassMetadata($this->metadata));
|
||||
}
|
||||
|
||||
public function testLoadClassMetadataReturnsFalseWhenEmpty()
|
||||
{
|
||||
$loader = new YamlFileLoader(__DIR__.'/../../Fixtures/empty-mapping.yml');
|
||||
$this->assertFalse($loader->loadClassMetadata($this->metadata));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\MappingException
|
||||
*/
|
||||
public function testLoadClassMetadataReturnsThrowsInvalidMapping()
|
||||
{
|
||||
$loader = new YamlFileLoader(__DIR__.'/../../Fixtures/invalid-mapping.yml');
|
||||
$loader->loadClassMetadata($this->metadata);
|
||||
}
|
||||
|
||||
public function testLoadClassMetadata()
|
||||
{
|
||||
$this->loader->loadClassMetadata($this->metadata);
|
||||
|
||||
$this->assertEquals(TestClassMetadataFactory::createXmlCLassMetadata(), $this->metadata);
|
||||
}
|
||||
}
|
82
vendor/symfony/serializer/Tests/Mapping/TestClassMetadataFactory.php
vendored
Normal file
82
vendor/symfony/serializer/Tests/Mapping/TestClassMetadataFactory.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\Tests\Mapping;
|
||||
|
||||
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
|
||||
use Symfony\Component\Serializer\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class TestClassMetadataFactory
|
||||
{
|
||||
public static function createClassMetadata($withParent = false, $withInterface = false)
|
||||
{
|
||||
$expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$foo = new AttributeMetadata('foo');
|
||||
$foo->addGroup('a');
|
||||
$expected->addAttributeMetadata($foo);
|
||||
|
||||
$bar = new AttributeMetadata('bar');
|
||||
$bar->addGroup('b');
|
||||
$bar->addGroup('c');
|
||||
$bar->addGroup('name_converter');
|
||||
$expected->addAttributeMetadata($bar);
|
||||
|
||||
$fooBar = new AttributeMetadata('fooBar');
|
||||
$fooBar->addGroup('a');
|
||||
$fooBar->addGroup('b');
|
||||
$fooBar->addGroup('name_converter');
|
||||
$expected->addAttributeMetadata($fooBar);
|
||||
|
||||
$symfony = new AttributeMetadata('symfony');
|
||||
$expected->addAttributeMetadata($symfony);
|
||||
|
||||
if ($withParent) {
|
||||
$kevin = new AttributeMetadata('kevin');
|
||||
$kevin->addGroup('a');
|
||||
$expected->addAttributeMetadata($kevin);
|
||||
|
||||
$coopTilleuls = new AttributeMetadata('coopTilleuls');
|
||||
$coopTilleuls->addGroup('a');
|
||||
$coopTilleuls->addGroup('b');
|
||||
$expected->addAttributeMetadata($coopTilleuls);
|
||||
}
|
||||
|
||||
if ($withInterface) {
|
||||
$symfony->addGroup('a');
|
||||
$symfony->addGroup('name_converter');
|
||||
}
|
||||
|
||||
// load reflection class so that the comparison passes
|
||||
$expected->getReflectionClass();
|
||||
|
||||
return $expected;
|
||||
}
|
||||
|
||||
public static function createXmlCLassMetadata()
|
||||
{
|
||||
$expected = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\GroupDummy');
|
||||
|
||||
$foo = new AttributeMetadata('foo');
|
||||
$foo->addGroup('group1');
|
||||
$foo->addGroup('group2');
|
||||
$expected->addAttributeMetadata($foo);
|
||||
|
||||
$bar = new AttributeMetadata('bar');
|
||||
$bar->addGroup('group2');
|
||||
$expected->addAttributeMetadata($bar);
|
||||
|
||||
return $expected;
|
||||
}
|
||||
}
|
53
vendor/symfony/serializer/Tests/NameConverter/CamelCaseToSnakeCaseNameConverterTest.php
vendored
Normal file
53
vendor/symfony/serializer/Tests/NameConverter/CamelCaseToSnakeCaseNameConverterTest.php
vendored
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?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\Tests\NameConverter;
|
||||
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class CamelCaseToSnakeCaseNameConverterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$attributeMetadata = new CamelCaseToSnakeCaseNameConverter();
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\NameConverter\NameConverterInterface', $attributeMetadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributeProvider
|
||||
*/
|
||||
public function testNormalize($underscored, $lowerCamelCased)
|
||||
{
|
||||
$nameConverter = new CamelCaseToSnakeCaseNameConverter();
|
||||
$this->assertEquals($nameConverter->normalize($lowerCamelCased), $underscored);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider attributeProvider
|
||||
*/
|
||||
public function testDenormalize($underscored, $lowerCamelCased)
|
||||
{
|
||||
$nameConverter = new CamelCaseToSnakeCaseNameConverter();
|
||||
$this->assertEquals($nameConverter->denormalize($underscored), $lowerCamelCased);
|
||||
}
|
||||
|
||||
public function attributeProvider()
|
||||
{
|
||||
return array(
|
||||
array('coop_tilleuls', 'coopTilleuls'),
|
||||
array('_kevin_dunglas', '_kevinDunglas'),
|
||||
array('this_is_a_test', 'thisIsATest'),
|
||||
);
|
||||
}
|
||||
}
|
69
vendor/symfony/serializer/Tests/Normalizer/CustomNormalizerTest.php
vendored
Normal file
69
vendor/symfony/serializer/Tests/Normalizer/CustomNormalizerTest.php
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?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\Tests\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var CustomNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->normalizer = new CustomNormalizer();
|
||||
$this->normalizer->setSerializer(new Serializer());
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$obj = new ScalarDummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->xmlFoo = 'xml';
|
||||
$this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
|
||||
$this->assertEquals('xml', $this->normalizer->normalize($obj, 'xml'));
|
||||
}
|
||||
|
||||
public function testDeserialize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'xml');
|
||||
$this->assertEquals('foo', $obj->xmlFoo);
|
||||
$this->assertNull($obj->foo);
|
||||
|
||||
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'json');
|
||||
$this->assertEquals('foo', $obj->foo);
|
||||
$this->assertNull($obj->xmlFoo);
|
||||
}
|
||||
|
||||
public function testSupportsNormalization()
|
||||
{
|
||||
$this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy()));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
||||
}
|
||||
|
||||
public function testSupportsDenormalization()
|
||||
{
|
||||
$this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\ScalarDummy'));
|
||||
$this->assertFalse($this->normalizer->supportsDenormalization(array(), 'stdClass'));
|
||||
$this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Component\Serializer\Tests\Fixtures\DenormalizableDummy'));
|
||||
}
|
||||
}
|
799
vendor/symfony/serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
vendored
Normal file
799
vendor/symfony/serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
vendored
Normal file
|
@ -0,0 +1,799 @@
|
|||
<?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\Tests\Normalizer;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||
|
||||
class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var GetSetMethodNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock(__NAMESPACE__.'\SerializerNormalizer');
|
||||
$this->normalizer = new GetSetMethodNormalizer();
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testInterface()
|
||||
{
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $this->normalizer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $this->normalizer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new GetSetDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setBaz(true);
|
||||
$obj->setCamelCase('camelcase');
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->serializer
|
||||
->expects($this->once())
|
||||
->method('normalize')
|
||||
->with($object, 'any')
|
||||
->will($this->returnValue('string_object'))
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'foo' => 'foo',
|
||||
'bar' => 'bar',
|
||||
'baz' => true,
|
||||
'fooBar' => 'foobar',
|
||||
'camelCase' => 'camelcase',
|
||||
'object' => 'string_object',
|
||||
),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\GetSetDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testDenormalizeWithObject()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetSetDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\GetSetDummy'
|
||||
);
|
||||
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testNameConverterSupport()
|
||||
{
|
||||
$this->normalizer = new GetSetMethodNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\GetSetDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testDenormalizeNull()
|
||||
{
|
||||
$this->assertEquals(new GetSetDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\GetSetDummy'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesNormalize()
|
||||
{
|
||||
$obj = new GetCamelizedDummy('dunglas.fr');
|
||||
$obj->setFooBar('les-tilleuls.coop');
|
||||
$obj->setBar_foo('lostinthesupermarket.fr');
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesDenormalize()
|
||||
{
|
||||
$obj = new GetCamelizedDummy('dunglas.fr');
|
||||
$obj->setFooBar('les-tilleuls.coop');
|
||||
$obj->setBar_foo('lostinthesupermarket.fr');
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\GetCamelizedDummy'), $obj);
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\GetCamelizedDummy'), $obj);
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\GetConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithNullArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => null, 'baz' => true),
|
||||
__NAMESPACE__.'\GetConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertNull($obj->getBar());
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithMissingOptionalArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'test', 'baz' => array(1, 2, 3)),
|
||||
__NAMESPACE__.'\GetConstructorOptionalArgsDummy', 'any');
|
||||
$this->assertEquals('test', $obj->getFoo());
|
||||
$this->assertEquals(array(), $obj->getBar());
|
||||
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithOptionalDefaultArgument()
|
||||
{
|
||||
if (PHP_VERSION_ID <= 50316) {
|
||||
$this->markTestSkipped('See https://bugs.php.net/62715');
|
||||
}
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('bar' => 'test'),
|
||||
__NAMESPACE__.'\GetConstructorArgsWithDefaultValueDummy', 'any');
|
||||
$this->assertEquals(array(), $obj->getFoo());
|
||||
$this->assertEquals('test', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 5.6
|
||||
*/
|
||||
public function testConstructorDenormalizeWithVariadicArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => array(1, 2, 3)),
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
|
||||
$this->assertEquals(array(1, 2, 3), $obj->getFoo());
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires PHP 5.6
|
||||
*/
|
||||
public function testConstructorDenormalizeWithMissingVariadicArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array(),
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorArgsDummy', 'any');
|
||||
$this->assertEquals(array(), $obj->getFoo());
|
||||
}
|
||||
|
||||
public function testConstructorWithObjectDenormalize()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->baz = true;
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\GetConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testConstructorWArgWithPrivateMutator()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(array('foo' => 'bar'), __NAMESPACE__.'\ObjectConstructorArgsWithPrivateMutatorDummy', 'any');
|
||||
$this->assertEquals('bar', $obj->getFoo());
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setFooBar('fooBar');
|
||||
$obj->setSymfony('symfony');
|
||||
$obj->setKevin('kevin');
|
||||
$obj->setCoopTilleuls('coopTilleuls');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'symfony' => 'symfony',
|
||||
'foo' => 'foo',
|
||||
'fooBar' => 'fooBar',
|
||||
'bar' => 'bar',
|
||||
'kevin' => 'kevin',
|
||||
'coopTilleuls' => 'coopTilleuls',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
||||
}
|
||||
|
||||
public function testGroupsDenormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
|
||||
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
|
||||
$obj->setBar('bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a', 'b'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
}
|
||||
|
||||
public function testGroupsNormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
$obj->setCoopTilleuls('les-tilleuls.coop');
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
),
|
||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new GetSetMethodNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize(array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCallbacks
|
||||
*/
|
||||
public function testCallbacks($callbacks, $value, $result, $message)
|
||||
{
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
|
||||
$obj = new GetConstructorDummy('', $value, true);
|
||||
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
$this->normalizer->normalize($obj, 'any'),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUncallableCallbacks()
|
||||
{
|
||||
$this->normalizer->setCallbacks(array('bar' => null));
|
||||
|
||||
$obj = new GetConstructorDummy('baz', 'quux', true);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
|
||||
|
||||
$obj = new GetSetDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setBaz(true);
|
||||
|
||||
$this->assertEquals(
|
||||
array('fooBar' => 'foobar'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function provideCallbacks()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return 'baz';
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => 'baz', 'baz' => true),
|
||||
'Change a string',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return;
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => null, 'baz' => true),
|
||||
'Null an item',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return $bar->format('d-m-Y H:i:s');
|
||||
},
|
||||
),
|
||||
new \DateTime('2011-09-10 06:30:00'),
|
||||
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
|
||||
'Format a date',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
$foos = '';
|
||||
foreach ($bars as $bar) {
|
||||
$foos .= $bar->getFoo();
|
||||
}
|
||||
|
||||
return $foos;
|
||||
},
|
||||
),
|
||||
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
|
||||
'Collect a property',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
return count($bars);
|
||||
},
|
||||
),
|
||||
array(new GetConstructorDummy('baz', '', false), new GetConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 2, 'baz' => true),
|
||||
'Count a property',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new GetSetDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceLimit(2);
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$this->normalizer->normalize($obj);
|
||||
}
|
||||
|
||||
public function testSiblingReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$siblingHolder = new SiblingHolder();
|
||||
|
||||
$expected = array(
|
||||
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
);
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceHandler(function ($obj) {
|
||||
return get_class($obj);
|
||||
});
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
||||
}
|
||||
|
||||
public function testObjectToPopulate()
|
||||
{
|
||||
$dummy = new GetSetDummy();
|
||||
$dummy->setFoo('foo');
|
||||
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('bar' => 'bar'),
|
||||
__NAMESPACE__.'\GetSetDummy',
|
||||
null,
|
||||
array('object_to_populate' => $dummy)
|
||||
);
|
||||
|
||||
$this->assertEquals($dummy, $obj);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testDenormalizeNonExistingAttribute()
|
||||
{
|
||||
$this->assertEquals(
|
||||
new GetSetDummy(),
|
||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\GetSetDummy')
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoTraversableSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
|
||||
public function testPrivateSetter()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(array('foo' => 'foobar'), __NAMESPACE__.'\ObjectWithPrivateSetterDummy');
|
||||
$this->assertEquals('bar', $obj->getFoo());
|
||||
}
|
||||
}
|
||||
|
||||
class GetSetDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
private $baz;
|
||||
protected $camelCase;
|
||||
protected $object;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function setBar($bar)
|
||||
{
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function setBaz($baz)
|
||||
{
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFooBar()
|
||||
{
|
||||
return $this->foo.$this->bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
|
||||
public function setObject($object)
|
||||
{
|
||||
$this->object = $object;
|
||||
}
|
||||
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
}
|
||||
|
||||
class GetConstructorDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar, $baz)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
|
||||
{
|
||||
}
|
||||
|
||||
class GetConstructorOptionalArgsDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar = array(), $baz = array())
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function getBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
class GetConstructorArgsWithDefaultValueDummy
|
||||
{
|
||||
protected $foo;
|
||||
protected $bar;
|
||||
|
||||
public function __construct($foo = array(), $bar)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
class GetCamelizedDummy
|
||||
{
|
||||
private $kevinDunglas;
|
||||
private $fooBar;
|
||||
private $bar_foo;
|
||||
|
||||
public function __construct($kevinDunglas = null)
|
||||
{
|
||||
$this->kevinDunglas = $kevinDunglas;
|
||||
}
|
||||
|
||||
public function getKevinDunglas()
|
||||
{
|
||||
return $this->kevinDunglas;
|
||||
}
|
||||
|
||||
public function setFooBar($fooBar)
|
||||
{
|
||||
$this->fooBar = $fooBar;
|
||||
}
|
||||
|
||||
public function getFooBar()
|
||||
{
|
||||
return $this->fooBar;
|
||||
}
|
||||
|
||||
public function setBar_foo($bar_foo)
|
||||
{
|
||||
$this->bar_foo = $bar_foo;
|
||||
}
|
||||
|
||||
public function getBar_foo()
|
||||
{
|
||||
return $this->bar_foo;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectConstructorArgsWithPrivateMutatorDummy
|
||||
{
|
||||
private $foo;
|
||||
|
||||
public function __construct($foo)
|
||||
{
|
||||
$this->setFoo($foo);
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
private function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectWithPrivateSetterDummy
|
||||
{
|
||||
private $foo = 'bar';
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
private function setFoo($foo)
|
||||
{
|
||||
}
|
||||
}
|
605
vendor/symfony/serializer/Tests/Normalizer/ObjectNormalizerTest.php
vendored
Normal file
605
vendor/symfony/serializer/Tests/Normalizer/ObjectNormalizerTest.php
vendored
Normal file
|
@ -0,0 +1,605 @@
|
|||
<?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\Tests\Normalizer;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||
|
||||
/**
|
||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||
*/
|
||||
class ObjectNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var ObjectNormalizerTest
|
||||
*/
|
||||
private $normalizer;
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock(__NAMESPACE__.'\ObjectSerializerNormalizer');
|
||||
$this->normalizer = new ObjectNormalizer();
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new ObjectDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setFoo('foo');
|
||||
$obj->bar = 'bar';
|
||||
$obj->setBaz(true);
|
||||
$obj->setCamelCase('camelcase');
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->serializer
|
||||
->expects($this->once())
|
||||
->method('normalize')
|
||||
->with($object, 'any')
|
||||
->will($this->returnValue('string_object'))
|
||||
;
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'foo' => 'foo',
|
||||
'bar' => 'bar',
|
||||
'baz' => true,
|
||||
'fooBar' => 'foobar',
|
||||
'camelCase' => 'camelcase',
|
||||
'object' => 'string_object',
|
||||
),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\ObjectDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testDenormalizeWithObject()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\ObjectDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\ObjectDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testNameConverterSupport()
|
||||
{
|
||||
$this->normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\ObjectDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testDenormalizeNull()
|
||||
{
|
||||
$this->assertEquals(new ObjectDummy(), $this->normalizer->denormalize(null, __NAMESPACE__.'\ObjectDummy'));
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar'),
|
||||
__NAMESPACE__.'\ObjectConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithNullArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => null, 'baz' => true),
|
||||
__NAMESPACE__.'\ObjectConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertNull($obj->bar);
|
||||
$this->assertTrue($obj->isBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithMissingOptionalArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'test', 'baz' => array(1, 2, 3)),
|
||||
__NAMESPACE__.'\ObjectConstructorOptionalArgsDummy', 'any');
|
||||
$this->assertEquals('test', $obj->getFoo());
|
||||
$this->assertEquals(array(), $obj->bar);
|
||||
$this->assertEquals(array(1, 2, 3), $obj->getBaz());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithOptionalDefaultArgument()
|
||||
{
|
||||
if (PHP_VERSION_ID <= 50316) {
|
||||
$this->markTestSkipped('See https://bugs.php.net/62715');
|
||||
}
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('bar' => 'test'),
|
||||
__NAMESPACE__.'\ObjectConstructorArgsWithDefaultValueDummy', 'any');
|
||||
$this->assertEquals(array(), $obj->getFoo());
|
||||
$this->assertEquals('test', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testConstructorWithObjectDenormalize()
|
||||
{
|
||||
$data = new \stdClass();
|
||||
$data->foo = 'foo';
|
||||
$data->bar = 'bar';
|
||||
$data->baz = true;
|
||||
$data->fooBar = 'foobar';
|
||||
$obj = $this->normalizer->denormalize($data, __NAMESPACE__.'\ObjectConstructorDummy', 'any');
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->bar);
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setFooBar('fooBar');
|
||||
$obj->setSymfony('symfony');
|
||||
$obj->setKevin('kevin');
|
||||
$obj->setCoopTilleuls('coopTilleuls');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
||||
|
||||
$this->assertEquals(array(
|
||||
'symfony' => 'symfony',
|
||||
'foo' => 'foo',
|
||||
'fooBar' => 'fooBar',
|
||||
'bar' => 'bar',
|
||||
'kevin' => 'kevin',
|
||||
'coopTilleuls' => 'coopTilleuls',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
||||
}
|
||||
|
||||
public function testGroupsDenormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
|
||||
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
|
||||
$obj->setBar('bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a', 'b'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
}
|
||||
|
||||
public function testGroupsNormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
$obj->setCoopTilleuls('les-tilleuls.coop');
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
),
|
||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize(array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCallbacks
|
||||
*/
|
||||
public function testCallbacks($callbacks, $value, $result, $message)
|
||||
{
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
|
||||
$obj = new ObjectConstructorDummy('', $value, true);
|
||||
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
$this->normalizer->normalize($obj, 'any'),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUncallableCallbacks()
|
||||
{
|
||||
$this->normalizer->setCallbacks(array('bar' => null));
|
||||
|
||||
$obj = new ObjectConstructorDummy('baz', 'quux', true);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'baz', 'camelCase', 'object'));
|
||||
|
||||
$obj = new ObjectDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->bar = 'bar';
|
||||
$obj->setBaz(true);
|
||||
|
||||
$this->assertEquals(
|
||||
array('fooBar' => 'foobar'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function provideCallbacks()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return 'baz';
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => 'baz', 'baz' => true),
|
||||
'Change a string',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return;
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => null, 'baz' => true),
|
||||
'Null an item',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return $bar->format('d-m-Y H:i:s');
|
||||
},
|
||||
),
|
||||
new \DateTime('2011-09-10 06:30:00'),
|
||||
array('foo' => '', 'bar' => '10-09-2011 06:30:00', 'baz' => true),
|
||||
'Format a date',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
$foos = '';
|
||||
foreach ($bars as $bar) {
|
||||
$foos .= $bar->getFoo();
|
||||
}
|
||||
|
||||
return $foos;
|
||||
},
|
||||
),
|
||||
array(new ObjectConstructorDummy('baz', '', false), new ObjectConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 'bazquux', 'baz' => true),
|
||||
'Collect a property',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
return count($bars);
|
||||
},
|
||||
),
|
||||
array(new ObjectConstructorDummy('baz', '', false), new ObjectConstructorDummy('quux', '', false)),
|
||||
array('foo' => '', 'bar' => 2, 'baz' => true),
|
||||
'Count a property',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new ObjectDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setObject($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceLimit(2);
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$this->normalizer->normalize($obj);
|
||||
}
|
||||
|
||||
public function testSiblingReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$siblingHolder = new SiblingHolder();
|
||||
|
||||
$expected = array(
|
||||
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
);
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceHandler(function ($obj) {
|
||||
return get_class($obj);
|
||||
});
|
||||
|
||||
$obj = new CircularReferenceDummy();
|
||||
|
||||
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy');
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
||||
}
|
||||
|
||||
public function testDenormalizeNonExistingAttribute()
|
||||
{
|
||||
$this->assertEquals(
|
||||
new ObjectDummy(),
|
||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\ObjectDummy')
|
||||
);
|
||||
}
|
||||
|
||||
public function testNoTraversableSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectDummy
|
||||
{
|
||||
protected $foo;
|
||||
public $bar;
|
||||
private $baz;
|
||||
protected $camelCase;
|
||||
protected $object;
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function setFoo($foo)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function setBaz($baz)
|
||||
{
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFooBar()
|
||||
{
|
||||
return $this->foo.$this->bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
|
||||
public function setObject($object)
|
||||
{
|
||||
$this->object = $object;
|
||||
}
|
||||
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectConstructorDummy
|
||||
{
|
||||
protected $foo;
|
||||
public $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar, $baz)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function isBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ObjectSerializerNormalizer implements SerializerInterface, NormalizerInterface
|
||||
{
|
||||
}
|
||||
|
||||
class ObjectConstructorOptionalArgsDummy
|
||||
{
|
||||
protected $foo;
|
||||
public $bar;
|
||||
private $baz;
|
||||
|
||||
public function __construct($foo, $bar = array(), $baz = array())
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
$this->baz = $baz;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBaz()
|
||||
{
|
||||
return $this->baz;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectConstructorArgsWithDefaultValueDummy
|
||||
{
|
||||
protected $foo;
|
||||
protected $bar;
|
||||
|
||||
public function __construct($foo = array(), $bar)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function otherMethod()
|
||||
{
|
||||
throw new \RuntimeException('Dummy::otherMethod() should not be called');
|
||||
}
|
||||
}
|
493
vendor/symfony/serializer/Tests/Normalizer/PropertyNormalizerTest.php
vendored
Normal file
493
vendor/symfony/serializer/Tests/Normalizer/PropertyNormalizerTest.php
vendored
Normal file
|
@ -0,0 +1,493 @@
|
|||
<?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\Tests\Normalizer;
|
||||
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
|
||||
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
|
||||
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
|
||||
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\GroupDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\PropertySiblingHolder;
|
||||
|
||||
class PropertyNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var PropertyNormalizer
|
||||
*/
|
||||
private $normalizer;
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer = new PropertyNormalizer();
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
}
|
||||
|
||||
public function testNormalize()
|
||||
{
|
||||
$obj = new PropertyDummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->setBar('bar');
|
||||
$obj->setCamelCase('camelcase');
|
||||
$this->assertEquals(
|
||||
array('foo' => 'foo', 'bar' => 'bar', 'camelCase' => 'camelcase'),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar'),
|
||||
__NAMESPACE__.'\PropertyDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->foo);
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyDenormalizeOnCamelCaseFormat()
|
||||
{
|
||||
$this->normalizer->setCamelizedAttributes(array('camel_case'));
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'value'),
|
||||
__NAMESPACE__.'\PropertyDummy'
|
||||
);
|
||||
$this->assertEquals('value', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesNormalize()
|
||||
{
|
||||
$obj = new PropertyCamelizedDummy('dunglas.fr');
|
||||
$obj->fooBar = 'les-tilleuls.coop';
|
||||
$obj->bar_foo = 'lostinthesupermarket.fr';
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->normalize($obj), array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
public function testLegacyCamelizedAttributesDenormalize()
|
||||
{
|
||||
$obj = new PropertyCamelizedDummy('dunglas.fr');
|
||||
$obj->fooBar = 'les-tilleuls.coop';
|
||||
$obj->bar_foo = 'lostinthesupermarket.fr';
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('kevin_dunglas'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevin_dunglas' => 'dunglas.fr',
|
||||
'fooBar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\PropertyCamelizedDummy'), $obj);
|
||||
|
||||
$this->normalizer->setCamelizedAttributes(array('foo_bar'));
|
||||
$this->assertEquals($this->normalizer->denormalize(array(
|
||||
'kevinDunglas' => 'dunglas.fr',
|
||||
'foo_bar' => 'les-tilleuls.coop',
|
||||
'bar_foo' => 'lostinthesupermarket.fr',
|
||||
), __NAMESPACE__.'\PropertyCamelizedDummy'), $obj);
|
||||
}
|
||||
|
||||
public function testNameConverterSupport()
|
||||
{
|
||||
$this->normalizer = new PropertyNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('camel_case' => 'camelCase'),
|
||||
__NAMESPACE__.'\PropertyDummy'
|
||||
);
|
||||
$this->assertEquals('camelCase', $obj->getCamelCase());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalize()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => 'foo', 'bar' => 'bar'),
|
||||
__NAMESPACE__.'\PropertyConstructorDummy',
|
||||
'any'
|
||||
);
|
||||
$this->assertEquals('foo', $obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
public function testConstructorDenormalizeWithNullArgument()
|
||||
{
|
||||
$obj = $this->normalizer->denormalize(
|
||||
array('foo' => null, 'bar' => 'bar'),
|
||||
__NAMESPACE__.'\PropertyConstructorDummy', '
|
||||
any'
|
||||
);
|
||||
$this->assertNull($obj->getFoo());
|
||||
$this->assertEquals('bar', $obj->getBar());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideCallbacks
|
||||
*/
|
||||
public function testCallbacks($callbacks, $value, $result, $message)
|
||||
{
|
||||
$this->normalizer->setCallbacks($callbacks);
|
||||
|
||||
$obj = new PropertyConstructorDummy('', $value);
|
||||
|
||||
$this->assertEquals(
|
||||
$result,
|
||||
$this->normalizer->normalize($obj, 'any'),
|
||||
$message
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testUncallableCallbacks()
|
||||
{
|
||||
$this->normalizer->setCallbacks(array('bar' => null));
|
||||
|
||||
$obj = new PropertyConstructorDummy('baz', 'quux');
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testIgnoredAttributes()
|
||||
{
|
||||
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
|
||||
|
||||
$obj = new PropertyDummy();
|
||||
$obj->foo = 'foo';
|
||||
$obj->setBar('bar');
|
||||
|
||||
$this->assertEquals(
|
||||
array(),
|
||||
$this->normalizer->normalize($obj, 'any')
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsNormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
$obj->setBar('bar');
|
||||
$obj->setFooBar('fooBar');
|
||||
$obj->setSymfony('symfony');
|
||||
$obj->setKevin('kevin');
|
||||
$obj->setCoopTilleuls('coopTilleuls');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('c'))));
|
||||
|
||||
// The PropertyNormalizer is not able to hydrate properties from parent classes
|
||||
$this->assertEquals(array(
|
||||
'symfony' => 'symfony',
|
||||
'foo' => 'foo',
|
||||
'fooBar' => 'fooBar',
|
||||
'bar' => 'bar',
|
||||
), $this->normalizer->normalize($obj, null, array('groups' => array('a', 'c'))));
|
||||
}
|
||||
|
||||
public function testGroupsDenormalize()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory);
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFoo('foo');
|
||||
|
||||
$toNormalize = array('foo' => 'foo', 'bar' => 'bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
|
||||
$obj->setBar('bar');
|
||||
|
||||
$normalized = $this->normalizer->denormalize(
|
||||
$toNormalize,
|
||||
'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy',
|
||||
null,
|
||||
array('groups' => array('a', 'b'))
|
||||
);
|
||||
$this->assertEquals($obj, $normalized);
|
||||
}
|
||||
|
||||
public function testGroupsNormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
$obj->setCoopTilleuls('les-tilleuls.coop');
|
||||
|
||||
$this->assertEquals(
|
||||
array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
),
|
||||
$this->normalizer->normalize($obj, null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function testGroupsDenormalizeWithNameConverter()
|
||||
{
|
||||
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
|
||||
$this->normalizer = new PropertyNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
|
||||
$this->normalizer->setSerializer($this->serializer);
|
||||
|
||||
$obj = new GroupDummy();
|
||||
$obj->setFooBar('@dunglas');
|
||||
$obj->setSymfony('@coopTilleuls');
|
||||
|
||||
$this->assertEquals(
|
||||
$obj,
|
||||
$this->normalizer->denormalize(array(
|
||||
'bar' => null,
|
||||
'foo_bar' => '@dunglas',
|
||||
'symfony' => '@coopTilleuls',
|
||||
'coop_tilleuls' => 'les-tilleuls.coop',
|
||||
), 'Symfony\Component\Serializer\Tests\Fixtures\GroupDummy', null, array('groups' => array('name_converter')))
|
||||
);
|
||||
}
|
||||
|
||||
public function provideCallbacks()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return 'baz';
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => 'baz'),
|
||||
'Change a string',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return;
|
||||
},
|
||||
),
|
||||
'baz',
|
||||
array('foo' => '', 'bar' => null),
|
||||
'Null an item',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bar) {
|
||||
return $bar->format('d-m-Y H:i:s');
|
||||
},
|
||||
),
|
||||
new \DateTime('2011-09-10 06:30:00'),
|
||||
array('foo' => '', 'bar' => '10-09-2011 06:30:00'),
|
||||
'Format a date',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
$foos = '';
|
||||
foreach ($bars as $bar) {
|
||||
$foos .= $bar->getFoo();
|
||||
}
|
||||
|
||||
return $foos;
|
||||
},
|
||||
),
|
||||
array(new PropertyConstructorDummy('baz', ''), new PropertyConstructorDummy('quux', '')),
|
||||
array('foo' => '', 'bar' => 'bazquux'),
|
||||
'Collect a property',
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'bar' => function ($bars) {
|
||||
return count($bars);
|
||||
},
|
||||
),
|
||||
array(new PropertyConstructorDummy('baz', ''), new PropertyConstructorDummy('quux', '')),
|
||||
array('foo' => '', 'bar' => 2),
|
||||
'Count a property',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException
|
||||
*/
|
||||
public function testUnableToNormalizeCircularReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceLimit(2);
|
||||
|
||||
$obj = new PropertyCircularReferenceDummy();
|
||||
|
||||
$this->normalizer->normalize($obj);
|
||||
}
|
||||
|
||||
public function testSiblingReference()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$siblingHolder = new PropertySiblingHolder();
|
||||
|
||||
$expected = array(
|
||||
'sibling0' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling1' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
'sibling2' => array('coopTilleuls' => 'Les-Tilleuls.coop'),
|
||||
);
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($siblingHolder));
|
||||
}
|
||||
|
||||
public function testCircularReferenceHandler()
|
||||
{
|
||||
$serializer = new Serializer(array($this->normalizer));
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
$this->normalizer->setCircularReferenceHandler(function ($obj) {
|
||||
return get_class($obj);
|
||||
});
|
||||
|
||||
$obj = new PropertyCircularReferenceDummy();
|
||||
|
||||
$expected = array('me' => 'Symfony\Component\Serializer\Tests\Fixtures\PropertyCircularReferenceDummy');
|
||||
$this->assertEquals($expected, $this->normalizer->normalize($obj));
|
||||
}
|
||||
|
||||
public function testDenormalizeNonExistingAttribute()
|
||||
{
|
||||
$this->assertEquals(
|
||||
new PropertyDummy(),
|
||||
$this->normalizer->denormalize(array('non_existing' => true), __NAMESPACE__.'\PropertyDummy')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
* @expectedExceptionMessage Cannot normalize attribute "bar" because injected serializer is not a normalizer
|
||||
*/
|
||||
public function testUnableToNormalizeObjectAttribute()
|
||||
{
|
||||
$serializer = $this->getMock('Symfony\Component\Serializer\SerializerInterface');
|
||||
$this->normalizer->setSerializer($serializer);
|
||||
|
||||
$obj = new PropertyDummy();
|
||||
$object = new \stdClass();
|
||||
$obj->setBar($object);
|
||||
|
||||
$this->normalizer->normalize($obj, 'any');
|
||||
}
|
||||
|
||||
public function testNoTraversableSupport()
|
||||
{
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \ArrayObject()));
|
||||
}
|
||||
}
|
||||
|
||||
class PropertyDummy
|
||||
{
|
||||
public $foo;
|
||||
private $bar;
|
||||
protected $camelCase;
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
|
||||
public function setBar($bar)
|
||||
{
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getCamelCase()
|
||||
{
|
||||
return $this->camelCase;
|
||||
}
|
||||
|
||||
public function setCamelCase($camelCase)
|
||||
{
|
||||
$this->camelCase = $camelCase;
|
||||
}
|
||||
}
|
||||
|
||||
class PropertyConstructorDummy
|
||||
{
|
||||
protected $foo;
|
||||
private $bar;
|
||||
|
||||
public function __construct($foo, $bar)
|
||||
{
|
||||
$this->foo = $foo;
|
||||
$this->bar = $bar;
|
||||
}
|
||||
|
||||
public function getFoo()
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
|
||||
public function getBar()
|
||||
{
|
||||
return $this->bar;
|
||||
}
|
||||
}
|
||||
|
||||
class PropertyCamelizedDummy
|
||||
{
|
||||
private $kevinDunglas;
|
||||
public $fooBar;
|
||||
public $bar_foo;
|
||||
|
||||
public function __construct($kevinDunglas = null)
|
||||
{
|
||||
$this->kevinDunglas = $kevinDunglas;
|
||||
}
|
||||
}
|
37
vendor/symfony/serializer/Tests/Normalizer/TestDenormalizer.php
vendored
Normal file
37
vendor/symfony/serializer/Tests/Normalizer/TestDenormalizer.php
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?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\Tests\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
/**
|
||||
* Provides a test Normalizer which only implements the DenormalizerInterface.
|
||||
*
|
||||
* @author Lin Clark <lin@lin-clark.com>
|
||||
*/
|
||||
class TestDenormalizer implements DenormalizerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = null, array $context = array())
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
37
vendor/symfony/serializer/Tests/Normalizer/TestNormalizer.php
vendored
Normal file
37
vendor/symfony/serializer/Tests/Normalizer/TestNormalizer.php
vendored
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?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\Tests\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* Provides a test Normalizer which only implements the NormalizerInterface.
|
||||
*
|
||||
* @author Lin Clark <lin@lin-clark.com>
|
||||
*/
|
||||
class TestNormalizer implements NormalizerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($object, $format = null, array $context = array())
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
267
vendor/symfony/serializer/Tests/SerializerTest.php
vendored
Normal file
267
vendor/symfony/serializer/Tests/SerializerTest.php
vendored
Normal file
|
@ -0,0 +1,267 @@
|
|||
<?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\Tests;
|
||||
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
||||
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
|
||||
use Symfony\Component\Serializer\Tests\Fixtures\NormalizableTraversableDummy;
|
||||
use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
|
||||
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
|
||||
|
||||
class SerializerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testInterface()
|
||||
{
|
||||
$serializer = new Serializer();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\SerializerInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\NormalizerInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Normalizer\DenormalizerInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Encoder\EncoderInterface', $serializer);
|
||||
$this->assertInstanceOf('Symfony\Component\Serializer\Encoder\DecoderInterface', $serializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testNormalizeNoMatch()
|
||||
{
|
||||
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
|
||||
$this->serializer->normalize(new \stdClass(), 'xml');
|
||||
}
|
||||
|
||||
public function testNormalizeTraversable()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$result = $this->serializer->serialize(new TraversableDummy(), 'json');
|
||||
$this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
|
||||
}
|
||||
|
||||
public function testNormalizeGivesPriorityToInterfaceOverTraversable()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
|
||||
$result = $this->serializer->serialize(new NormalizableTraversableDummy(), 'json');
|
||||
$this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testNormalizeOnDenormalizer()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new TestDenormalizer()), array());
|
||||
$this->assertTrue($this->serializer->normalize(new \stdClass(), 'json'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDenormalizeNoMatch()
|
||||
{
|
||||
$this->serializer = new Serializer(array($this->getMock('Symfony\Component\Serializer\Normalizer\CustomNormalizer')));
|
||||
$this->serializer->denormalize('foo', 'stdClass');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDenormalizeOnNormalizer()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new TestNormalizer()), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertTrue($this->serializer->denormalize(json_encode($data), 'stdClass', 'json'));
|
||||
}
|
||||
|
||||
public function testCustomNormalizerCanNormalizeCollectionsAndScalar()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new TestNormalizer()), array());
|
||||
$this->assertNull($this->serializer->normalize(array('a', 'b')));
|
||||
$this->assertNull($this->serializer->normalize(new \ArrayObject(array('c', 'd'))));
|
||||
$this->assertNull($this->serializer->normalize(array()));
|
||||
$this->assertNull($this->serializer->normalize('test'));
|
||||
}
|
||||
|
||||
public function testSerialize()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$result = $this->serializer->serialize(Model::fromArray($data), 'json');
|
||||
$this->assertEquals(json_encode($data), $result);
|
||||
}
|
||||
|
||||
public function testSerializeScalar()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$result = $this->serializer->serialize('foo', 'json');
|
||||
$this->assertEquals('"foo"', $result);
|
||||
}
|
||||
|
||||
public function testSerializeArrayOfScalars()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('foo', array(5, 3));
|
||||
$result = $this->serializer->serialize($data, 'json');
|
||||
$this->assertEquals(json_encode($data), $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testSerializeNoEncoder()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->serialize($data, 'json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
*/
|
||||
public function testSerializeNoNormalizer()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->serialize(Model::fromArray($data), 'json');
|
||||
}
|
||||
|
||||
public function testDeserialize()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$result = $this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
$this->assertEquals($data, $result->toArray());
|
||||
}
|
||||
|
||||
public function testDeserializeUseCache()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
$data = array('title' => 'bar', 'numbers' => array(2, 8));
|
||||
$result = $this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
$this->assertEquals($data, $result->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\LogicException
|
||||
*/
|
||||
public function testDeserializeNoNormalizer()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDeserializeWrongNormalizer()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDeserializeNoEncoder()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->serializer->deserialize(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json');
|
||||
}
|
||||
|
||||
public function testDeserializeSupported()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertTrue($this->serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
|
||||
}
|
||||
|
||||
public function testDeserializeNotSupported()
|
||||
{
|
||||
$this->serializer = new Serializer(array(new GetSetMethodNormalizer()), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertFalse($this->serializer->supportsDenormalization(json_encode($data), 'stdClass', 'json'));
|
||||
}
|
||||
|
||||
public function testDeserializeNotSupportedMissing()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array());
|
||||
$data = array('title' => 'foo', 'numbers' => array(5, 3));
|
||||
$this->assertFalse($this->serializer->supportsDenormalization(json_encode($data), '\Symfony\Component\Serializer\Tests\Model', 'json'));
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('foo', array(5, 3));
|
||||
$result = $this->serializer->encode($data, 'json');
|
||||
$this->assertEquals(json_encode($data), $result);
|
||||
}
|
||||
|
||||
public function testDecode()
|
||||
{
|
||||
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
|
||||
$data = array('foo', array(5, 3));
|
||||
$result = $this->serializer->decode(json_encode($data), 'json');
|
||||
$this->assertEquals($data, $result);
|
||||
}
|
||||
}
|
||||
|
||||
class Model
|
||||
{
|
||||
private $title;
|
||||
private $numbers;
|
||||
|
||||
public static function fromArray($array)
|
||||
{
|
||||
$model = new self();
|
||||
if (isset($array['title'])) {
|
||||
$model->setTitle($array['title']);
|
||||
}
|
||||
if (isset($array['numbers'])) {
|
||||
$model->setNumbers($array['numbers']);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function getNumbers()
|
||||
{
|
||||
return $this->numbers;
|
||||
}
|
||||
|
||||
public function setNumbers($numbers)
|
||||
{
|
||||
$this->numbers = $numbers;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return array('title' => $this->title, 'numbers' => $this->numbers);
|
||||
}
|
||||
}
|
45
vendor/symfony/serializer/composer.json
vendored
Normal file
45
vendor/symfony/serializer/composer.json
vendored
Normal file
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
"name": "symfony/serializer",
|
||||
"type": "library",
|
||||
"description": "Symfony Serializer Component",
|
||||
"keywords": [],
|
||||
"homepage": "https://symfony.com",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.3.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"symfony/phpunit-bridge": "~2.7",
|
||||
"symfony/yaml": "~2.0,>=2.0.5",
|
||||
"symfony/config": "~2.2",
|
||||
"symfony/property-access": "~2.3",
|
||||
"doctrine/annotations": "~1.0",
|
||||
"doctrine/cache": "~1.0"
|
||||
},
|
||||
"suggest": {
|
||||
"doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.",
|
||||
"doctrine/cache": "For using the default cached annotation reader and metadata cache.",
|
||||
"symfony/yaml": "For using the default YAML mapping loader.",
|
||||
"symfony/config": "For using the XML mapping loader.",
|
||||
"symfony/property-access": "For using the ObjectNormalizer."
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": { "Symfony\\Component\\Serializer\\": "" }
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.7-dev"
|
||||
}
|
||||
}
|
||||
}
|
28
vendor/symfony/serializer/phpunit.xml.dist
vendored
Normal file
28
vendor/symfony/serializer/phpunit.xml.dist
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
colors="true"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
<php>
|
||||
<ini name="error_reporting" value="-1" />
|
||||
</php>
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Symfony Serializer Component Test Suite">
|
||||
<directory>./Tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./</directory>
|
||||
<exclude>
|
||||
<directory>./vendor</directory>
|
||||
<directory>./Tests</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
Reference in a new issue