BaseDataProvider.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\data;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\base\InvalidArgumentException;
  11. /**
  12. * BaseDataProvider provides a base class that implements the [[DataProviderInterface]].
  13. *
  14. * For more details and usage information on BaseDataProvider, see the [guide article on data providers](guide:output-data-providers).
  15. *
  16. * @property int $count The number of data models in the current page. This property is read-only.
  17. * @property array $keys The list of key values corresponding to [[models]]. Each data model in [[models]] is
  18. * uniquely identified by the corresponding key value in this array.
  19. * @property array $models The list of data models in the current page.
  20. * @property Pagination|false $pagination The pagination object. If this is false, it means the pagination is
  21. * disabled. Note that the type of this property differs in getter and setter. See [[getPagination()]] and
  22. * [[setPagination()]] for details.
  23. * @property Sort|bool $sort The sorting object. If this is false, it means the sorting is disabled. Note that
  24. * the type of this property differs in getter and setter. See [[getSort()]] and [[setSort()]] for details.
  25. * @property int $totalCount Total number of possible data models.
  26. *
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @since 2.0
  29. */
  30. abstract class BaseDataProvider extends Component implements DataProviderInterface
  31. {
  32. /**
  33. * @var int Number of data providers on the current page. Used to generate unique IDs.
  34. */
  35. private static $counter = 0;
  36. /**
  37. * @var string an ID that uniquely identifies the data provider among all data providers.
  38. * Generated automatically the following way in case it is not set:
  39. *
  40. * - First data provider ID is empty.
  41. * - Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc.
  42. */
  43. public $id;
  44. private $_sort;
  45. private $_pagination;
  46. private $_keys;
  47. private $_models;
  48. private $_totalCount;
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. if ($this->id === null) {
  56. if (self::$counter > 0) {
  57. $this->id = 'dp-' . self::$counter;
  58. }
  59. self::$counter++;
  60. }
  61. }
  62. /**
  63. * Prepares the data models that will be made available in the current page.
  64. * @return array the available data models
  65. */
  66. abstract protected function prepareModels();
  67. /**
  68. * Prepares the keys associated with the currently available data models.
  69. * @param array $models the available data models
  70. * @return array the keys
  71. */
  72. abstract protected function prepareKeys($models);
  73. /**
  74. * Returns a value indicating the total number of data models in this data provider.
  75. * @return int total number of data models in this data provider.
  76. */
  77. abstract protected function prepareTotalCount();
  78. /**
  79. * Prepares the data models and keys.
  80. *
  81. * This method will prepare the data models and keys that can be retrieved via
  82. * [[getModels()]] and [[getKeys()]].
  83. *
  84. * This method will be implicitly called by [[getModels()]] and [[getKeys()]] if it has not been called before.
  85. *
  86. * @param bool $forcePrepare whether to force data preparation even if it has been done before.
  87. */
  88. public function prepare($forcePrepare = false)
  89. {
  90. if ($forcePrepare || $this->_models === null) {
  91. $this->_models = $this->prepareModels();
  92. }
  93. if ($forcePrepare || $this->_keys === null) {
  94. $this->_keys = $this->prepareKeys($this->_models);
  95. }
  96. }
  97. /**
  98. * Returns the data models in the current page.
  99. * @return array the list of data models in the current page.
  100. */
  101. public function getModels()
  102. {
  103. $this->prepare();
  104. return $this->_models;
  105. }
  106. /**
  107. * Sets the data models in the current page.
  108. * @param array $models the models in the current page
  109. */
  110. public function setModels($models)
  111. {
  112. $this->_models = $models;
  113. }
  114. /**
  115. * Returns the key values associated with the data models.
  116. * @return array the list of key values corresponding to [[models]]. Each data model in [[models]]
  117. * is uniquely identified by the corresponding key value in this array.
  118. */
  119. public function getKeys()
  120. {
  121. $this->prepare();
  122. return $this->_keys;
  123. }
  124. /**
  125. * Sets the key values associated with the data models.
  126. * @param array $keys the list of key values corresponding to [[models]].
  127. */
  128. public function setKeys($keys)
  129. {
  130. $this->_keys = $keys;
  131. }
  132. /**
  133. * Returns the number of data models in the current page.
  134. * @return int the number of data models in the current page.
  135. */
  136. public function getCount()
  137. {
  138. return count($this->getModels());
  139. }
  140. /**
  141. * Returns the total number of data models.
  142. * When [[pagination]] is false, this returns the same value as [[count]].
  143. * Otherwise, it will call [[prepareTotalCount()]] to get the count.
  144. * @return int total number of possible data models.
  145. */
  146. public function getTotalCount()
  147. {
  148. if ($this->getPagination() === false) {
  149. return $this->getCount();
  150. } elseif ($this->_totalCount === null) {
  151. $this->_totalCount = $this->prepareTotalCount();
  152. }
  153. return $this->_totalCount;
  154. }
  155. /**
  156. * Sets the total number of data models.
  157. * @param int $value the total number of data models.
  158. */
  159. public function setTotalCount($value)
  160. {
  161. $this->_totalCount = $value;
  162. }
  163. /**
  164. * Returns the pagination object used by this data provider.
  165. * Note that you should call [[prepare()]] or [[getModels()]] first to get correct values
  166. * of [[Pagination::totalCount]] and [[Pagination::pageCount]].
  167. * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled.
  168. */
  169. public function getPagination()
  170. {
  171. if ($this->_pagination === null) {
  172. $this->setPagination([]);
  173. }
  174. return $this->_pagination;
  175. }
  176. /**
  177. * Sets the pagination for this data provider.
  178. * @param array|Pagination|bool $value the pagination to be used by this data provider.
  179. * This can be one of the following:
  180. *
  181. * - a configuration array for creating the pagination object. The "class" element defaults
  182. * to 'yii\data\Pagination'
  183. * - an instance of [[Pagination]] or its subclass
  184. * - false, if pagination needs to be disabled.
  185. *
  186. * @throws InvalidArgumentException
  187. */
  188. public function setPagination($value)
  189. {
  190. if (is_array($value)) {
  191. $config = ['class' => Pagination::className()];
  192. if ($this->id !== null) {
  193. $config['pageParam'] = $this->id . '-page';
  194. $config['pageSizeParam'] = $this->id . '-per-page';
  195. }
  196. $this->_pagination = Yii::createObject(array_merge($config, $value));
  197. } elseif ($value instanceof Pagination || $value === false) {
  198. $this->_pagination = $value;
  199. } else {
  200. throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.');
  201. }
  202. }
  203. /**
  204. * Returns the sorting object used by this data provider.
  205. * @return Sort|bool the sorting object. If this is false, it means the sorting is disabled.
  206. */
  207. public function getSort()
  208. {
  209. if ($this->_sort === null) {
  210. $this->setSort([]);
  211. }
  212. return $this->_sort;
  213. }
  214. /**
  215. * Sets the sort definition for this data provider.
  216. * @param array|Sort|bool $value the sort definition to be used by this data provider.
  217. * This can be one of the following:
  218. *
  219. * - a configuration array for creating the sort definition object. The "class" element defaults
  220. * to 'yii\data\Sort'
  221. * - an instance of [[Sort]] or its subclass
  222. * - false, if sorting needs to be disabled.
  223. *
  224. * @throws InvalidArgumentException
  225. */
  226. public function setSort($value)
  227. {
  228. if (is_array($value)) {
  229. $config = ['class' => Sort::className()];
  230. if ($this->id !== null) {
  231. $config['sortParam'] = $this->id . '-sort';
  232. }
  233. $this->_sort = Yii::createObject(array_merge($config, $value));
  234. } elseif ($value instanceof Sort || $value === false) {
  235. $this->_sort = $value;
  236. } else {
  237. throw new InvalidArgumentException('Only Sort instance, configuration array or false is allowed.');
  238. }
  239. }
  240. /**
  241. * Refreshes the data provider.
  242. * After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again,
  243. * they will re-execute the query and return the latest data available.
  244. */
  245. public function refresh()
  246. {
  247. $this->_totalCount = null;
  248. $this->_models = null;
  249. $this->_keys = null;
  250. }
  251. }