PageRenderTime 1581ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/web_app/src/fetcher/lmbFetcher.class.php

http://github.com/limb-php-framework/limb
PHP | 135 lines | 102 code | 20 blank | 13 comment | 15 complexity | d5fd86eff9fcd4c15b4cd7ca0edc2c7b MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 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. * abstract class lmbFetcher.
  12. *
  13. * @package web_app
  14. * @version $Id: lmbFetcher.class.php 7486 2009-01-26 19:13:20Z pachanga $
  15. */
  16. abstract class lmbFetcher
  17. {
  18. protected $decorators = array();
  19. protected $order_params = array();
  20. protected $offset = 0;
  21. protected $limit = 0;
  22. function __construct()
  23. {
  24. $this->_collectDecorators();
  25. }
  26. function setOffset($offset)
  27. {
  28. $this->offset = $offset;
  29. }
  30. function setLimit($limit)
  31. {
  32. $this->limit = $limit;
  33. }
  34. function setOrder($order)
  35. {
  36. if(is_array($order))
  37. {
  38. $this->order_params = $order;
  39. return;
  40. }
  41. else
  42. $this->order_params = self :: extractOrderPairsFromString($order);
  43. }
  44. function addDecorator($decorator, $params = array())
  45. {
  46. $this->decorators[] = array($decorator, $params);
  47. }
  48. protected function _applyDecorators($dataset)
  49. {
  50. $toolkit = lmbToolkit :: instance();
  51. foreach($this->decorators as $decorator_data)
  52. {
  53. $class_path = new lmbClassPath($decorator_data[0]);
  54. $dataset = $class_path->createObject(array($dataset));
  55. $this->_addParamsToDataset($dataset, $decorator_data[1]);
  56. }
  57. return $dataset;
  58. }
  59. protected function _addParamsToDataset($dataset, $params)
  60. {
  61. foreach($params as $param => $value)
  62. {
  63. $method = lmb_camel_case('set_'.$param, false);
  64. $dataset->$method($value);
  65. }
  66. }
  67. protected function _collectDecorators(){}
  68. function fetch()
  69. {
  70. $res = $this->_createDataSet();
  71. if(is_array($res))
  72. $dataset = new lmbCollection($res);
  73. elseif(is_object($res))
  74. $dataset = $res;
  75. else
  76. $dataset = new lmbCollection();
  77. $dataset = $this->_applyDecorators($dataset);
  78. if(is_array($this->order_params) && count($this->order_params))
  79. $dataset->sort($this->order_params);
  80. if($this->offset || $this->limit)
  81. {
  82. if(!$this->limit)
  83. $this->limit = $dataset->count();
  84. $dataset->paginate($this->offset, $this->limit);
  85. }
  86. return $dataset;
  87. }
  88. function fetchOne()
  89. {
  90. $dataset = $this->fetch();
  91. $dataset->rewind();
  92. if($dataset->valid())
  93. return $dataset->current();
  94. }
  95. static function extractOrderPairsFromString($order_string)
  96. {
  97. $order_items = explode(',', $order_string);
  98. $order_pairs = array();
  99. foreach($order_items as $order_pair)
  100. {
  101. $arr = explode('=', $order_pair);
  102. if(isset($arr[1]))
  103. {
  104. if(strtolower($arr[1]) == 'asc' || strtolower($arr[1]) == 'desc'
  105. || strtolower($arr[1]) == 'rand()')
  106. $order_pairs[$arr[0]] = strtoupper($arr[1]);
  107. else
  108. throw new lmbException('Wrong order type', array('order' => $arr[1]));
  109. }
  110. else
  111. $order_pairs[$arr[0]] = 'ASC';
  112. }
  113. return $order_pairs;
  114. }
  115. }