[limb-svn] r6522 - in 3.x/trunk/limb/core: src tests/cases

svn at limb-project.com svn at limb-project.com
Sat Nov 17 14:14:37 MSK 2007


Author: serega
Date: 2007-11-17 14:14:37 +0300 (Sat, 17 Nov 2007)
New Revision: 6522
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6522

Added:
   3.x/trunk/limb/core/src/lmbArrayIterator.class.php
   3.x/trunk/limb/core/tests/cases/lmbArrayIteratorTest.class.php
Log:
-- new lmbArrayIterator class added that is an advanced version of SPL ArrayIterator supporting lmbCollectionInterface

Added: 3.x/trunk/limb/core/src/lmbArrayIterator.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbArrayIterator.class.php	                        (rev 0)
+++ 3.x/trunk/limb/core/src/lmbArrayIterator.class.php	2007-11-17 11:14:37 UTC (rev 6522)
@@ -0,0 +1,114 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/core/src/lmbCollectionInterface.interface.php');
+
+/**
+ * class WactArrayIterator.
+ *
+ * @package wact
+ * @version $Id: WactArrayIterator.class.php 6386 2007-10-05 14:22:21Z serega $
+ */
+class lmbArrayIterator extends ArrayIterator implements lmbCollectionInterface
+{
+  public $position = 0;
+  public $offset = 0;
+  public $limit = 0;
+  protected $paginated = false;
+
+  function rewind()
+  {
+    $this->position = 0;
+    parent :: rewind();
+    
+    // goto offset item if possible
+    if($this->offset)
+    {
+      try
+      {
+        $this->seek($this->offset);
+      }
+      catch(OutOfBoundsException $e)
+      {
+        $this->seek($this->count()-1);
+        $this->next();
+      }
+    }
+  }
+
+  function next()
+  {
+    $this->position++;
+    return parent :: next();
+  }
+
+  function valid()
+  {
+    if($this->limit && ($this->position >= $this->limit))
+      return false;
+    return parent :: valid();
+  }
+
+  function getOffset()
+  {
+    return $this->offset;
+  }
+
+  function getLimit()
+  {
+    return $this->limit;
+  }
+
+  function paginate($offset, $limit)
+  {
+    $this->offset = $offset;
+    $this->limit = $limit;
+    $this->paginated = true;
+  }
+  
+  function getArray()
+  {
+    return $this->getArrayCopy();
+  }
+  
+  function at($pos)
+  {
+    try
+    {
+      $this->seek($pos);
+      return $this->current();
+    }
+    catch(OutOfBoundsException $e)
+    {
+      $this->seek($this->count()-1);
+      return null;
+    }
+  }
+  
+  function sort($params)
+  {
+    throw new lmbException('Doesn\'t support sorting since ArrayIterator is immutable object');
+  }
+
+  function countPaginated()
+  {
+    if(!$this->paginated)
+      return $this->count();
+
+    $total = $this->count();
+    
+    if($total <= $this->offset || $this->offset < 0)
+      return 0;
+    
+    if(($this->offset + $this->limit) < $total)
+      return $this->limit;
+    else
+      return $total - $this->offset;
+  }
+}
+

Added: 3.x/trunk/limb/core/tests/cases/lmbArrayIteratorTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbArrayIteratorTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/core/tests/cases/lmbArrayIteratorTest.class.php	2007-11-17 11:14:37 UTC (rev 6522)
@@ -0,0 +1,94 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+lmb_require('limb/core/src/lmbArrayIterator.class.php');
+
+class lmbArrayIteratorTest extends UnitTestCase
+{
+  function testEmptyIterator()
+  {
+    $iterator = new lmbArrayIterator(array());
+    $iterator->rewind();
+    $this->assertFalse($iterator->valid());
+  }
+
+  function testIterate()
+  {
+    $data = array (array('x' => 1,'y' => 2),
+                   array('x' => 3,'y' => 4),
+                   array('x' => 5,'y' => 6));
+
+    $iterator = new lmbArrayIterator(array('Ivan', 'Pavel', 'Serega'));
+    $iterator->rewind();
+    $this->assertTrue($iterator->valid());
+
+    $this->assertEqual($iterator->current(), 'Ivan');
+    $iterator->next();
+    $this->assertTrue($iterator->valid());
+    $this->assertEqual($iterator->current(), 'Pavel');
+  }
+
+  function testIterateOver()
+  {
+    $iterator = new lmbArrayIterator(array('Ivan'));
+    $iterator->rewind();
+    $iterator->next();
+    $iterator->next();
+    $this->assertFalse($iterator->valid());
+    $this->assertNull($iterator->current());
+  }
+  
+  function testIterateWithPagination()
+  {
+    $iterator = new lmbArrayIterator(array('a', 'b', 'c', 'd', 'e'));
+    $iterator->paginate($offset = 0, $limit = 2);
+    $this->assertEqual($iterator->count(), 5);
+    $this->assertEqual($iterator->countPaginated(), $limit);
+
+    $iterator->rewind();
+    $this->assertEqual($iterator->current(), 'a');
+    $iterator->next();
+    $this->assertEqual($iterator->current(), 'b');
+  }
+
+  function testIterateWithPaginationNonZeroOffset()
+  {
+    $iterator = new lmbArrayIterator(array('a', 'b', 'c', 'd', 'e'));
+    $iterator->paginate($offset = 2, $limit = 2);
+
+    $iterator->rewind();
+    $this->assertEqual($iterator->current(), 'c');
+    $iterator->next();
+    $this->assertEqual($iterator->current(), 'd');
+  }
+
+  function testPaginateWithOutOfBounds()
+  {
+    $iterator = new lmbArrayIterator(array('a', 'b', 'c', 'd', 'e'));
+    $iterator->paginate($offset = 5, $limit = 2);
+
+    $this->assertEqual($iterator->count(), 5);
+    $this->assertEqual($iterator->countPaginated(), 0);
+
+    $iterator->rewind();
+    $this->assertFalse($iterator->valid());
+  }
+
+  function testPaginateWithOffsetLessThanZero()
+  {
+    $iterator = new lmbArrayIterator(array('a', 'b', 'c', 'd', 'e'));
+    $iterator->paginate($offset = -1, $limit = 2);
+
+    $this->assertEqual($iterator->count(), 5);
+    $this->assertEqual($iterator->countPaginated(), 0);
+
+    $iterator->rewind();
+    $this->assertFalse($iterator->valid());
+  }
+}
+



More information about the limb-svn mailing list