1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link http://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Mangan\Exceptions\ManganException; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\LimitableInterface; |
18
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\MergeableInterface; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface; |
20
|
|
|
use Maslosoft\Mangan\Interfaces\DataProviderInterface; |
21
|
|
|
use Maslosoft\Mangan\Interfaces\FinderInterface; |
22
|
|
|
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface; |
23
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait; |
24
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait; |
25
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\DataTrait; |
26
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait; |
27
|
|
|
use Maslosoft\Mangan\Traits\ModelAwareTrait; |
28
|
|
|
use Maslosoft\Mangan\Traits\SortAwareTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Mongo document data provider |
32
|
|
|
* |
33
|
|
|
* Implements a data provider based on Document. |
34
|
|
|
* |
35
|
|
|
* DataProvider provides data in terms of Document objects which are |
36
|
|
|
* of class {@link modelClass}. It uses the AR {@link EntityManager::findAll} method |
37
|
|
|
* to retrieve the data from database. The {@link query} property can be used to |
38
|
|
|
* specify various query options, such as conditions, sorting, pagination, etc. |
39
|
|
|
* |
40
|
|
|
* @author Ianaré Sévi |
41
|
|
|
* @author Dariusz Górecki <[email protected]> |
42
|
|
|
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org |
43
|
|
|
* @copyright 2011 CleverIT http://www.cleverit.com.pl |
44
|
|
|
* @since v1.0 |
45
|
|
|
*/ |
46
|
|
|
class DataProvider implements DataProviderInterface |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
use ConfigureTrait, |
50
|
|
|
CriteriaTrait, |
51
|
|
|
DataTrait, |
52
|
|
|
ModelAwareTrait, |
53
|
|
|
PaginationTrait, |
54
|
|
|
SortAwareTrait; |
55
|
|
|
|
56
|
|
|
const CriteriaClass = Criteria::class; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Finder instance |
60
|
|
|
* @var FinderInterface |
61
|
|
|
*/ |
62
|
|
|
private $finder = null; |
63
|
|
|
private $totalItemCount = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Constructor. |
67
|
|
|
* @param mixed $modelClass the model class (e.g. 'Post') or the model finder instance |
68
|
|
|
* (e.g. <code>Post::model()</code>, <code>Post::model()->published()</code>). |
69
|
|
|
* @param array $config configuration (name=>value) to be applied as the initial property values of this class. |
70
|
|
|
* @since v1.0 |
71
|
|
|
*/ |
72
|
1 |
|
public function __construct($modelClass, $config = []) |
73
|
|
|
{ |
74
|
1 |
|
$this->configure($modelClass, $config); |
75
|
1 |
|
$this->finder = Finder::create($this->getModel()); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the number of data items in the current page. |
80
|
|
|
* This is equivalent to <code>count($provider->getData())</code>. |
81
|
|
|
* When {@link pagination} is set false, this returns the same value as {@link totalItemCount}. |
82
|
|
|
* @param boolean $refresh whether the number of data items should be re-calculated. |
83
|
|
|
* @return integer the number of data items in the current page. |
84
|
|
|
*/ |
85
|
|
|
public function getItemCount($refresh = false) |
86
|
|
|
{ |
87
|
|
|
return count($this->getData($refresh)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the total number of data items. |
92
|
|
|
* When {@link pagination} is set false, this returns the same value as {@link itemCount}. |
93
|
|
|
* @return integer total number of possible data items. |
94
|
|
|
*/ |
95
|
1 |
|
public function getTotalItemCount() |
96
|
|
|
{ |
97
|
1 |
|
if ($this->totalItemCount === null) |
98
|
1 |
|
{ |
99
|
1 |
|
$this->totalItemCount = $this->finder->count($this->getCriteria()); |
100
|
1 |
|
} |
101
|
1 |
|
return $this->totalItemCount; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Fetches the data from the persistent data storage. |
106
|
|
|
* @return Document[]|Cursor list of data items |
107
|
|
|
* @since v1.0 |
108
|
|
|
*/ |
109
|
1 |
|
protected function fetchData() |
110
|
|
|
{ |
111
|
|
|
// Setup required objects |
112
|
1 |
|
$sort = $this->getSort(); |
113
|
1 |
|
$criteria = $this->getCriteria(); |
114
|
1 |
|
$pagination = $this->getPagination(); |
115
|
|
|
|
116
|
|
|
// Apply limits if required |
117
|
1 |
|
if ($pagination !== false && $criteria instanceof LimitableInterface) |
118
|
1 |
|
{ |
119
|
1 |
|
$pagination->setCount($this->getTotalItemCount()); |
120
|
1 |
|
$pagination->apply($criteria); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
// Apply sort if required |
124
|
1 |
|
if ($sort->isSorted()) |
125
|
1 |
|
{ |
126
|
|
|
$criteria->setSort($sort); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Finally apply all to finder |
130
|
1 |
|
return $this->finder->findAll($criteria); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|