Completed
Push — master ( 4b57bc...bee414 )
by Peter
06:36
created

ScopeManager::resetScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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