/** * Iterates through a list of nodes and stores to each node in the * ConfigValueHolder * * @param mixed An array or an object that can be iterated over * @param AgaviXmlValueHolder The storage for the info from the nodes * @param bool Whether this list is the singular form of the parent node * * @author David Zülke <*****@*****.**> * @author Dominik del Bondio <*****@*****.**> * @since 0.11.0 */ protected function parseNodes($nodes, AgaviConfigValueHolder $parentVh, $isSingular = false) { foreach ($nodes as $node) { if ($node->nodeType == XML_ELEMENT_NODE && (!$node->namespaceURI || $node->namespaceURI == AgaviXmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_0_11)) { $vh = new AgaviConfigValueHolder(); $nodeName = $this->convertEncoding($node->localName); $vh->setName($nodeName); $parentVh->addChildren($nodeName, $vh); foreach ($node->attributes as $attribute) { if (!$attribute->namespaceURI || $attribute->namespaceURI == AgaviXmlConfigParser::NAMESPACE_AGAVI_ENVELOPE_0_11) { $vh->setAttribute($this->convertEncoding($attribute->localName), $this->convertEncoding($attribute->nodeValue)); } } // there are no child nodes so we set the node text contents as the value for the valueholder if ($node->getElementsByTagName('*')->length == 0) { $vh->setValue($this->convertEncoding($node->nodeValue)); } if ($node->hasChildNodes()) { $this->parseNodes($node->childNodes, $vh); } } } }
public function testIteratorIterface() { $vh = new AgaviConfigValueHolder(); $vhChild1 = new AgaviConfigValueHolder(); $vhChild2 = new AgaviConfigValueHolder(); $vhChild3 = new AgaviConfigValueHolder(); $vh->addChildren('child1', $vhChild1); $vh->addChildren('child2', $vhChild2); $vh->addChildren('child3', $vhChild3); $i = 1; foreach ($vh as $name => $child) { $this->assertSame('child' . $i, $name); $this->assertSame(${'vhChild' . $i}, $child); ++$i; } $vh2 = new AgaviConfigValueHolder(); $vh2->appendChildren($vhChild1); $vh2->appendChildren($vhChild2); $vh2->appendChildren($vhChild3); $i = 0; foreach ($vh2 as $id => $child) { $this->assertSame($i, $id); $this->assertSame(${'vhChild' . ($i + 1)}, $child); ++$i; } }
/** * Magic getter overload. * * @param string Name of the child . * * @return AgaviConfigValueHolder The child, if it exists. * * @author Dominik del Bondio <*****@*****.**> * @since 0.11.0 */ public function __get($name) { if (isset($this->_childs[$name])) { return $this->_childs[$name]; } else { $tagName = $name; $tagNameStart = ''; if (($lastUScore = strrpos($tagName, '_')) !== false) { $lastUScore++; $tagNameStart = substr($tagName, 0, $lastUScore); $tagName = substr($tagName, $lastUScore); } // check if the requested node was specified using the plural version // and create a "virtual" node which reflects the non existent plural node $singularName = $tagNameStart . AgaviInflector::singularize($tagName); if ($this->hasChildren($singularName)) { $vh = new AgaviConfigValueHolder(); $vh->setName($name); foreach ($this->_childs as $child) { if ($child->getName() == $singularName) { $vh->addChildren($singularName, $child); } } return $vh; } else { //throw new AgaviException('Node with the name ' . $name . ' does not exist ('.$this->getName().', '.implode(', ', array_keys($this->_childs)).')'); return null; } } }