public function visitProperty(PropertyMetadata $metadata, $object, Context $context) { $v = $metadata->getValue($object); if (null === $v && !$context->shouldSerializeNull()) { return; } if ($metadata->xmlAttribute) { $this->setCurrentMetadata($metadata); $node = $this->navigator->accept($v, $metadata->type, $context); $this->revertCurrentMetadata(); if (!$node instanceof \DOMCharacterData) { throw new RuntimeException(sprintf('Unsupported value for XML attribute for %s. Expected character data, but got %s.', $metadata->name, json_encode($v))); } $attributeName = $this->namingStrategy->translateName($metadata); $this->setAttributeOnNode($this->currentNode, $attributeName, $node->nodeValue, $metadata->xmlNamespace); return; } if ($metadata->xmlValue && $this->currentNode->childNodes->length > 0 || !$metadata->xmlValue && $this->hasValue) { throw new RuntimeException(sprintf('If you make use of @XmlValue, all other properties in the class must have the @XmlAttribute annotation. Invalid usage detected in class %s.', $metadata->class)); } if ($metadata->xmlValue) { $this->hasValue = true; $this->setCurrentMetadata($metadata); $node = $this->navigator->accept($v, $metadata->type, $context); $this->revertCurrentMetadata(); if ($node !== null) { if (!$node instanceof \DOMCharacterData) { throw new RuntimeException(sprintf('Unsupported value for property %s::$%s. Expected character data, but got %s.', $metadata->reflection->class, $metadata->reflection->name, is_object($node) ? get_class($node) : gettype($node))); } $this->currentNode->appendChild($node); } return; } if ($metadata->xmlAttributeMap) { if (!is_array($v)) { throw new RuntimeException(sprintf('Unsupported value type for XML attribute map. Expected array but got %s.', gettype($v))); } foreach ($v as $key => $value) { $this->setCurrentMetadata($metadata); $node = $this->navigator->accept($value, null, $context); $this->revertCurrentMetadata(); if (!$node instanceof \DOMCharacterData) { throw new RuntimeException(sprintf('Unsupported value for a XML attribute map value. Expected character data, but got %s.', json_encode($v))); } $this->setAttributeOnNode($this->currentNode, $key, $node->nodeValue, $metadata->xmlNamespace); } return; } if ($addEnclosingElement = (!$metadata->xmlCollection || !$metadata->xmlCollectionInline) && !$metadata->inline) { $elementName = $this->namingStrategy->translateName($metadata); if (null !== $metadata->xmlNamespace) { $element = $this->createElement($elementName, $metadata->xmlNamespace); } else { $defaultNamespace = $this->getClassDefaultNamespace($this->objectMetadataStack->top()); $element = $this->createElement($elementName, $defaultNamespace); } $this->setCurrentNode($element); } $this->setCurrentMetadata($metadata); if (null !== ($node = $this->navigator->accept($v, $metadata->type, $context))) { $this->currentNode->appendChild($node); } $this->revertCurrentMetadata(); if ($addEnclosingElement) { $this->revertCurrentNode(); if ($element->hasChildNodes() || $element->hasAttributes() || $node === null && $v !== null) { $this->currentNode->appendChild($element); } } $this->hasValue = false; }
public function visitProperty(PropertyMetadata $metadata, $data, Context $context) { $v = $metadata->getValue($data); if (null === $v && !$context->shouldSerializeNull()) { return; } $name = $this->namingStrategy->translateName($metadata); if (!$metadata->inline) { $this->writer->writeln(Inline::dump($name) . ':')->indent(); } $this->setCurrentMetadata($metadata); $count = $this->writer->changeCount; if (null !== ($v = $this->navigator->accept($v, $metadata->type, $context))) { $this->writer->rtrim(false)->writeln(' ' . $v); } elseif ($count === $this->writer->changeCount && !$metadata->inline) { $this->writer->revert(); } if (!$metadata->inline) { $this->writer->outdent(); } $this->revertCurrentMetadata(); }