2015-08-17 17:00:26 -07:00
< ? 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 ;
2018-11-23 12:29:20 +00:00
use Symfony\Component\Serializer\SerializerAwareInterface ;
use Symfony\Component\Serializer\SerializerAwareTrait ;
2015-08-17 17:00:26 -07:00
/**
* @ author Jordi Boggiano < j . boggiano @ seld . be >
*/
2018-11-23 12:29:20 +00:00
class CustomNormalizer implements NormalizerInterface , DenormalizerInterface , SerializerAwareInterface
2015-08-17 17:00:26 -07:00
{
2018-11-23 12:29:20 +00:00
use ObjectToPopulateTrait ;
use SerializerAwareTrait ;
private $cache = array ();
2015-08-17 17:00:26 -07:00
/**
* { @ 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 ())
{
2018-11-23 12:29:20 +00:00
$object = $this -> extractObjectToPopulate ( $class , $context ) ? : new $class ();
2015-08-17 17:00:26 -07:00
$object -> denormalize ( $this -> serializer , $data , $format , $context );
return $object ;
}
/**
* Checks if the given class implements the NormalizableInterface .
*
2017-02-02 16:28:38 -08:00
* @ param mixed $data Data to normalize
* @ param string $format The format being ( de - ) serialized from or into
2015-08-17 17:00:26 -07:00
*
* @ return bool
*/
public function supportsNormalization ( $data , $format = null )
{
return $data instanceof NormalizableInterface ;
}
/**
2018-11-23 12:29:20 +00:00
* Checks if the given class implements the DenormalizableInterface .
2015-08-17 17:00:26 -07:00
*
2017-02-02 16:28:38 -08: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
2015-08-17 17:00:26 -07:00
*
* @ return bool
*/
public function supportsDenormalization ( $data , $type , $format = null )
{
2018-11-23 12:29:20 +00:00
if ( isset ( $this -> cache [ $type ])) {
return $this -> cache [ $type ];
}
2016-04-20 09:56:34 -07:00
if ( ! class_exists ( $type )) {
2018-11-23 12:29:20 +00:00
return $this -> cache [ $type ] = false ;
2016-04-20 09:56:34 -07:00
}
2015-08-17 17:00:26 -07:00
2018-11-23 12:29:20 +00:00
return $this -> cache [ $type ] = is_subclass_of ( $type , 'Symfony\Component\Serializer\Normalizer\DenormalizableInterface' );
2015-08-17 17:00:26 -07:00
}
}