public function visitObject(ClassMetadata $metadata, $data, Type $type, Context $context, ObjectConstructorInterface $objectConstructor = null)
 {
     $this->data = [];
     /** @var PropertyMetadata $propertyMetadata */
     foreach ($context->getNonSkippedProperties($metadata) as $propertyMetadata) {
         $context->getMetadataStack()->push($propertyMetadata);
         $this->visitProperty($propertyMetadata, $data, $context);
         $context->getMetadataStack()->pop();
     }
     return $this->data;
 }
 private function isTooDeep(Context $context)
 {
     $depth = $context->getDepth();
     $metadataStack = $context->getMetadataStack();
     $nthProperty = 1;
     foreach ($metadataStack as $metadata) {
         $relativeDepth = $depth - ++$nthProperty;
         if (null !== $metadata->maxDepth && $relativeDepth > $metadata->maxDepth) {
             return true;
         }
     }
     return false;
 }
 private function getGroupsFor(Context $navigatorContext)
 {
     $groups = $this->groups;
     foreach ($navigatorContext->getMetadataStack()->getPath() as $index => $path) {
         if (!array_key_exists($path, $groups)) {
             if ($index > 0) {
                 return [self::DEFAULT_GROUP];
             }
             break;
         }
         $groups = $groups[$path];
     }
     return $groups;
 }
 protected function filterPropertyMetadata(PropertyMetadata $propertyMetadata)
 {
     if ($propertyMetadata->readOnly) {
         return false;
     }
     return parent::filterPropertyMetadata($propertyMetadata);
 }
 protected function visitProperty(PropertyMetadata $metadata, $data, Context $context)
 {
     $name = $this->namingStrategy->translateName($metadata);
     if (null === $data || !array_key_exists($name, $data)) {
         return null;
     }
     if (null === $metadata->type) {
         throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->getReflection()->class, $metadata->name));
     }
     $v = $data[$name] !== null ? $context->accept($data[$name], $metadata->type) : null;
     $this->addData($name, $v);
     return $v;
 }
Exemplo n.º 6
0
 private function visit(VisitorInterface $visitor, Context $context, $data, $format, Type $type = null)
 {
     $data = $visitor->prepare($data);
     $context->initialize($format, $visitor, $this->navigator, $this->factory);
     $visitor->setNavigator($this->navigator);
     $this->navigator->accept($data, $type, $context);
     return $visitor->getResult();
 }
Exemplo n.º 7
0
 private function callVisitor($data, Type $type, Context $context, ClassMetadata $metadata = null)
 {
     $visitor = $context->getVisitor();
     // First, try whether a custom handler exists for the given type
     if (null !== ($handler = $this->handlerRegistry->getHandler($context->getDirection(), $type->getName()))) {
         return $visitor->visitCustom($handler, $data, $type, $context);
     }
     switch ($type->getName()) {
         case 'NULL':
             return $visitor->visitNull($data, $type, $context);
         case 'string':
             return $visitor->visitString($data, $type, $context);
         case 'integer':
             return $visitor->visitInteger($data, $type, $context);
         case 'boolean':
             return $visitor->visitBoolean($data, $type, $context);
         case 'double':
         case 'float':
             return $visitor->visitDouble($data, $type, $context);
         case 'array':
             return $this->visitArray($visitor, $data, $type, $context);
         case 'resource':
             $msg = 'Resources are not supported in serialized data.';
             throw new RuntimeException($msg);
         default:
             if (null === $metadata) {
                 // Missing handler for custom type
                 return null;
             }
             $exclusionStrategy = $context->getExclusionStrategy();
             if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($metadata, $context)) {
                 return null;
             }
             return $visitor->visitObject($metadata, $data, $type, $context, $this->objectConstructor);
     }
 }
 protected function visitProperty(PropertyMetadata $metadata, $data, Context $context)
 {
     $name = $this->namingStrategy->translateName($metadata);
     if (null === $metadata->type) {
         throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->getReflection()->class, $metadata->name));
     }
     if ($metadata->xmlAttribute) {
         $attributes = $data->attributes($metadata->xmlNamespace);
         if (isset($attributes[$name])) {
             return $context->accept($attributes[$name], $metadata->type);
         }
         return null;
     }
     if ($metadata->xmlValue) {
         return $context->accept($data, $metadata->type);
     }
     if ($metadata->xmlCollection) {
         $enclosingElem = $data;
         if (!$metadata->xmlCollectionInline) {
             $enclosingElem = $data->children($metadata->xmlNamespace)->{$name};
         }
         return $context->accept($enclosingElem, $metadata->type);
     }
     if ($metadata->xmlNamespace) {
         $node = $data->children($metadata->xmlNamespace)->{$name};
         if (!$node->count()) {
             return null;
         }
     } else {
         $namespaces = $data->getDocNamespaces();
         if (isset($namespaces[''])) {
             $prefix = uniqid('ns-');
             $data->registerXPathNamespace($prefix, $namespaces['']);
             $nodes = $data->xpath('./' . $prefix . ':' . $name);
             if (empty($nodes)) {
                 return null;
             }
             $node = reset($nodes);
         } else {
             if (!isset($data->{$name})) {
                 return null;
             }
             $node = $data->{$name};
         }
     }
     if ($this->isNullNode($node)) {
         return $this->visitNull(null, Type::null(), $context);
     }
     return $context->accept($node, $metadata->type);
 }
Exemplo n.º 9
0
 public function initialize($format, VisitorInterface $visitor, GraphNavigator $navigator, MetadataFactoryInterface $factory)
 {
     parent::initialize($format, $visitor, $navigator, $factory);
     $this->visitingSet = new \SplObjectStorage();
 }
Exemplo n.º 10
0
 public function visitArray($data, Type $type, Context $context)
 {
     if ($this->nodeStack->count() === 1 && $this->document->documentElement === null) {
         $this->createRootNode();
     }
     /** @var PropertyMetadata $metadata */
     $nodeName = 'entry';
     if (($metadata = $context->getMetadataStack()->getCurrent()) && !empty($metadata->xmlEntryName)) {
         $nodeName = $metadata->xmlEntryName;
     }
     $attributeName = null !== $metadata ? $metadata->xmlKeyAttribute : null;
     $namespace = null !== $metadata ? $metadata->xmlEntryNamespace : null;
     /** @var \DOMNode[] $nodes */
     $nodes = [];
     $elementType = $this->getElementType($type);
     foreach ($data as $k => $v) {
         $elementName = null !== $metadata && $metadata->xmlKeyValuePairs && $this->isElementNameValid($k) ? (string) $k : $nodeName;
         $this->currentNodes = $this->createElement($namespace, $elementName);
         $context->accept($v, $elementType);
         if (null !== $attributeName) {
             $this->currentNodes->setAttribute($attributeName, (string) $k);
         }
         $nodes[] = $this->currentNodes;
     }
     return $this->currentNodes = $nodes;
 }
 public function serializeViolation(VisitorInterface $visitor, ConstraintViolation $violation, Type $type, Context $context)
 {
     $serializableViolation = new SerializableConstraintViolation($violation);
     $metadata = $context->getMetadataFactory()->getMetadataFor($serializableViolation);
     return $visitor->visitObject($metadata, $serializableViolation, $type, $context);
 }
Exemplo n.º 12
0
 public function serializeForm(VisitorInterface $visitor, Form $form, Type $type, Context $context)
 {
     $serializableForm = new SerializableForm($form);
     $metadata = $context->getMetadataFactory()->getMetadataFor($serializableForm);
     return $visitor->visitObject($metadata, $serializableForm, $type, $context);
 }