Completed
Push — master ( 4e9dd5...abf073 )
by Peter
05:53
created

AbstractScopeManager::getModelCriteria()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

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