1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Eloquent Flag. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Flag\Scopes\Classic; |
15
|
|
|
|
16
|
|
|
use Illuminate\Database\Eloquent\Builder; |
17
|
|
|
use Illuminate\Database\Eloquent\Model; |
18
|
|
|
use Illuminate\Database\Eloquent\Scope; |
19
|
|
|
|
20
|
|
|
final class ApprovedFlagScope implements Scope |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* All of the extensions to be added to the builder. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $extensions = [ |
28
|
|
|
'Approve', |
29
|
|
|
'UndoApprove', |
30
|
|
|
'WithNotApproved', |
31
|
|
|
'WithoutNotApproved', |
32
|
|
|
'OnlyNotApproved', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Apply the scope to a given Eloquent query builder. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
39
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function apply(Builder $builder, Model $model): void |
43
|
|
|
{ |
44
|
|
|
$builder->where('is_approved', 1); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Extend the query builder with the needed functions. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
public function extend(Builder $builder): void |
54
|
|
|
{ |
55
|
|
|
foreach ($this->extensions as $extension) { |
56
|
|
|
$this->{"add{$extension}"}($builder); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add the `approve` extension to the builder. |
62
|
|
|
* |
63
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
protected function addApprove(Builder $builder): void |
67
|
|
|
{ |
68
|
|
|
$builder->macro('approve', function (Builder $builder) { |
69
|
|
|
$builder->withNotApproved(); |
70
|
|
|
|
71
|
|
|
return $builder->update(['is_approved' => 1]); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add the `undoApprove` extension to the builder. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
protected function addUndoApprove(Builder $builder): void |
82
|
|
|
{ |
83
|
|
|
$builder->macro('undoApprove', function (Builder $builder) { |
84
|
|
|
return $builder->update(['is_approved' => 0]); |
85
|
|
|
}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add the `withNotApproved` extension to the builder. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
protected function addWithNotApproved(Builder $builder): void |
95
|
|
|
{ |
96
|
|
|
$builder->macro('withNotApproved', function (Builder $builder) { |
97
|
|
|
return $builder->withoutGlobalScope($this); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add the `withoutNotApproved` extension to the builder. |
103
|
|
|
* |
104
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function addWithoutNotApproved(Builder $builder): void |
108
|
|
|
{ |
109
|
|
|
$builder->macro('withoutNotApproved', function (Builder $builder) { |
110
|
|
|
return $builder->withoutGlobalScope($this)->where('is_approved', 1); |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Add the `onlyNotApproved` extension to the builder. |
116
|
|
|
* |
117
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function addOnlyNotApproved(Builder $builder): void |
121
|
|
|
{ |
122
|
|
|
$builder->macro('onlyNotApproved', function (Builder $builder) { |
123
|
|
|
return $builder->withoutGlobalScope($this)->where('is_approved', 0); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|