[Serializer] Split supports in supportsNormalization and supportsDenormalization
This commit is contained in:
parent
919f16a7d6
commit
ded30a2937
@ -124,6 +124,9 @@ beta1 to beta2
|
||||
'allow_delete' => true,
|
||||
));
|
||||
|
||||
* Serializer: The NormalizerInterface's `supports()` method has been split in
|
||||
two methods: `supportsNormalization` and `supportsDenormalization`.
|
||||
|
||||
PR12 to beta1
|
||||
-------------
|
||||
|
||||
|
@ -39,13 +39,27 @@ class CustomNormalizer extends AbstractNormalizer
|
||||
/**
|
||||
* Checks if the given class implements the NormalizableInterface.
|
||||
*
|
||||
* @param ReflectionClass $class A ReflectionClass instance of the class
|
||||
* to serialize into or from.
|
||||
* @param string $format The format being (de-)serialized from or into.
|
||||
* @param mixed $data Data to normalize.
|
||||
* @param string $format The format being (de-)serialized from or into.
|
||||
* @return Boolean
|
||||
*/
|
||||
public function supports(\ReflectionClass $class, $format = null)
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return $class->implementsInterface('Symfony\Component\Serializer\Normalizer\NormalizableInterface');
|
||||
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 Boolean
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = null)
|
||||
{
|
||||
$class = new \ReflectionClass($type);
|
||||
|
||||
return $class->isSubclassOf('Symfony\Component\Serializer\Normalizer\NormalizableInterface');
|
||||
}
|
||||
}
|
||||
|
@ -107,23 +107,37 @@ class GetSetMethodNormalizer extends AbstractNormalizer
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = null)
|
||||
{
|
||||
return $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 ReflectionClass $class A ReflectionClass instance of the class
|
||||
* to serialize into or from.
|
||||
* @param string $format The format being (de-)serialized from or into.
|
||||
* @return Boolean Whether the class has any getters.
|
||||
* @param string $class
|
||||
* @return Boolean
|
||||
*/
|
||||
public function supports(\ReflectionClass $class, $format = null)
|
||||
private function supports($class)
|
||||
{
|
||||
$class = new \ReflectionClass($type);
|
||||
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||
foreach ($methods as $method) {
|
||||
if ($this->isGetMethod($method)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -43,16 +43,25 @@ interface NormalizerInterface
|
||||
function denormalize($data, $class, $format = null);
|
||||
|
||||
/**
|
||||
* Checks whether the given class is supported by this normalizer
|
||||
*
|
||||
* @param ReflectionClass $class
|
||||
* @param string $format format the given data was extracted from
|
||||
* 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 Boolean
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
function supports(\ReflectionClass $class, $format = null);
|
||||
function supportsNormalization($data, $format = null);
|
||||
|
||||
/**
|
||||
* 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 Boolean
|
||||
* @api
|
||||
*/
|
||||
function supportsDenormalization($data, $type, $format = null);
|
||||
|
||||
/**
|
||||
* Sets the owning Serializer object
|
||||
|
@ -31,6 +31,7 @@ class Serializer implements SerializerInterface
|
||||
private $normalizers = array();
|
||||
private $encoders = array();
|
||||
protected $normalizerCache = array();
|
||||
protected $denormalizerCache = array();
|
||||
|
||||
/**
|
||||
* @param mixed $value value to test
|
||||
@ -68,9 +69,8 @@ class Serializer implements SerializerInterface
|
||||
if (isset($this->normalizerCache[$class][$format])) {
|
||||
return $this->normalizerCache[$class][$format]->normalize($object, $format, $properties);
|
||||
}
|
||||
$reflClass = new \ReflectionClass($class);
|
||||
foreach ($this->normalizers as $normalizer) {
|
||||
if ($normalizer->supports($reflClass, $format)) {
|
||||
if ($normalizer->supportsNormalization($object, $class, $format)) {
|
||||
$this->normalizerCache[$class][$format] = $normalizer;
|
||||
return $normalizer->normalize($object, $format, $properties);
|
||||
}
|
||||
@ -86,13 +86,12 @@ class Serializer implements SerializerInterface
|
||||
if (!$this->normalizers) {
|
||||
throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
|
||||
}
|
||||
if (isset($this->normalizerCache[$class][$format])) {
|
||||
return $this->normalizerCache[$class][$format]->denormalize($data, $class, $format);
|
||||
if (isset($this->denormalizerCache[$class][$format])) {
|
||||
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
|
||||
}
|
||||
$reflClass = new \ReflectionClass($class);
|
||||
foreach ($this->normalizers as $normalizer) {
|
||||
if ($normalizer->supports($reflClass, $format)) {
|
||||
$this->normalizerCache[$class][$format] = $normalizer;
|
||||
if ($normalizer->supportsDenormalization($class, $format)) {
|
||||
$this->denormalizerCache[$class][$format] = $normalizer;
|
||||
return $normalizer->denormalize($data, $class, $format);
|
||||
}
|
||||
}
|
||||
@ -107,22 +106,22 @@ class Serializer implements SerializerInterface
|
||||
if (!$this->isStructuredType($data)) {
|
||||
return $data;
|
||||
}
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $key => $val) {
|
||||
$data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
if ($data instanceof Traversable) {
|
||||
$normalized = array();
|
||||
foreach ($data as $key => $val) {
|
||||
$normalized[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
|
||||
$normalized[$key] = $this->normalize($val, $format);
|
||||
}
|
||||
return $normalized;
|
||||
}
|
||||
if (is_object($data)) {
|
||||
return $this->normalizeObject($data, $format);
|
||||
}
|
||||
if (is_array($data)) {
|
||||
foreach ($data as $key => $val) {
|
||||
$data[$key] = $this->normalize($val, $format);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,15 @@ class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertNull($obj->xmlFoo);
|
||||
}
|
||||
|
||||
public function testSupports()
|
||||
public function testSupportsNormalization()
|
||||
{
|
||||
$this->assertTrue($this->normalizer->supports(new \ReflectionClass(get_class(new ScalarDummy))));
|
||||
$this->assertFalse($this->normalizer->supports(new \ReflectionClass('stdClass')));
|
||||
$this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy));
|
||||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass));
|
||||
}
|
||||
|
||||
public function testSupportsDenormalization()
|
||||
{
|
||||
$this->assertTrue($this->normalizer->supportsDenormalization(array(), 'Symfony\Tests\Component\Serializer\Fixtures\ScalarDummy'));
|
||||
$this->assertFalse($this->normalizer->supportsDenormalization(array(), 'stdClass'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user