ArrayDataProvider.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\helpers\ArrayHelper;
  9. /**
  10. * ArrayDataProvider implements a data provider based on a data array.
  11. *
  12. * The [[allModels]] property contains all data models that may be sorted and/or paginated.
  13. * ArrayDataProvider will provide the data after sorting and/or pagination.
  14. * You may configure the [[sort]] and [[pagination]] properties to
  15. * customize the sorting and pagination behaviors.
  16. *
  17. * Elements in the [[allModels]] array may be either objects (e.g. model objects)
  18. * or associative arrays (e.g. query results of DAO).
  19. * Make sure to set the [[key]] property to the name of the field that uniquely
  20. * identifies a data record or false if you do not have such a field.
  21. *
  22. * Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient
  23. * because it needs to have [[allModels]] ready.
  24. *
  25. * ArrayDataProvider may be used in the following way:
  26. *
  27. * ```php
  28. * $query = new Query;
  29. * $provider = new ArrayDataProvider([
  30. * 'allModels' => $query->from('post')->all(),
  31. * 'sort' => [
  32. * 'attributes' => ['id', 'username', 'email'],
  33. * ],
  34. * 'pagination' => [
  35. * 'pageSize' => 10,
  36. * ],
  37. * ]);
  38. * // get the posts in the current page
  39. * $posts = $provider->getModels();
  40. * ```
  41. *
  42. * Note: if you want to use the sorting feature, you must configure the [[sort]] property
  43. * so that the provider knows which columns can be sorted.
  44. *
  45. * For more details and usage information on ArrayDataProvider, see the [guide article on data providers](guide:output-data-providers).
  46. *
  47. * @author Qiang Xue <qiang.xue@gmail.com>
  48. * @since 2.0
  49. */
  50. class ArrayDataProvider extends BaseDataProvider
  51. {
  52. /**
  53. * @var string|callable the column that is used as the key of the data models.
  54. * This can be either a column name, or a callable that returns the key value of a given data model.
  55. * If this is not set, the index of the [[models]] array will be used.
  56. * @see getKeys()
  57. */
  58. public $key;
  59. /**
  60. * @var array the data that is not paginated or sorted. When pagination is enabled,
  61. * this property usually contains more elements than [[models]].
  62. * The array elements must use zero-based integer keys.
  63. */
  64. public $allModels;
  65. /**
  66. * @var string the name of the [[\yii\base\Model|Model]] class that will be represented.
  67. * This property is used to get columns' names.
  68. * @since 2.0.9
  69. */
  70. public $modelClass;
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function prepareModels()
  75. {
  76. if (($models = $this->allModels) === null) {
  77. return [];
  78. }
  79. if (($sort = $this->getSort()) !== false) {
  80. $models = $this->sortModels($models, $sort);
  81. }
  82. if (($pagination = $this->getPagination()) !== false) {
  83. $pagination->totalCount = $this->getTotalCount();
  84. if ($pagination->getPageSize() > 0) {
  85. $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
  86. }
  87. }
  88. return $models;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. protected function prepareKeys($models)
  94. {
  95. if ($this->key !== null) {
  96. $keys = [];
  97. foreach ($models as $model) {
  98. if (is_string($this->key)) {
  99. $keys[] = $model[$this->key];
  100. } else {
  101. $keys[] = call_user_func($this->key, $model);
  102. }
  103. }
  104. return $keys;
  105. }
  106. return array_keys($models);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function prepareTotalCount()
  112. {
  113. return is_array($this->allModels) ? count($this->allModels) : 0;
  114. }
  115. /**
  116. * Sorts the data models according to the given sort definition.
  117. * @param array $models the models to be sorted
  118. * @param Sort $sort the sort definition
  119. * @return array the sorted data models
  120. */
  121. protected function sortModels($models, $sort)
  122. {
  123. $orders = $sort->getOrders();
  124. if (!empty($orders)) {
  125. ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
  126. }
  127. return $models;
  128. }
  129. }