vendor/friendsofsymfony/rest-bundle/Serializer/JMSSerializerAdapter.php line 83

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\RestBundle\Serializer;
  11. use FOS\RestBundle\Context\Context;
  12. use JMS\Serializer\Context as JMSContext;
  13. use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
  14. use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
  15. use JMS\Serializer\DeserializationContext;
  16. use JMS\Serializer\SerializationContext;
  17. use JMS\Serializer\SerializerInterface;
  18. /**
  19.  * Adapter to plug the JMS serializer into the FOSRestBundle Serializer API.
  20.  *
  21.  * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  22.  */
  23. class JMSSerializerAdapter implements Serializer
  24. {
  25.     /**
  26.      * @internal
  27.      */
  28.     const SERIALIZATION 0;
  29.     /**
  30.      * @internal
  31.      */
  32.     const DESERIALIZATION 1;
  33.     private $serializer;
  34.     private $serializationContextFactory;
  35.     private $deserializationContextFactory;
  36.     public function __construct(
  37.         SerializerInterface $serializer,
  38.         SerializationContextFactoryInterface $serializationContextFactory null,
  39.         DeserializationContextFactoryInterface $deserializationContextFactory null
  40.     ) {
  41.         $this->serializer $serializer;
  42.         $this->serializationContextFactory $serializationContextFactory;
  43.         $this->deserializationContextFactory $deserializationContextFactory;
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function serialize($data$formatContext $context)
  49.     {
  50.         $context $this->convertContext($contextself::SERIALIZATION);
  51.         return $this->serializer->serialize($data$format$context);
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function deserialize($data$type$formatContext $context)
  57.     {
  58.         $context $this->convertContext($contextself::DESERIALIZATION);
  59.         return $this->serializer->deserialize($data$type$format$context);
  60.     }
  61.     /**
  62.      * @param Context $context
  63.      * @param int     $direction {@see self} constants
  64.      *
  65.      * @return JMSContext
  66.      */
  67.     private function convertContext(Context $context$direction)
  68.     {
  69.         if (self::SERIALIZATION === $direction) {
  70.             $jmsContext $this->serializationContextFactory
  71.                 $this->serializationContextFactory->createSerializationContext()
  72.                 : SerializationContext::create();
  73.         } else {
  74.             $jmsContext $this->deserializationContextFactory
  75.                 $this->deserializationContextFactory->createDeserializationContext()
  76.                 : DeserializationContext::create();
  77.             $maxDepth $context->getMaxDepth(false);
  78.             if (null !== $maxDepth) {
  79.                 for ($i 0$i $maxDepth; ++$i) {
  80.                     $jmsContext->increaseDepth();
  81.                 }
  82.             }
  83.         }
  84.         foreach ($context->getAttributes() as $key => $value) {
  85.             $jmsContext->setAttribute($key$value);
  86.         }
  87.         if (null !== $context->getVersion()) {
  88.             $jmsContext->setVersion($context->getVersion());
  89.         }
  90.         if (null !== $context->getGroups()) {
  91.             $jmsContext->setGroups($context->getGroups());
  92.         }
  93.         if (null !== $context->getMaxDepth(false) || null !== $context->isMaxDepthEnabled()) {
  94.             $jmsContext->enableMaxDepthChecks();
  95.         }
  96.         if (null !== $context->getSerializeNull()) {
  97.             $jmsContext->setSerializeNull($context->getSerializeNull());
  98.         }
  99.         foreach ($context->getExclusionStrategies() as $strategy) {
  100.             $jmsContext->addExclusionStrategy($strategy);
  101.         }
  102.         return $jmsContext;
  103.     }
  104. }