Skip to content

Commit a5a57c5

Browse files
authored
Merge pull request #1006 from schmittjoh/context-methods
Move serialization info to the serialization context class
2 parents 1987195 + 2f431fe commit a5a57c5

4 files changed

Lines changed: 32 additions & 26 deletions

File tree

src/Context.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ abstract class Context
4646
/** @var DisjunctExclusionStrategy */
4747
private $exclusionStrategy;
4848

49-
/**
50-
* @var bool
51-
*/
52-
private $serializeNull = false;
53-
5449
/**
5550
* @var bool
5651
*/
@@ -193,25 +188,6 @@ public function enableMaxDepthChecks(): self
193188
return $this;
194189
}
195190

196-
/**
197-
* Set if NULLs should be serialized (TRUE) ot not (FALSE)
198-
*/
199-
public function setSerializeNull(bool $bool): self
200-
{
201-
$this->serializeNull = $bool;
202-
203-
return $this;
204-
}
205-
206-
/**
207-
* Returns TRUE when NULLs should be serialized
208-
* Returns FALSE when NULLs should not be serialized
209-
*/
210-
public function shouldSerializeNull(): bool
211-
{
212-
return $this->serializeNull;
213-
}
214-
215191
public function getFormat(): string
216192
{
217193
return $this->format;

src/SerializationContext.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class SerializationContext extends Context
2020
*/
2121
private $initialType;
2222

23+
/**
24+
* @var bool
25+
*/
26+
private $serializeNull = false;
27+
2328
public static function create(): self
2429
{
2530
return new self();
@@ -33,6 +38,25 @@ public function initialize(string $format, VisitorInterface $visitor, GraphNavig
3338
$this->visitingStack = new \SplStack();
3439
}
3540

41+
/**
42+
* Set if NULLs should be serialized (TRUE) ot not (FALSE)
43+
*/
44+
public function setSerializeNull(bool $bool): self
45+
{
46+
$this->serializeNull = $bool;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* Returns TRUE when NULLs should be serialized
53+
* Returns FALSE when NULLs should not be serialized
54+
*/
55+
public function shouldSerializeNull(): bool
56+
{
57+
return $this->serializeNull;
58+
}
59+
3660
/**
3761
* @param mixed $object
3862
*/

tests/Serializer/BaseSerializationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ public function testDeserializingNull()
726726
self::assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, SerializationContext::create()->setSerializeNull(true)));
727727

728728
if ($this->hasDeserializer()) {
729-
$deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), DeserializationContext::create()->setSerializeNull(true));
729+
$deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), DeserializationContext::create());
730730

731731
self::assertEquals('2011-07-30T00:00:00+00:00', $this->getField($deserialized, 'createdAt')->format(\DateTime::ATOM));
732732
self::assertAttributeEquals('This is a nice title.', 'title', $deserialized);

tests/Serializer/GraphNavigatorTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GraphNavigatorTest extends TestCase
3232
private $serializationNavigator;
3333
private $deserializationNavigator;
3434
private $context;
35+
private $deserializationContext;
3536
private $accessor;
3637

3738
private $serializationVisitor;
@@ -140,6 +141,11 @@ protected function setUp()
140141
->setMethodsExcept(['getExclusionStrategy'])
141142
->getMock();
142143

144+
$this->deserializationContext = $this->getMockBuilder(DeserializationContext::class)
145+
->enableOriginalConstructor()
146+
->setMethodsExcept(['getExclusionStrategy'])
147+
->getMock();
148+
143149
$this->dispatcher = new EventDispatcher();
144150
$this->accessor = new DefaultAccessorStrategy();
145151
$this->handlerRegistry = new HandlerRegistry();
@@ -151,7 +157,7 @@ protected function setUp()
151157
$this->serializationNavigator->initialize($this->serializationVisitor, $this->context);
152158

153159
$this->deserializationNavigator = new DeserializationGraphNavigator($this->metadataFactory, $this->handlerRegistry, $this->objectConstructor, $this->accessor, $this->dispatcher);
154-
$this->deserializationNavigator->initialize($this->deserializationVisitor, $this->context);
160+
$this->deserializationNavigator->initialize($this->deserializationVisitor, $this->deserializationContext);
155161
}
156162
}
157163

0 commit comments

Comments
 (0)