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 https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Abstracts; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Mangan\Document; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface; |
18
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
20
|
|
|
use Maslosoft\Mangan\Interfaces\ModelAwareInterface; |
21
|
|
|
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface; |
22
|
|
|
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface; |
23
|
|
|
use Maslosoft\Mangan\Traits\ModelAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Base class for implementing scope managers |
27
|
|
|
* |
28
|
|
|
* @see ScopeManagerInterface |
29
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractScopeManager implements ModelAwareInterface |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
use ModelAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* |
38
|
|
|
* @var CriteriaInterface |
39
|
|
|
*/ |
40
|
|
|
private $criteria = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the declaration of named scopes. |
44
|
|
|
* A named scope represents a query criteria that can be chained together with |
45
|
|
|
* other named scopes and applied to a query. This method should be overridden |
46
|
|
|
* by child classes to declare named scopes for the particular document classes. |
47
|
|
|
* For example, the following code declares two named scopes: 'recently' and |
48
|
|
|
* 'published'. |
49
|
|
|
* <pre> |
50
|
|
|
* return array( |
51
|
|
|
* 'published'=>array( |
52
|
|
|
* 'conditions'=>array( |
53
|
|
|
* 'status'=>array('==', 1), |
54
|
|
|
* ), |
55
|
|
|
* ), |
56
|
|
|
* 'recently'=>array( |
57
|
|
|
* 'sort'=>array('create_time'=>Criteria::SortDesc), |
58
|
|
|
* 'limit'=>5, |
59
|
|
|
* ), |
60
|
|
|
* ); |
61
|
|
|
* </pre> |
62
|
|
|
* If the above scopes are declared in a 'Post' model, we can perform the following |
63
|
|
|
* queries: |
64
|
|
|
* <pre> |
65
|
|
|
* $posts=Post::model()->published()->findAll(); |
66
|
|
|
* $posts=Post::model()->published()->recently()->findAll(); |
67
|
|
|
* $posts=Post::model()->published()->published()->recently()->find(); |
68
|
|
|
* </pre> |
69
|
|
|
* |
70
|
|
|
* @return array the scope definition. The array keys are scope names; the array |
71
|
|
|
* values are the corresponding scope definitions. Each scope definition is represented |
72
|
|
|
* as an array whose keys must be properties of {@link Criteria}. |
73
|
|
|
* @since v1.0 |
74
|
|
|
*/ |
75
|
|
|
public function scopes() |
76
|
|
|
{ |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the default named scope that should be implicitly applied to all queries for this model. |
82
|
|
|
* Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries. |
83
|
|
|
* The default implementation simply returns an empty array. You may override this method |
84
|
|
|
* if the model needs to be queried with some default criteria (e.g. only active records should be returned). |
85
|
|
|
* @return array the mongo criteria. This will be used as the parameter to the constructor |
86
|
|
|
* of {@link Criteria}. |
87
|
|
|
* @since v1.2.2 |
88
|
|
|
*/ |
89
|
|
|
public function defaultScope() |
90
|
|
|
{ |
91
|
|
|
return []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Resets all scopes and criteria applied including default scope. |
96
|
|
|
* |
97
|
|
|
* @return Document |
98
|
|
|
* @since v1.0 |
99
|
|
|
*/ |
100
|
|
|
public function resetScope() |
101
|
|
|
{ |
102
|
|
|
$this->criteria = $this->getNewCriteria(); |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Apply scopes to criteria, will create criteria object if not provided and pass it by reference |
108
|
|
|
* @param CriteriaInterface|array|null $criteria |
109
|
|
|
* @return CriteriaInterface |
110
|
|
|
*/ |
111
|
97 |
|
public function apply(&$criteria = null) |
112
|
|
|
{ |
113
|
97 |
|
if (null === $criteria) |
114
|
|
|
{ |
115
|
24 |
|
return $this->getModelCriteria(); |
116
|
|
|
} |
117
|
92 |
|
elseif (is_array($criteria)) |
118
|
|
|
{ |
119
|
1 |
|
$criteria = $this->getNewCriteria($criteria); |
120
|
|
|
} |
121
|
92 |
|
$criteria->mergeWith($this->criteria); |
122
|
92 |
|
$criteria->mergeWith($this->getModelCriteria()); |
123
|
92 |
|
if ($criteria instanceof DecoratableInterface) |
124
|
|
|
{ |
125
|
92 |
|
$criteria->decorateWith($this->getModel()); |
126
|
|
|
} |
127
|
92 |
|
return $criteria; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function reset() |
131
|
|
|
{ |
132
|
|
|
$this->criteria = $this->getNewCriteria(); |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
97 |
|
protected function getModelCriteria() |
137
|
|
|
{ |
138
|
97 |
|
$criteria = null; |
139
|
97 |
|
if ($this->model instanceof WithCriteriaInterface) |
140
|
|
|
{ |
141
|
33 |
|
$criteria = $this->model->getDbCriteria(); |
142
|
|
|
} |
143
|
64 |
|
elseif ($this->model instanceof CriteriaAwareInterface) |
144
|
|
|
{ |
145
|
|
|
$criteria = $this->model->getCriteria(); |
146
|
|
|
} |
147
|
97 |
|
if (empty($criteria)) |
148
|
|
|
{ |
149
|
64 |
|
return $this->getNewCriteria(); |
150
|
|
|
} |
151
|
33 |
|
return $criteria; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
abstract public function getNewCriteria($criteria = null); |
155
|
|
|
} |
156
|
|
|
|