/** * @param \tomzx\HtmlParser\Node $node * @return array */ protected function parseNode(Node $node) { $statements = $node->getChildren(); foreach ($node->getChildren() as $child) { $statements[] = $this->parseNode($child); } return $statements; }
public function testChildrenManipulation() { $node = new Node(); $node->setChildren(['foo']); $this->assertCount(1, $node->getChildren()); $this->assertEquals(['foo'], $node->getChildren()); $node->clearChildren(); $this->assertEmpty($node->getChildren()); $node->setChildAtIndex($child1 = new Node(), 2); $this->assertCount(1, $children = $node->getChildren()); $this->assertEquals($child1, reset($children)); $this->assertEquals(2, key($children)); }
public function render(Node $node) { echo str_repeat('--', $node->getDepth()) . $node->getType() . "\n"; foreach ($node->getChildren() as $child) { $this->render($child); } }
/** * Returns siblings of the node, optionally including the node itself. * * @param bool $includeSelf If true, the node itself will be included in the resulting * array. In either case, the sort order will be correct. * This argument is deprecated and will be removed in v2.0 * * Note: The argument is deprecated and will be removed in version 2; please * use getSiblingsAndSelf(). * * @return Node[] */ public function getSiblings($includeSelf = false) { $siblings = array(); foreach ($this->parent->getChildren() as $child) { if ($includeSelf || $child->getId() != $this->getId()) { $siblings[] = $child; } } return $siblings; }
public function getChildren() { if (!$this->areBaseChildrenAccessible()) { return parent::getChildren(); } $names = array_keys(array_merge($this->getBaseNode()->getChildren(), $this->childNodes)); $nodes = []; foreach ($names as $name) { $nodes[$name] = $this->getChild($name); } return $nodes; }
protected function applyArgs(Node $node) { if ($node instanceof TextNode) { $node->setContent($this->interpolate($node->getContent())); } else { if ($node instanceof Element) { foreach ($node->getAttributes() as $attr => $value) { $node->setAttribute($attr, $this->interpolate($value)); } } } foreach ($node->getChildren() as $child) { $this->applyArgs($child); } }
/** * Find matching nodes among node children * * @param Node $node * * @param string $ignoreRoot * * @return \USync\AST\NodeInterface[] */ public function find(Node $node, $ignoreRoot = true) { $ret = []; if ($ignoreRoot && $node->isRoot()) { foreach ($node->getChildren() as $child) { $this->_find($ret, $child); } } else { $this->_find($ret, $node); } return $ret; }
/** * Returns an array representation of the given Node structure. * * @param Node $node * * @return array */ public function toArray(Node $node) { return array('name' => $node->getName(), 'value' => $node->getValue(), 'attributes' => $node->getAttributes(), 'children' => array_map(function (Node $child) { return $this->toArray($child); }, $node->getChildren())); }
/** * Create a new node and its children * * @param object Hierarchy $newHierarchy * @param object Node $oldNode * @param optional object Id $parentId * @return void * @access protected * @since 4/17/08 */ protected function addNode(Hierarchy $newHierarchy, Node $oldNode, Id $parentId = null) { // If it has already been created, get it and try to set its parent try { $newNode = $newHierarchy->getNode($oldNode->getId()); if (!is_null($parentId)) { try { $newNode->addParent($parentId); } catch (Exception $e) { // Do nothing if the child already exists if ($e->getMessage() != "A child with the given id already exists!") { throw $e; } } } } catch (UnknownIdException $e) { if (is_null($parentId)) { $newNode = $newHierarchy->createRootNode($oldNode->getId(), $oldNode->getType(), $oldNode->getDisplayName(), $oldNode->getDescription()); } else { $newNode = $newHierarchy->createNode($oldNode->getId(), $parentId, $oldNode->getType(), $oldNode->getDisplayName(), $oldNode->getDescription()); } $this->nodeStatus->updateStatistics(); } $oldChildren = $oldNode->getChildren(); while ($oldChildren->hasNext()) { $oldChild = $oldChildren->next(); $this->addNode($newHierarchy, $oldChild, $newNode->getId()); } }
/** * @return \RecursiveIteratorIterator */ public function getIterator() { return new \RecursiveIteratorIterator($this->head->getChildren(), \RecursiveIteratorIterator::SELF_FIRST); }
/** * Write an individual type to the stream. * * @param resource $fPtr Stream pointer * @param int $tagType Type of tag to write * @param Node $node Node containing value to write * * @return bool */ private function writeType($fPtr, $tagType, Node $node) { switch ($tagType) { case Tag::TAG_BYTE: // Signed byte (8 bit) return $this->dataHandler->putTAGByte($fPtr, $node->getValue()); case Tag::TAG_SHORT: // Signed short (16 bit, big endian) return $this->dataHandler->putTAGShort($fPtr, $node->getValue()); case Tag::TAG_INT: // Signed integer (32 bit, big endian) return $this->dataHandler->putTAGInt($fPtr, $node->getValue()); case Tag::TAG_LONG: // Signed long (64 bit, big endian) return $this->dataHandler->putTAGLong($fPtr, $node->getValue()); case Tag::TAG_FLOAT: // Floating point value (32 bit, big endian, IEEE 754-2008) return $this->dataHandler->putTAGFloat($fPtr, $node->getValue()); case Tag::TAG_DOUBLE: // Double value (64 bit, big endian, IEEE 754-2008) return $this->dataHandler->putTAGDouble($fPtr, $node->getValue()); case Tag::TAG_BYTE_ARRAY: // Byte array return $this->dataHandler->putTAGByteArray($fPtr, $node->getValue()); case Tag::TAG_STRING: // String return $this->dataHandler->putTAGString($fPtr, $node->getValue()); case Tag::TAG_INT_ARRAY: // Byte array return $this->dataHandler->putTAGIntArray($fPtr, $node->getValue()); case Tag::TAG_LIST: // List if (!($this->dataHandler->putTAGByte($fPtr, $node->getPayloadType()) && $this->dataHandler->putTAGInt($fPtr, count($node->getChildren())))) { return false; } foreach ($node->getChildren() as $childNode) { if (!$this->writeType($fPtr, $node->getPayloadType(), $childNode)) { return false; } } return true; case Tag::TAG_COMPOUND: // Compound foreach ($node->getChildren() as $childNode) { if (!$this->writeTag($fPtr, $childNode)) { return false; } } if (!$this->writeType($fPtr, Tag::TAG_END, new Node())) { return false; } return true; case Tag::TAG_END: // End tag return is_int(fwrite($fPtr, "")); } }
/** * @test */ public function getChildrenReturnsEmptyArrayWhenNoChildNodesExist() { $parent = new Node(array('id' => 52)); $this->assertSame(array(), $parent->getChildren()); }