[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/web_app/src/fetcher/ -> lmbFetcher.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/lmbCollection.class.php');
  10  
  11  /**

  12   * abstract class lmbFetcher.

  13   *

  14   * @package web_app

  15   * @version $Id: lmbFetcher.class.php 6025 2007-06-29 08:49:08Z serega $

  16   */
  17  abstract class lmbFetcher
  18  {
  19    protected $decorators = array();
  20    protected $order_params = array();
  21    protected $offset = 0;
  22    protected $limit = 0;
  23  
  24    function __construct()
  25    {
  26      $this->_collectDecorators();
  27    }
  28  
  29    function setOffset($offset)
  30    {
  31      $this->offset = $offset;
  32    }
  33  
  34    function setLimit($limit)
  35    {
  36      $this->limit = $limit;
  37    }
  38  
  39    function setOrder($order)
  40    {
  41      if(is_array($order))
  42      {
  43        $this->order_params = $order;
  44        return;
  45      }
  46      else
  47        $this->order_params = self :: extractOrderPairsFromString($order);
  48    }
  49  
  50    function addDecorator($decorator, $params = array())
  51    {
  52      $this->decorators[] = array($decorator, $params);
  53    }
  54  
  55    protected function _applyDecorators($dataset)
  56    {
  57      $toolkit = lmbToolkit :: instance();
  58  
  59      foreach($this->decorators as $decorator_data)
  60      {
  61        $class_path = new lmbClassPath($decorator_data[0]);
  62        $dataset = $class_path->createObject(array($dataset));
  63        $this->_addParamsToDataset($dataset, $decorator_data[1]);
  64      }
  65      return $dataset;
  66    }
  67  
  68    protected function _addParamsToDataset($dataset, $params)
  69    {
  70      foreach($params as $param => $value)
  71      {
  72        $method = lmb_camel_case('set_'.$param, false);
  73        $dataset->$method($value);
  74      }
  75    }
  76  
  77    protected function _collectDecorators(){}
  78  
  79    function fetch()
  80    {
  81      $res = $this->_createDataSet();
  82  
  83      if(is_array($res))
  84        $dataset = new lmbCollection($res);
  85      elseif(is_object($res))
  86        $dataset = $res;
  87      else
  88        $dataset = new lmbCollection();
  89  
  90      $dataset = $this->_applyDecorators($dataset);
  91      if(is_array($this->order_params) && count($this->order_params))
  92        $dataset->sort($this->order_params);
  93  
  94      if($this->offset || $this->limit)
  95      {
  96        if(!$this->limit)
  97          $this->limit = $dataset->count();
  98  
  99        $dataset->paginate($this->offset, $this->limit);
 100      }
 101      return $dataset;
 102    }
 103  
 104    function fetchOne()
 105    {
 106      $dataset = $this->fetch();
 107      $dataset->rewind();
 108      if($dataset->valid())
 109        return $dataset->current();
 110    }
 111  
 112    /**

 113     * @deprecated

 114     * @see fetch()

 115     */
 116    function getDataSet()
 117    {
 118      return $this->fetch();
 119    }
 120  
 121    /**

 122     * @deprecated

 123     * @see fetchOne()

 124     */
 125    function getFirstRecord()
 126    {
 127      return $this->fetchOne();
 128    }
 129  
 130    static function extractOrderPairsFromString($order_string)
 131    {
 132      $order_items = explode(',', $order_string);
 133      $order_pairs = array();
 134      foreach($order_items as $order_pair)
 135      {
 136        $arr = explode('=', $order_pair);
 137  
 138        if(isset($arr[1]))
 139        {
 140          if(strtolower($arr[1]) == 'asc' || strtolower($arr[1]) == 'desc'
 141             || strtolower($arr[1]) == 'rand()')
 142            $order_pairs[$arr[0]] = strtoupper($arr[1]);
 143          else
 144            throw new lmbException('Wrong order type', array('order' => $arr[1]));
 145        }
 146        else
 147          $order_pairs[$arr[0]] = 'ASC';
 148      }
 149  
 150      return $order_pairs;
 151    }
 152  }
 153  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7