vendor/jms/serializer/src/JMS/Serializer/SerializationContext.php line 123

Open in your IDE?
  1. <?php
  2. namespace JMS\Serializer;
  3. use JMS\Serializer\Exception\RuntimeException;
  4. use Metadata\MetadataFactoryInterface;
  5. class SerializationContext extends Context
  6. {
  7.     /** @var \SplObjectStorage */
  8.     private $visitingSet;
  9.     /** @var \SplStack */
  10.     private $visitingStack;
  11.     /**
  12.      * @var string
  13.      */
  14.     private $initialType;
  15.     public static function create()
  16.     {
  17.         return new self();
  18.     }
  19.     /**
  20.      * @param string $format
  21.      */
  22.     public function initialize($formatVisitorInterface $visitorGraphNavigator $navigatorMetadataFactoryInterface $factory)
  23.     {
  24.         parent::initialize($format$visitor$navigator$factory);
  25.         $this->visitingSet = new \SplObjectStorage();
  26.         $this->visitingStack = new \SplStack();
  27.     }
  28.     public function startVisiting($object)
  29.     {
  30.         if (!\is_object($object)) {
  31.             return;
  32.         }
  33.         $this->visitingSet->attach($object);
  34.         $this->visitingStack->push($object);
  35.     }
  36.     public function stopVisiting($object)
  37.     {
  38.         if (!\is_object($object)) {
  39.             return;
  40.         }
  41.         $this->visitingSet->detach($object);
  42.         $poppedObject $this->visitingStack->pop();
  43.         if ($object !== $poppedObject) {
  44.             throw new RuntimeException('Context visitingStack not working well');
  45.         }
  46.     }
  47.     public function isVisiting($object)
  48.     {
  49.         if (!\is_object($object)) {
  50.             return false;
  51.         }
  52.         return $this->visitingSet->contains($object);
  53.     }
  54.     public function getPath()
  55.     {
  56.         $path = array();
  57.         foreach ($this->visitingStack as $obj) {
  58.             $path[] = \get_class($obj);
  59.         }
  60.         if (!$path) {
  61.             return null;
  62.         }
  63.         return implode(' -> '$path);
  64.     }
  65.     public function getDirection()
  66.     {
  67.         return GraphNavigator::DIRECTION_SERIALIZATION;
  68.     }
  69.     public function getDepth()
  70.     {
  71.         return $this->visitingStack->count();
  72.     }
  73.     public function getObject()
  74.     {
  75.         return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null;
  76.     }
  77.     public function getVisitingStack()
  78.     {
  79.         return $this->visitingStack;
  80.     }
  81.     public function getVisitingSet()
  82.     {
  83.         return $this->visitingSet;
  84.     }
  85.     /**
  86.      * @param string $type
  87.      * @return $this
  88.      */
  89.     public function setInitialType($type)
  90.     {
  91.         $this->initialType $type;
  92.         $this->attributes->set('initial_type'$type);
  93.         return $this;
  94.     }
  95.     /**
  96.      * @return string|null
  97.      */
  98.     public function getInitialType()
  99.     {
  100.         return $this->initialType
  101.             $this->initialType
  102.             $this->attributes->containsKey('initial_type') ? $this->attributes->get('initial_type')->get() : null;
  103.     }
  104. }