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

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.  * @final since 2.8
  24.  */
  25. class JMSSerializerAdapter implements Serializer
  26. {
  27.     /**
  28.      * @internal
  29.      */
  30.     const SERIALIZATION 0;
  31.     /**
  32.      * @internal
  33.      */
  34.     const DESERIALIZATION 1;
  35.     private $serializer;
  36.     private $serializationContextFactory;
  37.     private $deserializationContextFactory;
  38.     public function __construct(
  39.         SerializerInterface $serializer,
  40.         SerializationContextFactoryInterface $serializationContextFactory null,
  41.         DeserializationContextFactoryInterface $deserializationContextFactory null
  42.     ) {
  43.         $this->serializer $serializer;
  44.         $this->serializationContextFactory $serializationContextFactory;
  45.         $this->deserializationContextFactory $deserializationContextFactory;
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function serialize($data$formatContext $context)
  51.     {
  52.         $context $this->convertContext($contextself::SERIALIZATION);
  53.         return $this->serializer->serialize($data$format$context);
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function deserialize($data$type$formatContext $context)
  59.     {
  60.         $context $this->convertContext($contextself::DESERIALIZATION);
  61.         return $this->serializer->deserialize($data$type$format$context);
  62.     }
  63.     private function convertContext(Context $contextint $direction): JMSContext
  64.     {
  65.         if (self::SERIALIZATION === $direction) {
  66.             $jmsContext $this->serializationContextFactory
  67.                 $this->serializationContextFactory->createSerializationContext()
  68.                 : SerializationContext::create();
  69.         } else {
  70.             $jmsContext $this->deserializationContextFactory
  71.                 $this->deserializationContextFactory->createDeserializationContext()
  72.                 : DeserializationContext::create();
  73.             $maxDepth $context->getMaxDepth(false);
  74.             if (null !== $maxDepth) {
  75.                 for ($i 0$i $maxDepth; ++$i) {
  76.                     $jmsContext->increaseDepth();
  77.                 }
  78.             }
  79.         }
  80.         foreach ($context->getAttributes() as $key => $value) {
  81.             $jmsContext->setAttribute($key$value);
  82.         }
  83.         if (null !== $context->getVersion()) {
  84.             $jmsContext->setVersion($context->getVersion());
  85.         }
  86.         if (null !== $context->getGroups()) {
  87.             $jmsContext->setGroups($context->getGroups());
  88.         }
  89.         if (null !== $context->getMaxDepth(false) || true === $context->isMaxDepthEnabled()) {
  90.             $jmsContext->enableMaxDepthChecks();
  91.         }
  92.         if (null !== $context->getSerializeNull()) {
  93.             $jmsContext->setSerializeNull($context->getSerializeNull());
  94.         }
  95.         foreach ($context->getExclusionStrategies() as $strategy) {
  96.             $jmsContext->addExclusionStrategy($strategy);
  97.         }
  98.         return $jmsContext;
  99.     }
  100. }