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

Commit

Permalink
[ZF-9396] Zend_Paginator:
Browse files Browse the repository at this point in the history
- Fixed implemented the ArrayAccess interface.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22635 44c647ce-9c0f-0410-b52a-842ac1e357ba
  • Loading branch information
ramon authored and weierophinney committed Jul 22, 2010
2 parents c73202b + a96d3c6 commit a950ec0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/SerializableLimitIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class SerializableLimitIterator extends \LimitIterator implements \Serializable
class SerializableLimitIterator extends \LimitIterator implements \Serializable, \ArrayAccess
{

/**
Expand Down Expand Up @@ -87,4 +87,62 @@ public function unserialize($data)
$this->seek($dataArr['pos']+$dataArr['offset']);
}

/**
* Returns value of the Iterator
*
* @param int $offset
* @return mixed
*/
public function offsetGet($offset)
{
$currentOffset = $this->key();
$this->seek($offset);
$current = $this->current();
$this->seek($currentOffset);
return $current;
}

/**
* Does nothing
* Required by the ArrayAccess implementation
*
* @param int $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
}

/**
* Determine if a value of Iterator is set and is not NULL
*
* @param int $offset
*/
public function offsetExists($offset)
{
if ($offset > 0 && $offset < $this->_count) {
try {
$currentOffset = $this->key();
$this->seek($offset);
$current = $this->current();
$this->seek($currentOffset);
return null !== $current;
} catch (OutOfBoundsException $e) {
// reset position in case of exception is assigned null
$this->seek($currentOffset);
return false;
}
}
return false;
}

/**
* Does nothing
* Required by the ArrayAccess implementation
*
* @param int $offset
*/
public function offsetUnset($offset)
{
}
}
21 changes: 21 additions & 0 deletions test/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,27 @@ public function testInvalidDataInConstructor_ThrowsException()

$p = new Paginator\Paginator(array());
}

/**
* @group ZF-9396
*/
public function testArrayAccessInClassSerializableLimitIterator()
{
$iterator = new ArrayIterator(array('zf9396', 'foo', null));
$paginator = Zend_Paginator::factory($iterator);

$this->assertEquals('zf9396', $paginator->getItem(1));

$items = $paginator->getAdapter()
->getItems(0, 10);

$this->assertEquals('foo', $items[1]);
$this->assertEquals(0, $items->key());
$this->assertFalse(isset($items[2]));
$this->assertTrue(isset($items[1]));
$this->assertFalse(isset($items[3]));
$this->assertEquals(0, $items->key());
}
}

class TestArrayAggregate implements Paginator\AdapterAggregate
Expand Down

0 comments on commit a950ec0

Please sign in to comment.