public function testCanAddItemsAndUseAccessors() { $firstNode = new XPathDocument_Dom_Text($this->_xpath->query('//p[@class="title"]/a/text()')->item(0)); $secondNode = new XPathDocument_Dom_Element($this->_xpath->query('//p[@class="title"]/a')->item(1)); $thirdNode = new XPathDocument_Dom_Attr($this->_xpath->query('//p[@class="title"]/a/attribute::href')->item(2)); $list = new XPathDocument_Dom_List(); $list->add($firstNode); $list->add($secondNode); $list->add($thirdNode); $this->assertEquals(count($list), 3); $this->assertTrue($list->current() instanceof XPathDocument_Dom_Text); $this->assertEquals($list->key(), 0); $list->next(); $this->assertEquals($list->key(), 1); $list->rewind(); $this->assertEquals($list->key(), 0); $this->assertTrue($list->valid()); $this->assertTrue($list->offsetExists(2)); $this->assertFalse($list->offsetExists(3)); $this->assertEquals($list->offsetGet(2)->getText(), 'http://www.mickeysfishing.com/'); $list->offsetSet(3, 'Test'); $this->assertTrue($list->offsetExists(3)); $this->assertEquals($list->offsetGet(3), 'Test'); $list->offsetUnset(3); $this->assertFalse($list->offsetExists(3)); $this->assertEquals(count($list->toArray()), 3); }
/** * @constructor * @param DOMNodeList $nodes * @param DOMDocument $dom * Package all of the DOM* classes into their XPathDocument equivalents, so that we can * continue to extend DOMDocument. * @throws Exception * @return \XPathDocument_Dom_Package */ public function __construct(DOMNodeList $nodes, DOMDocument $dom) { // Create the list that we'll populate. $list = new XPathDocument_Dom_List(); // Loop through all of the nodes, injecting each one into XPathDocument_Dom_List. foreach ($nodes as $node) { // Converts things like DOMElement into XPathDocument_Dom_Element. $className = get_class($node); $className = str_replace('DOM', '', $className); $className = sprintf('XPathDocument_Dom_%s', $className); // If this class does not exist, then throw an exception. if (!class_exists($className)) { throw new Exception('Cannot find DOM class: ' . $className); } // Package the DOM class into a special XPathDocument class representing the DOM. $class = new $className($node); $class->injectDom($dom); $list->add($class); } $this->_list = $list; }