2011-01-18 19:17:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-03-31 22:00:32 +01:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-18 19:17:24 +00:00
|
|
|
*
|
2012-03-31 22:00:32 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-02 04:52:14 +01:00
|
|
|
namespace Symfony\Component\Serializer\Normalizer;
|
|
|
|
|
2016-01-26 12:57:16 +00:00
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface;
|
|
|
|
use Symfony\Component\Serializer\SerializerAwareTrait;
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*/
|
2018-04-25 16:25:58 +01:00
|
|
|
class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface, NormalizerWithCacheableSupportResultInterface
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2017-02-22 15:41:23 +00:00
|
|
|
use ObjectToPopulateTrait;
|
2016-01-26 12:57:16 +00:00
|
|
|
use SerializerAwareTrait;
|
|
|
|
|
2017-02-22 15:41:23 +00:00
|
|
|
private $cache = array();
|
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-01-18 13:08:59 +00:00
|
|
|
public function normalize($object, $format = null, array $context = array())
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2013-01-18 13:08:59 +00:00
|
|
|
return $object->normalize($this->serializer, $format, $context);
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2013-01-18 13:08:59 +00:00
|
|
|
public function denormalize($data, $class, $format = null, array $context = array())
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2017-02-22 15:41:23 +00:00
|
|
|
$object = $this->extractObjectToPopulate($class, $context) ?: new $class();
|
2013-01-18 13:08:59 +00:00
|
|
|
$object->denormalize($this->serializer, $data, $format, $context);
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-01-18 19:17:24 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given class implements the NormalizableInterface.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @param mixed $data Data to normalize
|
|
|
|
* @param string $format The format being (de-)serialized from or into
|
2012-11-16 16:06:12 +00:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2011-01-18 19:17:24 +00:00
|
|
|
*/
|
2011-05-06 18:36:56 +01:00
|
|
|
public function supportsNormalization($data, $format = null)
|
2011-01-18 19:17:24 +00:00
|
|
|
{
|
2011-05-06 18:36:56 +01:00
|
|
|
return $data instanceof NormalizableInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-28 00:21:22 +00:00
|
|
|
* Checks if the given class implements the DenormalizableInterface.
|
2011-05-06 18:36:56 +01:00
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @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
|
2012-11-16 16:06:12 +00:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool
|
2011-05-06 18:36:56 +01:00
|
|
|
*/
|
|
|
|
public function supportsDenormalization($data, $type, $format = null)
|
|
|
|
{
|
2017-09-15 22:40:40 +01:00
|
|
|
if (isset($this->cache[$type])) {
|
|
|
|
return $this->cache[$type];
|
|
|
|
}
|
|
|
|
|
2015-04-13 22:07:23 +01:00
|
|
|
if (!class_exists($type)) {
|
2017-09-15 22:40:40 +01:00
|
|
|
return $this->cache[$type] = false;
|
2015-04-13 22:07:23 +01:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:40:40 +01:00
|
|
|
return $this->cache[$type] = is_subclass_of($type, 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface');
|
2011-01-18 19:17:24 +00:00
|
|
|
}
|
|
|
|
}
|