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