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
|
10 |
|
public function getCriteria(): ?Collection |
32
|
|
|
{ |
33
|
10 |
|
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
|
2 |
|
public function pushCriteria($criteria): self |
60
|
|
|
{ |
61
|
2 |
|
if (is_string($criteria)) { |
62
|
|
|
$criteria = new $criteria(); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
if (!$criteria instanceof CriteriaInterface) { |
66
|
|
|
throw new RepositoryException( |
67
|
|
|
'Class ' . get_class($criteria) . ' must be an instance of ' . CriteriaInterface::class |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$this->criteria->push($criteria); |
72
|
|
|
|
73
|
2 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Remove Criteria from collection |
78
|
|
|
* |
79
|
|
|
* @param $criteria |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function popCriteria($criteria): self |
84
|
|
|
{ |
85
|
|
|
$this->criteria = $this->criteria->reject(function ($value) use ($criteria) { |
86
|
|
|
if (is_object($value) && is_string($criteria)) { |
87
|
|
|
return get_class($value) === $criteria; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (is_string($value) && is_object($criteria)) { |
91
|
|
|
return $value === get_class($criteria); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return get_class($value) === get_class($criteria); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Apply criteria in current Query |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
10 |
|
protected function applyCriteria(): self |
106
|
|
|
{ |
107
|
10 |
|
if ($this->disableCriteria === true) { |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
10 |
|
$criteria = $this->getCriteria(); |
112
|
|
|
|
113
|
10 |
|
if (! $criteria) { |
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
10 |
|
foreach ($criteria as $value) { |
118
|
2 |
|
if ($value instanceof CriteriaInterface) { |
119
|
2 |
|
$this->model = $value->apply($this->model, $this); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
10 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Clear all Criteria |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function clearCriteria(): self |
132
|
|
|
{ |
133
|
|
|
$this->criteria = new Collection(); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|