Просмотр исходного кода

[ZF-9396] Zend_Paginator:

- Fixed implemented the ArrayAccess interface.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22635 44c647ce-9c0f-0410-b52a-842ac1e357ba
ramon 15 лет назад
Родитель
Сommit
36a4139cd7
2 измененных файлов с 80 добавлено и 1 удалено
  1. 59 1
      library/Zend/Paginator/SerializableLimitIterator.php
  2. 21 0
      tests/Zend/PaginatorTest.php

+ 59 - 1
library/Zend/Paginator/SerializableLimitIterator.php

@@ -25,7 +25,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 Zend_Paginator_SerializableLimitIterator extends LimitIterator implements Serializable
+class Zend_Paginator_SerializableLimitIterator extends LimitIterator implements Serializable, ArrayAccess
 {
 
     /**
@@ -80,4 +80,62 @@ class Zend_Paginator_SerializableLimitIterator extends LimitIterator implements
         $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 - 0
tests/Zend/PaginatorTest.php

@@ -997,6 +997,27 @@ class Zend_PaginatorTest extends PHPUnit_Framework_TestCase
 
         $p = new Zend_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 Zend_Paginator_TestArrayAggregate implements Zend_Paginator_AdapterAggregate