Exemplo n.º 1
0
 public function testToArrayConversNestedArrayObjects()
 {
     $list = new AuthorList();
     $list->add(new Author(null));
     $result = $this->serializer->toArray($list);
     $this->assertSame(['authors' => [[]]], $result);
 }
Exemplo n.º 2
0
 protected function setUp()
 {
     $loader = new AnnotationLoader();
     $loader->setReader(new AnnotationReader());
     $this->factory = new MetadataFactory($loader);
     $this->handlerRegistry = new HandlerRegistry();
     $this->handlerRegistry->registerSubscribingHandler(new ConstraintViolationHandler());
     $this->handlerRegistry->registerSubscribingHandler(new DateHandler());
     $this->handlerRegistry->registerSubscribingHandler(new FormErrorHandler(new IdentityTranslator(new MessageSelector())));
     $this->handlerRegistry->registerSubscribingHandler(new PhpCollectionHandler());
     $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler());
     $this->handlerRegistry->registerHandler(Direction::DIRECTION_SERIALIZATION, 'AuthorList', function (VisitorInterface $visitor, $object, Type $type, Context $context) {
         return $visitor->visitArray(iterator_to_array($object), $type, $context);
     });
     $this->handlerRegistry->registerHandler(Direction::DIRECTION_DESERIALIZATION, 'AuthorList', function (VisitorInterface $visitor, $data, $type, Context $context) {
         $type = new Type('array', [Type::from('integer'), Type::from(Author::class)]);
         $elements = $context->accept($data, $type);
         $list = new AuthorList();
         foreach ($elements as $author) {
             $list->add($author);
         }
         return $list;
     });
     $this->dispatcher = new EventDispatcher();
     $this->dispatcher->addSubscriber(new DoctrineProxySubscriber());
     $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
     $objectConstructor = new UnserializeObjectConstructor();
     $this->serializationVisitors = ['array' => new GenericSerializationVisitor($namingStrategy), 'json' => new JsonSerializationVisitor($namingStrategy), 'xml' => new XmlSerializationVisitor($namingStrategy), 'yml' => new YamlSerializationVisitor($namingStrategy)];
     $this->deserializationVisitors = ['array' => new GenericDeserializationVisitor($namingStrategy), 'xml' => new XmlDeserializationVisitor($namingStrategy), 'yml' => new YamlDeserializationVisitor($namingStrategy), 'json' => new JsonDeserializationVisitor($namingStrategy)];
     $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors, $this->dispatcher);
 }