public function getXmlNamespace() { $namespace = $this->_parseAnnotation($this->property->getDocComment(), 'xmlNamespace'); if (!$namespace && $this->configuration) { $value = $this->getValue(); if (is_object($this->getValue())) { $classMeta = new ClassMetadata($this->getValue(), $this->configuration); $namespace = $classMeta->getXmlNamespace(); } elseif (is_array($value) && count($value) > 0) { $classMeta = new ClassMetadata($this->getValue()[0], $this->configuration); $namespace = $classMeta->getXmlNamespace(); } else { $classNamespace = $this->property->getDeclaringClass()->getNamespaceName(); $namespace = $this->configuration->getXmlNamespace($classNamespace); } } return $namespace; }
/** * @param ClassMetadata $class * * @return \SimpleXMLElement */ public function createRootNode(ClassMetadata $class) { $longNs = $class->getXmlNamespace(); $ns = $this->configuration->getShortXmlNamespace($longNs); $rootNodeName = $class->getXmlNodeName(); $xml = new \SimpleXMLElement('<' . $rootNodeName . '/>', LIBXML_NOERROR, false, $ns); if ($this->configuration->getDefaultXmlNamespace()) { $xml->addAttribute('xmlns:xmlns', $this->configuration->getDefaultXmlNamespace()); } $xmlNamespaces = $this->configuration->getAllShortXmlNamespaces(); if (count($xmlNamespaces) > 1 || count($xmlNamespaces) == 1 && key($xmlNamespaces) !== $this->configuration->getDefaultXmlNamespace()) { foreach ($xmlNamespaces as $longNs => $shortNs) { if ($longNs === $this->configuration->getDefaultXmlNamespace()) { continue; } $attrNs = $shortNs ? 'xmlns:xmlns:' . $shortNs : 'xmlns:xmlns'; if (!$xml->attributes()->{'xmlns:' . $shortNs}) { $xml->addAttribute($attrNs, $longNs); } } } return $xml; }
public function testNamespace() { $node = new TextNode(); $meta = new ClassMetadata($node); $this->assertEquals('com.example.testcase', $meta->getXmlNamespace()); }