/** * * @param \SimpleXmlElement $xml * @param object $obj * * @throws \Exception */ private function appendObjectProperties(\SimpleXMLElement $xml, $obj) { $clsMeta = new ClassMetadata($obj, $this->configuration); foreach ($clsMeta->getProperties() as $prop) { $propName = $prop->getName(); $nodeName = "xmlns:" . $prop->getXmlNodeName(); $value = $obj->{$propName}; if (is_object($value)) { $this->appendObjectProperties($xml->addChild($nodeName), $value); } elseif (is_string($value) && $prop->isScalarValue()) { $xml[0] = $value; } elseif (is_string($value) && $prop->isAttribute()) { $xml->addAttribute($prop->getName(), $value); } elseif (is_string($value)) { $xml->addChild($nodeName, $value); } elseif ((is_int($value) || is_float($value)) && $prop->isScalarValue()) { $xml[0] = (string) $value; } elseif (is_null($value)) { continue; } elseif (is_array($value)) { if (count($value) == 0) { continue; } foreach ($value as $itemValue) { if (is_object($itemValue)) { $this->appendObjectProperties($xml->addChild($nodeName), $itemValue); } else { throw new \Exception("Failed to serialize. Only object node arrays supported"); } } } else { throw new \Exception("Not implemented yet. Prop: {$propName} cannot be converted to xml node"); } } }
public function testNamespacedNodeName() { $configuration = new Configuration(); $configuration->addNamespace('com.example.property', 'XmlSerializer\\Fixtures\\Metadata'); $classMeta = new ClassMetadata(new PropertyMeta(), $configuration); $this->assertEquals('com.example.property', $classMeta->getProperty('basicNode')->getXmlNamespace()); $this->assertEquals('ns1:basicNode', $classMeta->getProperty('basicNode')->getXmlNodeName()); }
public function getValue() { if (!$this->parentClass) { throw new \Exception('Invalid method call. Object reference is not found - property value cannot be obtained'); } return $this->property->getValue($this->parentClass->getObject()); }
/** * @param XmlElement $xml * @param string $rootClassName * * @return mixed * @throws \Exception * */ private function xmlToObj(XmlElement $xml, $rootClassName = '') { if (!$rootClassName) { $rootClassName = $xml->getClassName($this->configuration); // todo: use node name index instead } $document = new $rootClassName(); $clsMeta = new ClassMetadata($document); $refCls = new \ReflectionClass($document); foreach ($xml->getChildrenNodes() as $child) { $prop = $child->getName(); if (count($child->getChildrenNodes()) > 0) { if (isset($document->{$prop}) && !is_array($document->{$prop})) { $document->{$prop} = [$document->{$prop}]; } else { if ($clsMeta->hasProperty($child->getName())) { $propMeta = $clsMeta->getProperty($child->getName()); $propClass = $propMeta->getClass(); } else { $propClass = '\\stdClass'; } if (isset($document->{$prop}) && is_array($document->{$prop})) { $document->{$prop}[] = $this->xmlToObj($child, $propClass); } else { $document->{$prop} = $this->xmlToObj($child, $propClass); } } } elseif (count($child->getAttributes()) > 0) { if ($clsMeta->hasProperty($child->getName()) && $clsMeta->hasClassProperty($child->getName())) { $propClassName = $clsMeta->getProperty($child->getName())->getClass(); // $this->parseVarAnnotation($refCls->getProperty($child->getName())); } else { $propClassName = '\\stdClass'; } $document->{$prop} = new $propClassName(); $document->{$prop}->value = $child->__toString(); /** @var XMLElement $attr */ foreach ($child->getAttributes() as $attr) { $attrName = $attr->getName(); $document->{$prop}->{$attrName} = $attr->__toString(); } } else { $document->{$prop} = $child->__toString(); } } return $document; }
public function testNamespace() { $node = new TextNode(); $meta = new ClassMetadata($node); $this->assertEquals('com.example.testcase', $meta->getXmlNamespace()); }