Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop' of https://github.com/zendframework/zf2 into h…
Browse files Browse the repository at this point in the history
…otfix/zend-session-global-session-storage
  • Loading branch information
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -13,7 +13,8 @@
}
},
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"zendframework/zend-stdlib": "self.version"
},
"extra": {
"branch-alias": {
Expand Down
21 changes: 21 additions & 0 deletions src/Exception/BadMethodCallException.php
@@ -0,0 +1,21 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @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\Exception;

/**
* Zend_Dom Exceptions
*
* @category Zend
* @package Zend_Dom
*/
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{
}
48 changes: 47 additions & 1 deletion src/NodeList.php
Expand Up @@ -15,14 +15,15 @@
use DOMNodeList;
use DOMNode;
use Iterator;
use ArrayAccess;

/**
* Nodelist for DOM XPath query
*
* @package Zend_Dom
* @subpackage Query
*/
class NodeList implements Iterator, Countable
class NodeList implements Iterator, Countable, ArrayAccess
{
/**
* CSS Selector query
Expand Down Expand Up @@ -166,4 +167,49 @@ public function count()
{
return $this->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');
}
}
10 changes: 9 additions & 1 deletion src/Query.php
Expand Up @@ -12,6 +12,7 @@

use DOMDocument;
use DOMXPath;
use Zend\Stdlib\ErrorHandler;

/**
* Query DOM structures based on CSS selectors and/or XPath
Expand Down Expand Up @@ -314,6 +315,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;
}
}
44 changes: 44 additions & 0 deletions test/QueryTest.php
Expand Up @@ -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] = '<foobar />';
}


/**
* @expectedException Zend\Dom\Exception\BadMethodCallException
*/
public function testOffsetUnset()
{
$this->loadHtml();
$results = $this->query->execute('input');
$this->assertEquals(3, $results->count());

unset($results[2]);
}
}

0 comments on commit 64fe065

Please sign in to comment.