|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace JMS\Serializer; |
| 4 | + |
| 5 | +use JMS\Serializer\Exception\RuntimeException; |
| 6 | +use JMS\Serializer\Exception\LogicException; |
| 7 | +use JMS\Serializer\Exclusion\ExclusionStrategyInterface; |
| 8 | +use JMS\Serializer\Exclusion\GroupsExclusionStrategy; |
| 9 | +use JMS\Serializer\Exclusion\VersionExclusionStrategy; |
| 10 | +use PhpCollection\Map; |
| 11 | +use PhpOption\None; |
| 12 | +use PhpOption\Some; |
| 13 | + |
| 14 | +class Context |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \PhpCollection\Map |
| 18 | + */ |
| 19 | + public $attributes; |
| 20 | + |
| 21 | + private $direction; |
| 22 | + private $format; |
| 23 | + private $visitingSet; |
| 24 | + private $visitingStack; |
| 25 | + |
| 26 | + /** @var VisitorInterface */ |
| 27 | + private $visitor; |
| 28 | + |
| 29 | + /** @var GraphNavigator */ |
| 30 | + private $navigator; |
| 31 | + |
| 32 | + /** @var ExclusionStrategyInterface */ |
| 33 | + private $exclusionStrategy; |
| 34 | + |
| 35 | + /** @var boolean */ |
| 36 | + private $serializeNull; |
| 37 | + |
| 38 | + public static function create() |
| 39 | + { |
| 40 | + return new self(); |
| 41 | + } |
| 42 | + |
| 43 | + public function __construct() |
| 44 | + { |
| 45 | + $this->attributes = new Map(); |
| 46 | + } |
| 47 | + |
| 48 | + public function initialize($direction, $format, VisitorInterface $visitor, GraphNavigator $navigator) |
| 49 | + { |
| 50 | + $this->visitingSet = new \SplObjectStorage(); |
| 51 | + $this->visitingStack = new \SplStack(); |
| 52 | + $this->direction = $direction; |
| 53 | + $this->format = $format; |
| 54 | + $this->visitor = $visitor; |
| 55 | + $this->navigator = $navigator; |
| 56 | + } |
| 57 | + |
| 58 | + public function accept($data, array $type) |
| 59 | + { |
| 60 | + return $this->navigator->accept($data, $type, $this); |
| 61 | + } |
| 62 | + |
| 63 | + public function getVisitor() |
| 64 | + { |
| 65 | + return $this->visitor; |
| 66 | + } |
| 67 | + |
| 68 | + public function getNavigator() |
| 69 | + { |
| 70 | + return $this->navigator; |
| 71 | + } |
| 72 | + |
| 73 | + public function getExclusionStrategy() |
| 74 | + { |
| 75 | + return $this->exclusionStrategy; |
| 76 | + } |
| 77 | + |
| 78 | + public function setAttribute($key, $value) |
| 79 | + { |
| 80 | + $this->attributes->set($key, $value); |
| 81 | + |
| 82 | + return $this; |
| 83 | + } |
| 84 | + |
| 85 | + public function setExclusionStrategy(ExclusionStrategyInterface $strategy) |
| 86 | + { |
| 87 | + $this->exclusionStrategy = $strategy; |
| 88 | + |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param integer $version |
| 94 | + */ |
| 95 | + public function setVersion($version) |
| 96 | + { |
| 97 | + if (null === $version) { |
| 98 | + $this->exclusionStrategy = null; |
| 99 | + |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + $this->exclusionStrategy = new VersionExclusionStrategy($version); |
| 104 | + |
| 105 | + return $this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param null|array $groups |
| 110 | + */ |
| 111 | + public function setGroups($groups) |
| 112 | + { |
| 113 | + if ( ! $groups) { |
| 114 | + $this->exclusionStrategy = null; |
| 115 | + |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + $this->exclusionStrategy = new GroupsExclusionStrategy((array) $groups); |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | + |
| 124 | + public function setSerializeNull($bool) |
| 125 | + { |
| 126 | + $this->serializeNull = (boolean) $bool; |
| 127 | + |
| 128 | + return $this; |
| 129 | + } |
| 130 | + |
| 131 | + public function shouldSerializeNull() |
| 132 | + { |
| 133 | + return $this->serializeNull; |
| 134 | + } |
| 135 | + |
| 136 | + public function getDirection() |
| 137 | + { |
| 138 | + return $this->direction; |
| 139 | + } |
| 140 | + |
| 141 | + public function getFormat() |
| 142 | + { |
| 143 | + return $this->format; |
| 144 | + } |
| 145 | + |
| 146 | + public function startVisiting($object) |
| 147 | + { |
| 148 | + if ($this->direction !== GraphNavigator::DIRECTION_SERIALIZATION) { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + $this->visitingSet->attach($object); |
| 153 | + $this->visitingStack->push($object); |
| 154 | + } |
| 155 | + |
| 156 | + public function stopVisiting($object) |
| 157 | + { |
| 158 | + if ($this->direction !== GraphNavigator::DIRECTION_SERIALIZATION) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + $this->visitingSet->detach($object); |
| 163 | + $poppedObject = $this->visitingStack->pop(); |
| 164 | + |
| 165 | + if ($object !== $poppedObject) { |
| 166 | + throw new RuntimeException('Context visitingStack not working well'); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public function isVisiting($object) |
| 171 | + { |
| 172 | + if (! is_object($object)) { |
| 173 | + throw new LogicException('Expected object but got ' . gettype($object) . '. Do you have the wrong @Type mapping or could this be a Doctrine many-to-many relation?'); |
| 174 | + } |
| 175 | + return $this->visitingSet->contains($object); |
| 176 | + } |
| 177 | + |
| 178 | + public function getPath() |
| 179 | + { |
| 180 | + $path = array(); |
| 181 | + foreach ($this->visitingStack as $obj) { |
| 182 | + $path[] = get_class($obj); |
| 183 | + } |
| 184 | + |
| 185 | + if ( ! $path) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + return implode(' -> ', $path); |
| 190 | + } |
| 191 | + |
| 192 | + public function getDepth() |
| 193 | + { |
| 194 | + return $this->visitingStack->count(); |
| 195 | + } |
| 196 | + |
| 197 | + public function getObject() |
| 198 | + { |
| 199 | + return !$this->visitingStack->isEmpty() ? $this->visitingStack->top() : null; |
| 200 | + } |
| 201 | +} |
0 commit comments