Commit ab6b52f2 authored by Dries's avatar Dries

Issue #1810472 by linclark, mradcliffe: add Symfony's Serializer component to...

Issue #1810472 by linclark, mradcliffe: add Symfony's Serializer component to core despite Symfony potentially releasing BC-breaking updates after 2.3.
parent 1f37e575
......@@ -9,6 +9,7 @@
"symfony/http-foundation": "2.1.*",
"symfony/http-kernel": "2.1.*",
"symfony/routing": "2.1.*",
"symfony/serializer": "2.1.*",
"symfony/yaml": "2.1.*",
"twig/twig": "1.8.*",
"doctrine/common": "2.3.*",
......
......@@ -33,6 +33,10 @@
"package": "symfony/routing",
"version": "v2.1.0-RC2"
},
{
"package": "symfony/serializer",
"version": "v2.1.2"
},
{
"package": "symfony/yaml",
"version": "v2.1.0-RC2"
......
......@@ -8,6 +8,7 @@
return array(
'Twig_' => $vendorDir . '/twig/twig/lib/',
'Symfony\\Component\\Yaml' => $vendorDir . '/symfony/yaml/',
'Symfony\\Component\\Serializer' => $vendorDir . '/symfony/serializer/',
'Symfony\\Component\\Routing' => $vendorDir . '/symfony/routing/',
'Symfony\\Component\\Process' => $vendorDir . '/symfony/process/',
'Symfony\\Component\\HttpKernel' => $vendorDir . '/symfony/http-kernel/',
......
CHANGELOG
=========
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
)
)
)
<?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\Encoder\DecoderInterface;
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)
{
return $this->getDecoder($format)->decode($data, $format);
}
/**
* {@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));
}
}
<?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\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
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)
{
return $this->getEncoder($format)->encode($data, $format);
}
/**
* {@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 Boolean
*/
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));
}
}
<?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 decoders
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
interface DecoderInterface
{
/**
* Decodes a string into PHP data
*
* @param scalar $data Data to decode
* @param string $format Format name
*
* @return mixed
*/
public function decode($data, $format);
/**
* Checks whether the serializer can decode from given format
*
* @param string $format format name
* @return Boolean
*/
public function supportsDecoding($format);
}
<?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
*
* @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
*
* @return scalar
*/
public function encode($data, $format);
/**
* Checks whether the serializer can encode to given format
*
* @param string $format format name
* @return Boolean
*/
public function supportsEncoding($format);
}
<?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;
/**
* Decodes JSON data
*
* @author Sander Coolen <sander@jibber.nl>
*/
class JsonDecode implements DecoderInterface
{
private $associative;
private $recursionDepth;
private $lastError = JSON_ERROR_NONE;
public function __construct($associative = false, $depth = 512)
{
$this->associative = $associative;
$this->recursionDepth = $depth;
}
/**
* Returns the last decoding error (if any)
*
* @return integer
*
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
*/
public function getLastError()
{
return $this->lastError;
}
/**
* Decodes a JSON string into PHP data
*
* @param string $data JSON
*
* @return mixed
*/
public function decode($data, $format)
{
$decodedData = json_decode($data, $this->associative, $this->recursionDepth);
$this->lastError = json_last_error();
return $decodedData;
}
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
return JsonEncoder::FORMAT === $format;
}
}
<?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 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 integer
*
* @see http://php.net/manual/en/function.json-last-error.php json_last_error
*/
public function getLastError()
{
return $this->lastError;
}
/**
* Encodes PHP data to a JSON string
*
* @param mixed $data
*
* @return string
*/
public function encode($data, $format)
{
$encodedJson = json_encode($data, $this->options);
$this->lastError = json_last_error();
return $encodedJson;
}
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return JsonEncoder::FORMAT === $format;
}
}
<?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 = null === $encodingImpl ? new JsonEncode() : $encodingImpl;
$this->decodingImpl = null === $decodingImpl ? new JsonDecode(true) : $decodingImpl;
}
/**
* Returns the last encoding error (if any)
*
* @return integer
*/
public function getLastEncodingError()
{
return $this->encodingImpl->getLastError();
}
/**
* Returns the last decoding error (if any)
*
* @return integer
*/
public function getLastDecodingError()
{
return $this->decodingImpl->getLastError();
}
/**
* {@inheritdoc}
*/
public function encode($data, $format)
{
return $this->encodingImpl->encode($data, self::FORMAT);
}
/**
* {@inheritdoc}
*/
public function decode($data, $format)
{
return $this->decodingImpl->decode($data, self::FORMAT);
}
/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return self::FORMAT === $format;
}
/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
return self::FORMAT === $format;
}
}
<?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
{
}
<?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;
}
}
<?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
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface Exception
{
}
<?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 Exception
{
}
<?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
*