diff --git a/composer.json b/composer.json index 4f290ad..6e1124e 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,13 @@ } }, "require": { - "php": ">=5.3.3" + "php": ">=5.3.3", + "zendframework/zend-stdlib": "self.version" }, "extra": { "branch-alias": { - "dev-master": "2.4-dev", - "dev-develop": "2.5-dev" + "dev-master": "2.2-dev", + "dev-develop": "2.3-dev" } }, "autoload-dev": { diff --git a/src/Css2Xpath.php b/src/Css2Xpath.php index b2e75fc..c2acf15 100644 --- a/src/Css2Xpath.php +++ b/src/Css2Xpath.php @@ -3,18 +3,14 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @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 + * + * @param int $key + * @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 + * + * @param int $key + * @return mixed + */ + public function offsetGet($key) + { + return $this->nodeList->item($key); + } + + /** + * ArrayAccess: set offset + * + * @param mixed $key + * @param mixed $value + * @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 + * + * @param mixed $key + * @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 7c37a01..3fbc10c 100644 --- a/src/Query.php +++ b/src/Query.php @@ -3,20 +3,18 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @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/Css2XpathTest.php b/test/Css2XpathTest.php index 587cc7d..00f6ed9 100644 --- a/test/Css2XpathTest.php +++ b/test/Css2XpathTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @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 */ diff --git a/test/NodeListTest.php b/test/NodeListTest.php index 6c91471..d249a82 100644 --- a/test/NodeListTest.php +++ b/test/NodeListTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @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 */ diff --git a/test/QueryTest.php b/test/QueryTest.php index 00d7098..af81594 100644 --- a/test/QueryTest.php +++ b/test/QueryTest.php @@ -3,7 +3,7 @@ * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository - * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @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 */ @@ -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]); + } }