1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Noitran\Repositories\Repositories\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Noitran\Repositories\Contracts\Criteria\CriteriaInterface; |
7
|
|
|
use Noitran\Repositories\Exceptions\RepositoryException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait HasCriteria |
11
|
|
|
*/ |
12
|
|
|
trait HasCriteria |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Collection of Criteria |
16
|
|
|
* |
17
|
|
|
* @var Collection |
18
|
|
|
*/ |
19
|
|
|
protected $criteria; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $disableCriteria = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get Collection of Criteria |
28
|
|
|
* |
29
|
|
|
* @return Collection|null |
30
|
|
|
*/ |
31
|
8 |
|
public function getCriteria(): ?Collection |
32
|
|
|
{ |
33
|
8 |
|
return $this->criteria; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Disable/Enable all Criteria |
38
|
|
|
* |
39
|
|
|
* @param bool $disable |
40
|
|
|
* |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function disableCriteria($disable = true): self |
44
|
|
|
{ |
45
|
|
|
$this->disableCriteria = $disable; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Push Criteria into Collection |
52
|
|
|
* |
53
|
|
|
* @param $criteria |
54
|
|
|
* |
55
|
|
|
* @throws RepositoryException |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function pushCriteria($criteria): self |
60
|
|
|
{ |
61
|
|
|
if (is_string($criteria)) { |
62
|
|
|
$criteria = new $criteria(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!$criteria instanceof CriteriaInterface) { |
66
|
|
|
throw new RepositoryException( |
67
|
|
|
'Class ' . get_class($criteria) . ' must be an instance of ' . CriteriaInterface::class |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->criteria->push($criteria); |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Remove Criteria from collection |
78
|
|
|
* |
79
|
|
|
* @param $criteria |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function popCriteria($criteria) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
// @todo implement |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Apply criteria in current Query |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
8 |
|
protected function applyCriteria(): self |
94
|
|
|
{ |
95
|
8 |
|
if ($this->disableCriteria === true) { |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
8 |
|
$criteria = $this->getCriteria(); |
100
|
|
|
|
101
|
8 |
|
if (! $criteria) { |
|
|
|
|
102
|
8 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($criteria as $value) { |
106
|
|
|
if ($value instanceof CriteriaInterface) { |
107
|
|
|
$this->model = $value->apply($this->model, $this); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Clear all Criteria |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function clearCriteria(): self |
120
|
|
|
{ |
121
|
|
|
$this->criteria = new Collection(); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.