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; }
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); }
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; }