1 | <?php |
||
44 | class DataProvider implements DataProviderInterface |
||
45 | { |
||
46 | |||
47 | /** |
||
48 | * Instance of model |
||
49 | * @var Document |
||
50 | * @since v1.0 |
||
51 | */ |
||
52 | public $model; |
||
53 | |||
54 | /** |
||
55 | * Finder instance |
||
56 | * @var FinderInterface |
||
57 | */ |
||
58 | private $finder = null; |
||
59 | |||
60 | /** |
||
61 | * @var CriteriaInterface |
||
62 | */ |
||
63 | private $criteria; |
||
64 | |||
65 | /** |
||
66 | * @var SortInterface |
||
67 | */ |
||
68 | private $sort; |
||
69 | private $data = null; |
||
70 | private $totalItemCount = null; |
||
71 | |||
72 | /** |
||
73 | * Pagination instance |
||
74 | * @var PaginationInterface |
||
75 | */ |
||
76 | private $pagination = null; |
||
77 | |||
78 | /** |
||
79 | * Constructor. |
||
80 | * @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance |
||
81 | * (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>). |
||
82 | * @param array $config configuration (name=>value) to be applied as the initial property values of this class. |
||
83 | * @since v1.0 |
||
84 | */ |
||
85 | 1 | public function __construct($modelClass, $config = []) |
|
86 | { |
||
87 | 1 | if (is_string($modelClass)) |
|
88 | { |
||
89 | $this->model = new $modelClass; |
||
90 | } |
||
91 | 1 | elseif (is_object($modelClass)) |
|
92 | { |
||
93 | 1 | $this->model = $modelClass; |
|
94 | } |
||
95 | else |
||
96 | { |
||
97 | throw new ManganException('Invalid model type for ' . __CLASS__); |
||
98 | } |
||
99 | |||
100 | 1 | $this->finder = Finder::create($this->model); |
|
101 | 1 | if ($this->model instanceof WithCriteriaInterface) |
|
102 | { |
||
103 | $this->criteria = $this->model->getDbCriteria(); |
||
104 | } |
||
105 | else |
||
106 | { |
||
107 | 1 | $this->criteria = new Criteria(); |
|
108 | } |
||
109 | |||
110 | // Merge criteria from configuration |
||
111 | 1 | if (isset($config['criteria'])) |
|
112 | { |
||
113 | $this->criteria->mergeWith($config['criteria']); |
||
114 | unset($config['criteria']); |
||
115 | } |
||
116 | |||
117 | // Merge limit from configuration |
||
118 | 1 | if (isset($config['limit']) && $config['limit'] > 0) |
|
119 | { |
||
120 | $this->criteria->setLimit($config['limit']); |
||
121 | unset($config['limit']); |
||
122 | } |
||
123 | |||
124 | // Merge sorting from configuration |
||
125 | 1 | if (isset($config['sort'])) |
|
126 | { |
||
127 | // Apply default sorting if criteria does not have sort configured |
||
128 | if (isset($config['sort']['defaultOrder']) && empty($this->criteria->getSort())) |
||
129 | { |
||
130 | $this->criteria->setSort($config['sort']['defaultOrder']); |
||
131 | } |
||
132 | unset($config['sort']); |
||
133 | } |
||
134 | |||
135 | 1 | if (!$this->criteria->getSelect()) |
|
136 | { |
||
137 | 1 | $fields = array_keys(ManganMeta::create($this->model)->fields()); |
|
138 | 1 | $fields = array_fill_keys($fields, true); |
|
139 | 1 | $this->criteria->setSelect($fields); |
|
140 | } |
||
141 | |||
142 | 1 | foreach ($config as $key => $value) |
|
143 | { |
||
144 | $this->$key = $value; |
||
145 | } |
||
146 | 1 | } |
|
147 | |||
148 | /** |
||
149 | * Get model used by this dataprovider |
||
150 | * @return AnnotatedInterface |
||
151 | */ |
||
152 | public function getModel() |
||
156 | |||
157 | /** |
||
158 | * Returns the criteria. |
||
159 | * @return Criteria the query criteria |
||
160 | * @since v1.0 |
||
161 | */ |
||
162 | public function getCriteria() |
||
171 | |||
172 | /** |
||
173 | * Sets the query criteria. |
||
174 | * @param CriteriaInterface|array $criteria the query criteria. Array representing the MongoDB query criteria. |
||
175 | * @since v1.0 |
||
176 | */ |
||
177 | public function setCriteria($criteria) |
||
192 | |||
193 | /** |
||
194 | * Returns the sort object. |
||
195 | * @return Sort the sorting object. If this is false, it means the sorting is disabled. |
||
196 | */ |
||
197 | 1 | public function getSort() |
|
206 | |||
207 | /** |
||
208 | * Set sort |
||
209 | * @param SortInterface $sort |
||
210 | * @return DataProvider |
||
211 | */ |
||
212 | public function setSort(SortInterface $sort) |
||
218 | |||
219 | /** |
||
220 | * Returns the pagination object. |
||
221 | * @param string $className the pagination object class name, use this param to override default pagination class. |
||
222 | * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled. |
||
223 | */ |
||
224 | 1 | public function getPagination($className = Pagination::class) |
|
236 | |||
237 | /** |
||
238 | * Returns the number of data items in the current page. |
||
239 | * This is equivalent to <code>count($provider->getData())</code>. |
||
240 | * When {@link pagination} is set false, this returns the same value as {@link totalItemCount}. |
||
241 | * @param boolean $refresh whether the number of data items should be re-calculated. |
||
242 | * @return integer the number of data items in the current page. |
||
243 | */ |
||
244 | public function getItemCount($refresh = false) |
||
248 | |||
249 | /** |
||
250 | * Returns the total number of data items. |
||
251 | * When {@link pagination} is set false, this returns the same value as {@link itemCount}. |
||
252 | * @return integer total number of possible data items. |
||
253 | */ |
||
254 | 1 | public function getTotalItemCount() |
|
262 | |||
263 | /** |
||
264 | * Fetches the data from the persistent data storage. |
||
265 | * @return Document[]|Cursor list of data items |
||
266 | * @since v1.0 |
||
267 | */ |
||
268 | 1 | protected function fetchData() |
|
285 | |||
286 | /** |
||
287 | * Returns the data items currently available, ensures that result is at leas empty array |
||
288 | * @param boolean $refresh whether the data should be re-fetched from persistent storage. |
||
289 | * @return array the list of data items currently available in this data provider. |
||
290 | */ |
||
291 | 1 | public function getData($refresh = false) |
|
303 | |||
304 | } |
||
305 |