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

Commit

Permalink
Merge branch 'feature/ImplementArrayAccessOnNodeList' of git://github…
Browse files Browse the repository at this point in the history
….com/BlaineSch/zf2 into feature/dom-nodelist-access
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -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-2012 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
Original file line number Diff line number Diff line change
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');
}
}
44 changes: 44 additions & 0 deletions test/QueryTest.php
Original file line number Diff line number Diff line change
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 6956129

Please sign in to comment.