vendor/jms/serializer-bundle/ContextFactory/ConfiguredContextFactory.php line 101

Open in your IDE?
  1. <?php
  2. namespace JMS\SerializerBundle\ContextFactory;
  3. use JMS\Serializer\Context;
  4. use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
  5. use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
  6. use JMS\Serializer\DeserializationContext;
  7. use JMS\Serializer\SerializationContext;
  8. /**
  9.  * Class ConfiguredContextFactory
  10.  */
  11. class ConfiguredContextFactory implements SerializationContextFactoryInterfaceDeserializationContextFactoryInterface
  12. {
  13.     /**
  14.      * Application version
  15.      *
  16.      * @var null|string
  17.      */
  18.     private $version;
  19.     /**
  20.      * Flag if we should serialize null values
  21.      *
  22.      * @var bool
  23.      */
  24.     private $serializeNulls;
  25.     /**
  26.      * Flag if we should enable the max depth exclusion strategy
  27.      *
  28.      * @var bool
  29.      */
  30.     private $enableMaxDepthChecks false;
  31.     /**
  32.      * Key-value pairs with custom attributes
  33.      *
  34.      * @var array
  35.      */
  36.     private $attributes = array();
  37.     /**
  38.      * Serialization groups
  39.      *
  40.      * @var string[]
  41.      */
  42.     private $groups = array();
  43.     /**
  44.      * @param null|string $version
  45.      */
  46.     public function setVersion($version)
  47.     {
  48.         $this->version $version;
  49.     }
  50.     /**
  51.      * @param bool $serializeNulls
  52.      */
  53.     public function setSerializeNulls($serializeNulls)
  54.     {
  55.         $this->serializeNulls = (bool)$serializeNulls;
  56.     }
  57.     public function enableMaxDepthChecks()
  58.     {
  59.         $this->enableMaxDepthChecks true;
  60.     }
  61.     /**
  62.      * @param array $attributes
  63.      */
  64.     public function setAttributes(array $attributes)
  65.     {
  66.         $this->attributes $attributes;
  67.     }
  68.     /**
  69.      * @param string[] $groups
  70.      */
  71.     public function setGroups(array $groups)
  72.     {
  73.         $this->groups $groups;
  74.     }
  75.     /**
  76.      * @inheritDoc
  77.      */
  78.     public function createDeserializationContext()
  79.     {
  80.         return $this->configureContext(new DeserializationContext());
  81.     }
  82.     /**
  83.      * @inheritDoc
  84.      */
  85.     public function createSerializationContext()
  86.     {
  87.         return $this->configureContext(new SerializationContext());
  88.     }
  89.     /**
  90.      * Configures context according to configuration
  91.      *
  92.      * @param Context $context The context
  93.      *
  94.      * @return Context Given object
  95.      */
  96.     private function configureContext(Context $context)
  97.     {
  98.         foreach ($this->attributes as $key => $value) {
  99.             $context->setAttribute($key$value);
  100.         }
  101.         if (!empty($this->groups)) {
  102.             $context->setGroups($this->groups);
  103.         }
  104.         if ($this->serializeNulls !== null) {
  105.             $context->setSerializeNull($this->serializeNulls);
  106.         }
  107.         if ($this->enableMaxDepthChecks === true) {
  108.             $context->enableMaxDepthChecks();
  109.         }
  110.         if ($this->version !== null) {
  111.             $context->setVersion($this->version);
  112.         }
  113.         return $context;
  114.     }
  115. }