|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SebastianBerc\Repositories\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container as Application; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use SebastianBerc\Repositories\Contracts\CriteriaInterface; |
|
9
|
|
|
use SebastianBerc\Repositories\Contracts\ServiceInterface; |
|
10
|
|
|
use SebastianBerc\Repositories\Repository; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CriteriaService. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Sebastian Berć <[email protected]> |
|
16
|
|
|
* @copyright Copyright (c) Sebastian Berć |
|
17
|
|
|
*/ |
|
18
|
|
|
class CriteriaService implements ServiceInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Contains application instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @var Application |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $app; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Contains repository instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @var Repository |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $repository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Contains stack of criterias. |
|
36
|
|
|
* |
|
37
|
|
|
* @var Collection |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $stack; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create a new criteria service instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Application $app |
|
45
|
|
|
* @param Repository $repository |
|
46
|
|
|
*/ |
|
47
|
120 |
|
public function __construct(Application $app, Repository $repository) |
|
48
|
|
|
{ |
|
49
|
120 |
|
$this->app = $app; |
|
50
|
120 |
|
$this->repository = $repository; |
|
51
|
120 |
|
$this->stack = new Collection(); |
|
52
|
120 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Add criteria to stack. |
|
56
|
|
|
* |
|
57
|
|
|
* @param CriteriaInterface $criteria |
|
58
|
|
|
* |
|
59
|
|
|
* @return $this |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function addCriteria(CriteriaInterface $criteria) |
|
62
|
|
|
{ |
|
63
|
6 |
|
$this->stack->push($criteria); |
|
64
|
|
|
|
|
65
|
6 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Determinate if service stack has any criteria. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
84 |
|
public function hasCriteria() |
|
74
|
|
|
{ |
|
75
|
84 |
|
return !$this->stack->isEmpty(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns all criteria from stack. |
|
80
|
|
|
* |
|
81
|
|
|
* @return array |
|
82
|
|
|
*/ |
|
83
|
4 |
|
public function getCriterias() |
|
84
|
|
|
{ |
|
85
|
4 |
|
return $this->stack->all(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Remove one or all criteria from stack. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $criteriaName |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public function removeCriteria($criteriaName = '') |
|
96
|
|
|
{ |
|
97
|
2 |
|
if (!empty($criteriaName)) { |
|
98
|
|
|
$this->stack = $this->stack->filter(function (CriteriaInterface $criteria) use ($criteriaName) { |
|
99
|
2 |
|
return !is_a($criteria, $criteriaName); |
|
100
|
2 |
|
}); |
|
101
|
|
|
|
|
102
|
2 |
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
$this->stack = new Collection(); |
|
106
|
|
|
|
|
107
|
2 |
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Execute an criterias from the stack on given query builder. |
|
112
|
|
|
* |
|
113
|
|
|
* @param Builder $query |
|
114
|
|
|
* |
|
115
|
|
|
* @return Builder |
|
116
|
|
|
*/ |
|
117
|
|
|
public function executeOn(Builder $query) |
|
118
|
|
|
{ |
|
119
|
4 |
|
$this->stack->each(function (CriteriaInterface $criteria) use ($query) { |
|
120
|
4 |
|
$criteria->execute($query); |
|
121
|
4 |
|
}); |
|
122
|
|
|
|
|
123
|
4 |
|
return $query; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|