diff --git a/composer.json b/composer.json index 4f290ad..9c4ec21 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } }, "require": { - "php": ">=5.3.3" + "php": ">=5.3.3", + "zendframework/zend-stdlib": "self.version" }, "extra": { "branch-alias": { diff --git a/src/Css2Xpath.php b/src/Css2Xpath.php index 09d0f12..c2acf15 100644 --- a/src/Css2Xpath.php +++ b/src/Css2Xpath.php @@ -5,16 +5,12 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Dom */ namespace Zend\Dom; /** * Transform CSS selectors to XPath - * - * @package Zend_Dom - * @subpackage Query */ class Css2Xpath { diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php new file mode 100644 index 0000000..b613b24 --- /dev/null +++ b/src/Exception/BadMethodCallException.php @@ -0,0 +1,17 @@ +nodeList->length; } + + /** + * ArrayAccess: offset exists + * + * @return bool + */ + public function offsetExists($key) + { + if (in_array($key, range(0, $this->nodeList->length - 1)) && $this->nodeList->length > 0) { + return true; + } + return false; + } + + /** + * ArrayAccess: get offset + * + * @return mixed + */ + public function offsetGet($key) + { + return $this->nodeList->item($key); + } + + /** + * ArrayAccess: set offset + * + * @return void + * @throws Exception\BadMethodCallException when attemptingn to write to a read-only item + */ + public function offsetSet($key, $value) + { + throw new Exception\BadMethodCallException('Attempting to write to a read-only list'); + } + + /** + * ArrayAccess: unset offset + * + * @return void + * @throws Exception\BadMethodCallException when attemptingn to unset a read-only item + */ + public function offsetUnset($key) + { + throw new Exception\BadMethodCallException('Attempting to unset on a read-only list'); + } } diff --git a/src/Query.php b/src/Query.php index f872f90..3fbc10c 100644 --- a/src/Query.php +++ b/src/Query.php @@ -5,18 +5,16 @@ * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License - * @package Zend_Dom */ namespace Zend\Dom; use DOMDocument; use DOMXPath; +use Zend\Stdlib\ErrorHandler; /** * Query DOM structures based on CSS selectors and/or XPath - * - * @package Zend_Dom */ class Query { @@ -314,6 +312,13 @@ protected function getNodeList($document, $xpathQuery) : $xpath->registerPHPFunctions($this->xpathPhpFunctions); } $xpathQuery = (string) $xpathQuery; - return $xpath->query($xpathQuery); + + ErrorHandler::start(); + $nodeList = $xpath->query($xpathQuery); + $error = ErrorHandler::stop(); + if ($error) { + throw $error; + } + return $nodeList; } } diff --git a/test/QueryTest.php b/test/QueryTest.php index f22c709..af81594 100644 --- a/test/QueryTest.php +++ b/test/QueryTest.php @@ -356,4 +356,48 @@ public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttac $this->setExpectedException("\Zend\Dom\Exception\RuntimeException"); $this->query->queryXpath('/'); } + + public function testOffsetExists() + { + $this->loadHtml(); + $results = $this->query->execute('input'); + + $this->assertEquals(3, $results->count()); + $this->assertFalse($results->offsetExists(3)); + $this->assertTrue($results->offsetExists(2)); + } + + public function testOffsetGet() + { + $this->loadHtml(); + $results = $this->query->execute('input'); + + $this->assertEquals(3, $results->count()); + $this->assertEquals('login', $results[2]->getAttribute('id')); + } + + /** + * @expectedException Zend\Dom\Exception\BadMethodCallException + */ + public function testOffsetSet() + { + $this->loadHtml(); + $results = $this->query->execute('input'); + $this->assertEquals(3, $results->count()); + + $results[0] = ''; + } + + + /** + * @expectedException Zend\Dom\Exception\BadMethodCallException + */ + public function testOffsetUnset() + { + $this->loadHtml(); + $results = $this->query->execute('input'); + $this->assertEquals(3, $results->count()); + + unset($results[2]); + } }