[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/core/src/ -> lmbCollection.class.php (source)

   1  <?php
   2  /*

   3   * Limb PHP Framework

   4   *

   5   * @link http://limb-project.com

   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)

   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html

   8   */
   9  lmb_require('limb/core/src/lmbCollectionInterface.interface.php');
  10  lmb_require('limb/core/src/lmbArrayHelper.class.php');
  11  lmb_require('limb/core/src/lmbSet.class.php');
  12  
  13  /**

  14   * class lmbCollection.

  15   *

  16   * @package core

  17   * @version $Id$

  18   */
  19  class lmbCollection implements lmbCollectionInterface
  20  {
  21    protected $dataset;
  22    protected $iteratedDataset;
  23    protected $offset = 0;
  24    protected $limit = 0;
  25    protected $current;
  26    protected $valid = false;
  27  
  28    function __construct($array = array())
  29    {
  30      $this->dataset = $array;
  31    }
  32  
  33    static function concat()
  34    {
  35      $args = func_get_args();
  36      $result = array();
  37      foreach($args as $col)
  38      {
  39        foreach($col as $value)
  40          $result[] = $value;
  41      }
  42      return new lmbCollection($result);
  43    }
  44  
  45    function getArray()
  46    {
  47      $result = array();
  48      foreach($this as $object)
  49        $result[] = $object;
  50      return $result;
  51    }
  52  
  53    static function toFlatArray($iterator)
  54    {
  55      $result = array();
  56      foreach($iterator as $record)
  57      {
  58        if(is_object($record) && method_exists($record, 'export'))
  59          $result[] = $record->export();
  60        else
  61          $result[] = $record;
  62      }
  63      return $result;
  64    }
  65  
  66    function export()
  67    {
  68      return $this->dataset;
  69    }
  70  
  71    function sort($params)
  72    {
  73      if(count($this->dataset))
  74      {
  75        $this->dataset = lmbArrayHelper :: sortArray($this->dataset, $params, false);
  76        $this->iteratedDataset = null;
  77      }
  78      return $this;
  79    }
  80  
  81    function at($pos)
  82    {
  83      if(isset($this->dataset[$pos]))
  84        return $this->dataset[$pos];
  85    }
  86  
  87    function rewind()
  88    {
  89      $this->_setupIteratedDataset();
  90  
  91      $values = reset($this->iteratedDataset);
  92      $this->current = $this->_getCurrent($values);
  93      $this->key = key($this->iteratedDataset);
  94      $this->valid = $this->_isValid($values);
  95    }
  96  
  97    function next()
  98    {
  99      $values = next($this->iteratedDataset);
 100      $this->current = $this->_getCurrent($values);
 101      $this->key = key($this->iteratedDataset);
 102      $this->valid = $this->_isValid($values);
 103    }
 104  
 105    protected function _setupIteratedDataset()
 106    {
 107      if(!is_null($this->iteratedDataset))
 108        return;
 109  
 110      if(!$this->limit)
 111      {
 112        $this->iteratedDataset = $this->dataset;
 113        return;
 114      }
 115  
 116      if($this->offset < 0 || $this->offset >= count($this->dataset))
 117      {
 118        $this->iteratedDataset = array();
 119        return;
 120      }
 121  
 122      $to_splice_array = $this->dataset;
 123      $this->iteratedDataset = array_splice($to_splice_array, $this->offset, $this->limit);
 124  
 125      if(!$this->iteratedDataset)
 126        $this->iteratedDataset = array();
 127    }
 128  
 129    function valid()
 130    {
 131      return $this->valid;
 132    }
 133  
 134    function current()
 135    {
 136      return $this->current;
 137    }
 138  
 139    function key()
 140    {
 141      return $this->key;
 142    }
 143  
 144    function paginate($offset, $limit)
 145    {
 146      $this->iteratedDataset = null;
 147      $this->offset = $offset;
 148      $this->limit = $limit;
 149      return $this;
 150    }
 151  
 152    function getOffset()
 153    {
 154      return $this->offset;
 155    }
 156  
 157    function getLimit()
 158    {
 159      return $this->limit;
 160    }
 161  
 162    protected function _getCurrent($values)
 163    {
 164      if(is_object($values))
 165        return $values;
 166      else
 167        return new lmbSet($values);
 168    }
 169  
 170    protected function _isValid($values)
 171    {
 172      return (is_array($values) || is_object($values));
 173    }
 174  
 175    function add($item)
 176    {
 177      $this->dataset[] = $item;
 178      $this->iteratedDataset = null;
 179    }
 180  
 181    function isEmpty()
 182    {
 183      return sizeof($this->dataset) == 0;
 184    }
 185  
 186    //Countable interface

 187    function count()
 188    {
 189      return sizeof($this->dataset);
 190    }
 191    //

 192  
 193    function countPaginated()
 194    {
 195      $this->_setupIteratedDataset();
 196      return count($this->iteratedDataset);
 197    }
 198  
 199    //ArrayAccess interface

 200    function offsetExists($offset)
 201    {
 202      return !is_null($this->offsetGet($offset));
 203    }
 204  
 205    function offsetGet($offset)
 206    {
 207      if(is_numeric($offset))
 208        return $this->at((int)$offset);
 209    }
 210  
 211    function offsetSet($offset, $value)
 212    {
 213      if(!isset($offset))
 214        $this->add($value);
 215    }
 216  
 217    function offsetUnset($offset){}
 218    //end

 219  }
 220  ?>


Generated: Mon Sep 8 04:35:41 2008 Cross-referenced by PHPXref 0.7