|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Goopil\RestFilter\Scopes; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Scope as ScopeInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class FullScopes. |
|
11
|
|
|
*/ |
|
12
|
|
|
class FullScopes extends BaseScope implements ScopeInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $scopes = [ |
|
18
|
|
|
InNotInScope::class, |
|
19
|
|
|
FilterScope::class, |
|
20
|
|
|
IncludeScope::class, |
|
21
|
|
|
OffsetLimitScope::class, |
|
22
|
|
|
SearchScope::class, |
|
23
|
|
|
OrderScope::class, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* reset and set scopes to be executed. |
|
28
|
|
|
* |
|
29
|
|
|
* @param $scopes string|array |
|
30
|
|
|
* |
|
31
|
|
|
* @return $this |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setScopes($scopes) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->scopes = []; |
|
36
|
|
|
$scopes = is_array($scopes) ? $scopes : func_get_args(); |
|
37
|
|
|
$this->assemble($scopes); |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* add scopes to existing ones. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $scopes string|array |
|
46
|
|
|
* |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
|
|
public function addScopes($scopes) |
|
50
|
|
|
{ |
|
51
|
|
|
$scopes = is_array($scopes) ? $scopes : func_get_args(); |
|
52
|
|
|
$this->assemble($scopes); |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* assemble scopes. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $scopes |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function assemble(array $scopes) |
|
63
|
|
|
{ |
|
64
|
|
|
$errors = []; |
|
65
|
|
|
foreach ($scopes as $scope) { |
|
66
|
|
|
if (is_string($scope)) { |
|
67
|
|
|
$scope = app($scope); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($scope instanceof ScopeInterface) { |
|
71
|
|
|
if (! in_array($scope, $this->scopes)) { |
|
72
|
|
|
$this->scopes[] = $scope; |
|
73
|
|
|
} |
|
74
|
|
|
} else { |
|
75
|
|
|
$errors[] = 'The class '.get_class($scope).'doesn\'t implements '.ScopeInterface::class; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (count($errors) > 0) { |
|
80
|
|
|
throw new \InvalidArgumentException(implode(' \n', $errors)); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Process scopes. |
|
86
|
|
|
* |
|
87
|
|
|
* @param Builder $builder |
|
88
|
|
|
* @param Model $model |
|
89
|
|
|
* |
|
90
|
|
|
* @return Builder |
|
91
|
|
|
*/ |
|
92
|
|
|
public function apply(Builder $builder, Model $model) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->scopes as $scope) { |
|
95
|
|
|
/** @var $current ScopeInterface */ |
|
96
|
|
|
$current = new $scope($this->request, $this->primary, $this->secondary); |
|
97
|
|
|
$current->apply($builder, $model); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $builder; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|