public function testShouldSkipPropertyReturnsFalseIfNoPredicateMatches()
 {
     $metadata = new StaticPropertyMetadata(\stdClass::class, 'foo', 'bar');
     $context = SerializationContext::create();
     $strat = new DisjunctExclusionStrategy([$first = $this->createMock(ExclusionStrategyInterface::class), $last = $this->createMock(ExclusionStrategyInterface::class)]);
     $first->expects($this->once())->method('shouldSkipProperty')->with($metadata, $context)->will($this->returnValue(false));
     $last->expects($this->once())->method('shouldSkipProperty')->with($metadata, $context)->will($this->returnValue(false));
     $this->assertFalse($strat->shouldSkipProperty($metadata, $context));
 }
Пример #2
0
 public function testSerializeNullOption()
 {
     $context = SerializationContext::create();
     $this->assertFalse($context->shouldSerializeNull());
     $context->setSerializeNull(false);
     $this->assertFalse($context->shouldSerializeNull());
     $context->setSerializeNull(true);
     $this->assertTrue($context->shouldSerializeNull());
     $context->setSerializeNull('foo');
     $this->assertTrue($context->shouldSerializeNull());
     $context->setSerializeNull('0');
     $this->assertFalse($context->shouldSerializeNull());
 }
Пример #3
0
 private function serialize($data, Type $type, SerializationContext $context)
 {
     $inVisitingStack = is_object($data) && null !== $data && !$context->getMetadataStack()->getCurrent() instanceof AdditionalPropertyMetadata;
     if ($inVisitingStack) {
         if ($context->isVisiting($data)) {
             return null;
         }
         $context->startVisiting($data);
     }
     // If we're serializing a polymorphic type, then we'll be interested in the
     // metadata for the actual type of the object, not the base class.
     if (is_subclass_of($data, $type->getName(), false)) {
         $type = new Type(get_class($data));
     }
     if (null !== $this->dispatcher && !is_scalar($data)) {
         $this->dispatcher->dispatch(Events::PRE_SERIALIZE, $event = new PreSerializeEvent($context, $data, $type));
         $data = $event->getData();
     }
     $metadata = $this->getMetadataForType($type);
     if (null !== $metadata) {
         foreach ($metadata->preSerializeMethods as $method) {
             $method->getReflection()->invoke($data);
         }
     }
     $context->getVisitor()->startVisiting($data, $type, $context);
     $this->callVisitor($data, $type, $context, $metadata);
     if (null !== $metadata) {
         foreach ($metadata->postSerializeMethods as $method) {
             $method->getReflection()->invoke($data);
         }
     }
     if (null !== $this->dispatcher && !is_scalar($data)) {
         $this->dispatcher->dispatch(Events::POST_SERIALIZE, new PostSerializeEvent($context, $data, $type));
     }
     $rs = $context->getVisitor()->endVisiting($data, $type, $context);
     if ($inVisitingStack) {
         $context->stopVisiting($data);
     }
     return $rs;
 }
Пример #4
0
 public function testDepthExclusionStrategy()
 {
     $context = SerializationContext::create()->addExclusionStrategy(new DepthExclusionStrategy());
     $data = new Tree(new Node([new Node([new Node([new Node([new Node()])])])]));
     $this->assertEquals($this->getContent('tree'), $this->serializer->serialize($data, $this->getFormat(), $context));
 }
Пример #5
0
 public function testObjectWithNamespacesAndList()
 {
     $object = new ObjectWithNamespacesAndList();
     $object->name = 'name';
     $object->nameAlternativeB = 'nameB';
     $object->phones = ['111', '222'];
     $object->addresses = ['A' => 'Street 1', 'B' => 'Street 2'];
     $object->phonesAlternativeB = ['555', '666'];
     $object->addressesAlternativeB = ['A' => 'Street 5', 'B' => 'Street 6'];
     $object->phonesAlternativeC = ['777', '888'];
     $object->addressesAlternativeC = ['A' => 'Street 7', 'B' => 'Street 8'];
     $object->phonesAlternativeD = ['999', 'AAA'];
     $object->addressesAlternativeD = ['A' => 'Street 9', 'B' => 'Street A'];
     $this->assertEquals($this->getContent('object_with_namespaces_and_list'), $this->serialize($object, SerializationContext::create()));
     $this->assertEquals($object, $this->deserialize($this->getContent('object_with_namespaces_and_list'), get_class($object)));
 }