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\Traits\DataProvider; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface; |
18
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\ModelAwareInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* CriteriaTrait |
23
|
|
|
* |
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
25
|
|
|
*/ |
26
|
|
|
trait CriteriaTrait |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CriteriaInterface |
31
|
|
|
*/ |
32
|
|
|
private $criteria; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Returns the criteria. |
36
|
|
|
* @return CriteriaInterface the query criteria |
37
|
|
|
* @since v1.0 |
38
|
|
|
*/ |
39
|
4 |
|
public function getCriteria() |
40
|
|
|
{ |
41
|
|
|
// Initialise empty criteria, so it's always available via this method call. |
42
|
4 |
|
if (empty($this->criteria)) |
43
|
|
|
{ |
44
|
4 |
|
$className = static::CriteriaClass; |
45
|
4 |
|
if ($this instanceof ModelAwareInterface) |
46
|
|
|
{ |
47
|
4 |
|
$model = $this->getModel(); |
48
|
|
|
} |
49
|
|
|
else |
50
|
|
|
{ |
51
|
|
|
$model = null; |
52
|
|
|
} |
53
|
4 |
|
$this->criteria = new $className(null, $model); |
54
|
|
|
} |
55
|
4 |
|
return $this->criteria; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Sets the query criteria. |
60
|
|
|
* @param CriteriaInterface|array $criteria the query criteria. Array representing the MongoDB query criteria. |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
|
|
public function setCriteria($criteria) |
64
|
|
|
{ |
65
|
|
|
if (is_array($criteria)) |
66
|
|
|
{ |
67
|
|
|
$className = static::CriteriaClass; |
68
|
|
|
$this->criteria = new $className($criteria); |
69
|
|
|
} |
70
|
|
|
elseif ($criteria instanceof CriteriaInterface) |
71
|
|
|
{ |
72
|
|
|
$this->criteria = $criteria; |
73
|
|
|
} |
74
|
|
|
if ($this->criteria instanceof DecoratableInterface) |
75
|
|
|
{ |
76
|
|
|
$this->criteria->decorateWith($this->getModel()); |
77
|
|
|
} |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return AnnotatedInterface |
83
|
|
|
*/ |
84
|
|
|
abstract public function getModel(); |
85
|
|
|
} |
86
|
|
|
|