BaseDataProvider.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /**
  46. * @var Pagination|false
  47. */
  48. private $_pagination;
  49. private $_keys;
  50. private $_models;
  51. private $_totalCount;
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function init()
  56. {
  57. parent::init();
  58. if ($this->id === null) {
  59. if (self::$counter > 0) {
  60. $this->id = 'dp-' . self::$counter;
  61. }
  62. self::$counter++;
  63. }
  64. }
  65. /**
  66. * Prepares the data models that will be made available in the current page.
  67. * @return array the available data models
  68. */
  69. abstract protected function prepareModels();
  70. /**
  71. * Prepares the keys associated with the currently available data models.
  72. * @param array $models the available data models
  73. * @return array the keys
  74. */
  75. abstract protected function prepareKeys($models);
  76. /**
  77. * Returns a value indicating the total number of data models in this data provider.
  78. * @return int total number of data models in this data provider.
  79. */
  80. abstract protected function prepareTotalCount();
  81. /**
  82. * Prepares the data models and keys.
  83. *
  84. * This method will prepare the data models and keys that can be retrieved via
  85. * [[getModels()]] and [[getKeys()]].
  86. *
  87. * This method will be implicitly called by [[getModels()]] and [[getKeys()]] if it has not been called before.
  88. *
  89. * @param bool $forcePrepare whether to force data preparation even if it has been done before.
  90. */
  91. public function prepare($forcePrepare = false)
  92. {
  93. if ($forcePrepare || $this->_models === null) {
  94. $this->_models = $this->prepareModels();
  95. }
  96. if ($forcePrepare || $this->_keys === null) {
  97. $this->_keys = $this->prepareKeys($this->_models);
  98. }
  99. }
  100. /**
  101. * Returns the data models in the current page.
  102. * @return array the list of data models in the current page.
  103. */
  104. public function getModels()
  105. {
  106. $this->prepare();
  107. return $this->_models;
  108. }
  109. /**
  110. * Sets the data models in the current page.
  111. * @param array $models the models in the current page
  112. */
  113. public function setModels($models)
  114. {
  115. $this->_models = $models;
  116. }
  117. /**
  118. * Returns the key values associated with the data models.
  119. * @return array the list of key values corresponding to [[models]]. Each data model in [[models]]
  120. * is uniquely identified by the corresponding key value in this array.
  121. */
  122. public function getKeys()
  123. {
  124. $this->prepare();
  125. return $this->_keys;
  126. }
  127. /**
  128. * Sets the key values associated with the data models.
  129. * @param array $keys the list of key values corresponding to [[models]].
  130. */
  131. public function setKeys($keys)
  132. {
  133. $this->_keys = $keys;
  134. }
  135. /**
  136. * Returns the number of data models in the current page.
  137. * @return int the number of data models in the current page.
  138. */
  139. public function getCount()
  140. {
  141. return count($this->getModels());
  142. }
  143. /**
  144. * Returns the total number of data models.
  145. * When [[pagination]] is false, this returns the same value as [[count]].
  146. * Otherwise, it will call [[prepareTotalCount()]] to get the count.
  147. * @return int total number of possible data models.
  148. */
  149. public function getTotalCount()
  150. {
  151. if ($this->getPagination() === false) {
  152. return $this->getCount();
  153. } elseif ($this->_totalCount === null) {
  154. $this->_totalCount = $this->prepareTotalCount();
  155. }
  156. return $this->_totalCount;
  157. }
  158. /**
  159. * Sets the total number of data models.
  160. * @param int $value the total number of data models.
  161. */
  162. public function setTotalCount($value)
  163. {
  164. $this->_totalCount = $value;
  165. }
  166. /**
  167. * Returns the pagination object used by this data provider.
  168. * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled.
  169. */
  170. public function getPagination()
  171. {
  172. if ($this->_pagination === null) {
  173. $this->setPagination([]);
  174. }
  175. if (($this->_pagination !== false) && ($this->_pagination->totalCount === null)) {
  176. if ($this->_totalCount === null) {
  177. $this->setTotalCount($this->prepareTotalCount());
  178. }
  179. $this->_pagination->totalCount = $this->_totalCount;
  180. }
  181. return $this->_pagination;
  182. }
  183. /**
  184. * Sets the pagination for this data provider.
  185. * @param array|Pagination|bool $value the pagination to be used by this data provider.
  186. * This can be one of the following:
  187. *
  188. * - a configuration array for creating the pagination object. The "class" element defaults
  189. * to 'yii\data\Pagination'
  190. * - an instance of [[Pagination]] or its subclass
  191. * - false, if pagination needs to be disabled.
  192. *
  193. * @throws InvalidArgumentException
  194. */
  195. public function setPagination($value)
  196. {
  197. if (is_array($value)) {
  198. $config = ['class' => Pagination::className()];
  199. if ($this->id !== null) {
  200. $config['pageParam'] = $this->id . '-page';
  201. $config['pageSizeParam'] = $this->id . '-per-page';
  202. }
  203. $this->_pagination = Yii::createObject(array_merge($config, $value));
  204. } elseif ($value instanceof Pagination || $value === false) {
  205. $this->_pagination = $value;
  206. } else {
  207. throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.');
  208. }
  209. }
  210. /**
  211. * Returns the sorting object used by this data provider.
  212. * @return Sort|bool the sorting object. If this is false, it means the sorting is disabled.
  213. */
  214. public function getSort()
  215. {
  216. if ($this->_sort === null) {
  217. $this->setSort([]);
  218. }
  219. return $this->_sort;
  220. }
  221. /**
  222. * Sets the sort definition for this data provider.
  223. * @param array|Sort|bool $value the sort definition to be used by this data provider.
  224. * This can be one of the following:
  225. *
  226. * - a configuration array for creating the sort definition object. The "class" element defaults
  227. * to 'yii\data\Sort'
  228. * - an instance of [[Sort]] or its subclass
  229. * - false, if sorting needs to be disabled.
  230. *
  231. * @throws InvalidArgumentException
  232. */
  233. public function setSort($value)
  234. {
  235. if (is_array($value)) {
  236. $config = ['class' => Sort::className()];
  237. if ($this->id !== null) {
  238. $config['sortParam'] = $this->id . '-sort';
  239. }
  240. $this->_sort = Yii::createObject(array_merge($config, $value));
  241. } elseif ($value instanceof Sort || $value === false) {
  242. $this->_sort = $value;
  243. } else {
  244. throw new InvalidArgumentException('Only Sort instance, configuration array or false is allowed.');
  245. }
  246. }
  247. /**
  248. * Refreshes the data provider.
  249. * After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again,
  250. * they will re-execute the query and return the latest data available.
  251. */
  252. public function refresh()
  253. {
  254. $this->_totalCount = null;
  255. $this->_models = null;
  256. $this->_keys = null;
  257. }
  258. }