Exemplo n.º 1
0
 public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
 {
     if (0 !== strpos($type, 'ArrayCollection')) {
         return;
     }
     $visited = true;
     $elements = $visitor->visitArray($data, 'array' . substr($type, 15));
     return new ArrayCollection($elements);
 }
Exemplo n.º 2
0
 public function deserialize(VisitorInterface $visitor, $data, $type, &$handled)
 {
     if (!class_exists($type) || !in_array('JMS\\SerializerBundle\\Serializer\\Handler\\DeserializationHandlerInterface', class_implements($type))) {
         return;
     }
     $metadata = $this->metadataFactory->getMetadataForClass($type);
     $visitor->startVisitingObject($metadata, $data, $type);
     $instance = $visitor->getResult();
     $instance->deserialize($visitor, $data, $type, $handled);
     return $instance;
 }
Exemplo n.º 3
0
 public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
 {
     if (($data instanceof Proxy || $data instanceof ORMProxy) && (!$data->__isInitialized__ || get_class($data) === $type)) {
         $handled = true;
         if (!$data->__isInitialized__) {
             $data->__load();
         }
         $navigator = $visitor->getNavigator();
         $navigator->detachObject($data);
         // pass the parent class not to load the metadata for the proxy class
         return $navigator->accept($data, get_parent_class($data), $visitor);
     }
     return null;
 }
Exemplo n.º 4
0
 public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
 {
     if ($data instanceof ConstraintViolationList) {
         if ($visitor instanceof XmlSerializationVisitor) {
             $handled = true;
             if (null === $visitor->document) {
                 $visitor->document = $visitor->createDocument();
             }
             foreach ($data as $violation) {
                 $this->serialize($visitor, $violation, null, $visited);
             }
         }
     } else {
         if ($data instanceof ConstraintViolation) {
             if ($visitor instanceof XmlSerializationVisitor) {
                 $handled = true;
                 if (null === $visitor->document) {
                     $visitor->document = $visitor->createDocument(null, null, false);
                     $visitor->document->appendChild($violationNode = $visitor->document->createElement('violation'));
                     $visitor->setCurrentNode($violationNode);
                 } else {
                     $visitor->getCurrentNode()->appendChild($violationNode = $visitor->document->createElement('violation'));
                 }
                 $violationNode->setAttribute('property_path', $data->getPropertyPath());
                 $violationNode->appendChild($messageNode = $visitor->document->createElement('message'));
                 $messageNode->appendChild($visitor->document->createCDATASection($data->getMessage()));
                 return;
             } else {
                 if ($visitor instanceof GenericSerializationVisitor) {
                     $handled = true;
                     $violation = array('property_path' => $data->getPropertyPath(), 'message' => $data->getMessage());
                     if (null === $visitor->getRoot()) {
                         $visitor->setRoot($violation);
                     }
                     return $violation;
                 }
             }
         }
     }
     return;
 }
Exemplo n.º 5
0
 public function serialize(VisitorInterface $visitor, $data, $type, &$visited)
 {
     if (!$data instanceof \DateTime) {
         return;
     }
     if ($visitor instanceof XmlSerializationVisitor) {
         if (null === $visitor->document) {
             $visitor->document = $visitor->createDocument(null, null, true);
         }
         $visited = true;
         return $visitor->document->createTextNode($data->format($this->format));
     } else {
         if ($visitor instanceof GenericSerializationVisitor) {
             $visited = true;
             return $data->format($this->format);
         } else {
             if ($visitor instanceof YamlSerializationVisitor) {
                 $visited = true;
                 return Inline::dump($data->format($this->format));
             }
         }
     }
 }
Exemplo n.º 6
0
 public function accept($data, $type, VisitorInterface $visitor)
 {
     // determine type if not given
     if (null === $type) {
         if (null === $data) {
             return null;
         }
         $type = gettype($data);
         if ('object' === $type) {
             $type = get_class($data);
         }
     }
     if ('string' === $type) {
         return $visitor->visitString($data, $type);
     } else {
         if ('integer' === $type) {
             return $visitor->visitInteger($data, $type);
         } else {
             if ('boolean' === $type) {
                 return $visitor->visitBoolean($data, $type);
             } else {
                 if ('double' === $type) {
                     return $visitor->visitDouble($data, $type);
                 } else {
                     if ('array' === $type || 'a' === $type[0] && 0 === strpos($type, 'array<')) {
                         return $visitor->visitArray($data, $type);
                     } else {
                         if ('resource' === $type) {
                             $path = array();
                             foreach ($this->visiting as $obj) {
                                 $path[] = get_class($obj);
                             }
                             $msg = 'Resources are not supported in serialized data.';
                             if ($path) {
                                 $msg .= ' Path: ' . implode(' -> ', $path);
                             }
                             throw new \RuntimeException($msg);
                         } else {
                             if (self::DIRECTION_SERIALIZATION === $this->direction && null !== $data) {
                                 if ($this->visiting->contains($data)) {
                                     return null;
                                 }
                                 $this->visiting->attach($data);
                             }
                             // try custom handler
                             $handled = false;
                             $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
                             if ($handled) {
                                 if (self::DIRECTION_SERIALIZATION === $this->direction) {
                                     $this->visiting->detach($data);
                                 }
                                 return $rs;
                             }
                             $metadata = $this->metadataFactory->getMetadataForClass($type);
                             if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
                                 if (self::DIRECTION_SERIALIZATION === $this->direction) {
                                     $this->visiting->detach($data);
                                 }
                                 return null;
                             }
                             // pre-serialization callbacks
                             if (self::DIRECTION_SERIALIZATION === $this->direction) {
                                 foreach ($metadata->preSerializeMethods as $method) {
                                     $method->invoke($data);
                                 }
                             }
                             // check if traversable
                             if (self::DIRECTION_SERIALIZATION === $this->direction && $data instanceof \Traversable) {
                                 $rs = $visitor->visitTraversable($data, $type);
                                 $this->afterVisitingObject($metadata, $data, self::DIRECTION_SERIALIZATION === $this->direction);
                                 return $rs;
                             }
                             $visitor->startVisitingObject($metadata, $data, $type);
                             foreach ($metadata->propertyMetadata as $propertyMetadata) {
                                 if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
                                     continue;
                                 }
                                 if (self::DIRECTION_DESERIALIZATION === $this->direction && $propertyMetadata->readOnly) {
                                     continue;
                                 }
                                 // try custom handler
                                 if (!$visitor->visitPropertyUsingCustomHandler($propertyMetadata, $data)) {
                                     $visitor->visitProperty($propertyMetadata, $data);
                                 }
                             }
                             $rs = $visitor->endVisitingObject($metadata, $data, $type);
                             $this->afterVisitingObject($metadata, self::DIRECTION_SERIALIZATION === $this->direction ? $data : $rs);
                             return $rs;
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
 public function serialize(VisitorInterface $visitor, $data, $type, &$handled)
 {
     if ($data instanceof Form) {
         if ($visitor instanceof XmlSerializationVisitor) {
             $handled = true;
             if (null === $visitor->document) {
                 $visitor->document = $visitor->createDocument(null, null, false);
                 $visitor->document->appendChild($formNode = $visitor->document->createElement('form'));
                 $visitor->setCurrentNode($formNode);
             } else {
                 $visitor->getCurrentNode()->appendChild($formNode = $visitor->document->createElement('form'));
             }
             $formNode->setAttribute('name', $data->getName());
             $formNode->appendChild($errorsNode = $visitor->document->createElement('errors'));
             foreach ($data->getErrors() as $error) {
                 $errorNode = $visitor->document->createElement('entry');
                 $errorNode->appendChild($this->serialize($visitor, $error, null, $visited));
                 $errorsNode->appendChild($errorNode);
             }
             foreach ($data->getChildren() as $child) {
                 if (null !== ($node = $this->serialize($visitor, $child, null, $visited))) {
                     $formNode->appendChild($node);
                 }
             }
             return;
         } else {
             if ($visitor instanceof GenericSerializationVisitor) {
                 $handled = true;
                 $isRoot = null === $visitor->getRoot();
                 $form = $errors = array();
                 foreach ($data->getErrors() as $error) {
                     $errors[] = $this->serialize($visitor, $error, null, $visited);
                 }
                 if ($errors) {
                     $form['errors'] = $errors;
                 }
                 $children = array();
                 foreach ($data->getChildren() as $child) {
                     $children[$child->getName()] = $this->serialize($visitor, $child, null, $visited);
                 }
                 if ($children) {
                     $form['children'] = $children;
                 }
                 if ($isRoot) {
                     $visitor->setRoot($form);
                 }
                 return $form;
             }
         }
     } else {
         if ($data instanceof FormError) {
             $handled = true;
             $message = $this->translator->trans($data->getMessageTemplate(), $data->getMessageParameters(), 'validators');
             if ($visitor instanceof XmlSerializationVisitor) {
                 if (null === $visitor->document) {
                     $visitor->document = $visitor->createDocument(null, null, true);
                 }
                 return $visitor->document->createCDATASection($message);
             }
             return $message;
         }
     }
     return null;
 }
Exemplo n.º 8
0
 /**
  * Called for each node of the graph that is being traversed.
  *
  * @param mixed $data the data depends on the direction, and type of visitor
  * @param array|null $type array has the format ["name" => string, "params" => array]
  * @param VisitorInterface $visitor
  *
  * @return mixed the return value depends on the direction, and type of visitor
  */
 public function accept($data, array $type = null, VisitorInterface $visitor)
 {
     // If the type was not given, we infer the most specific type from the
     // input data in serialization mode.
     if (null === $type) {
         if (self::DIRECTION_DESERIALIZATION === $this->direction) {
             $msg = 'The type must be given for all properties when deserializing.';
             if (null !== ($path = $this->getCurrentPath())) {
                 $msg .= ' Path: ' . $path;
             }
             throw new \RuntimeException($msg);
         }
         $typeName = gettype($data);
         if ('object' === $typeName) {
             $typeName = get_class($data);
         }
         $type = array('name' => $typeName, 'params' => array());
     }
     switch ($type['name']) {
         case 'NULL':
             return $visitor->visitNull($data, $type);
         case 'string':
             return $visitor->visitString($data, $type);
         case 'integer':
             return $visitor->visitInteger($data, $type);
         case 'boolean':
             return $visitor->visitBoolean($data, $type);
         case 'double':
             return $visitor->visitDouble($data, $type);
         case 'array':
             return $visitor->visitArray($data, $type);
         case 'resource':
             $msg = 'Resources are not supported in serialized data.';
             if (null !== ($path = $this->getCurrentPath())) {
                 $msg .= ' Path: ' . implode(' -> ', $path);
             }
             throw new \RuntimeException($msg);
         default:
             $isSerializing = self::DIRECTION_SERIALIZATION === $this->direction;
             if ($isSerializing && null !== $data) {
                 if ($this->visiting->contains($data)) {
                     return null;
                 }
                 $this->visiting->attach($data);
             }
             // First, try whether a custom handler exists for the given type. This is done
             // before loading metadata because the type name might not be a class, but
             // could also simply be an artifical type.
             if (null !== ($handler = $this->handlerRegistry->getHandler($this->direction, $type['name'], $this->format))) {
                 $rs = call_user_func($handler, $visitor, $data, $type);
                 if ($isSerializing) {
                     $this->visiting->detach($data);
                 }
                 return $rs;
             }
             // Trigger pre-serialization callbacks, and listeners if they exist.
             if ($isSerializing) {
                 if (null !== $this->dispatcher && $this->dispatcher->hasListeners('serializer.pre_serialize', $type['name'], $this->format)) {
                     $this->dispatcher->dispatch('serializer.pre_serialize', $type['name'], $this->format, $event = new PreSerializeEvent($visitor, $data, $type));
                     $type = $event->getType();
                 }
             }
             // Load metadata, and check whether this class should be excluded.
             $metadata = $this->metadataFactory->getMetadataForClass($type['name']);
             if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata, $isSerializing ? $data : null)) {
                 if ($isSerializing) {
                     $this->visiting->detach($data);
                 }
                 return null;
             }
             if ($isSerializing) {
                 foreach ($metadata->preSerializeMethods as $method) {
                     $method->invoke($data);
                 }
             }
             $object = $data;
             if (!$isSerializing) {
                 $object = $this->objectConstructor->construct($visitor, $metadata, $data, $type);
             }
             if (isset($metadata->handlerCallbacks[$this->direction][$this->format])) {
                 $rs = $object->{$metadata->handlerCallbacks[$this->direction][$this->format]}($visitor, $isSerializing ? null : $data);
                 $this->afterVisitingObject($visitor, $metadata, $object, $type);
                 return $isSerializing ? $rs : $object;
             }
             $visitor->startVisitingObject($metadata, $object, $type);
             foreach ($metadata->propertyMetadata as $propertyMetadata) {
                 if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata, $isSerializing ? $data : null)) {
                     continue;
                 }
                 if (!$isSerializing && $propertyMetadata->readOnly) {
                     continue;
                 }
                 $visitor->visitProperty($propertyMetadata, $data);
             }
             if ($isSerializing) {
                 $this->afterVisitingObject($visitor, $metadata, $data, $type);
                 return $visitor->endVisitingObject($metadata, $data, $type);
             }
             $rs = $visitor->endVisitingObject($metadata, $data, $type);
             $this->afterVisitingObject($visitor, $metadata, $rs, $type);
             return $rs;
     }
 }
 public function serialize(VisitorInterface $visitor, $data, $type, &$visited)
 {
     if (!$data instanceof Article) {
         return;
     }
     if ($visitor instanceof XmlSerializationVisitor) {
         $visited = true;
         if (null === $visitor->document) {
             $visitor->document = $visitor->createDocument(null, null, false);
         }
         $visitor->document->appendChild($visitor->document->createElement($this->element, $this->value));
     } elseif ($visitor instanceof JsonSerializationVisitor) {
         $visited = true;
         $visitor->setRoot(array($this->element => $this->value));
     } elseif ($visitor instanceof YamlSerializationVisitor) {
         $visited = true;
         $visitor->writer->writeln(Inline::dump($this->element) . ': ' . Inline::dump($this->value));
     }
 }
 public function deserializeCollection(VisitorInterface $visitor, $data, array $type)
 {
     // See above.
     $type['name'] = 'array';
     return new ArrayCollection($visitor->visitArray($data, $type));
 }