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\Inverse; |
15
|
|
|
|
16
|
|
|
use Illuminate\Database\Eloquent\Builder; |
17
|
|
|
use Illuminate\Database\Eloquent\Model; |
18
|
|
|
use Illuminate\Database\Eloquent\Scope; |
19
|
|
|
|
20
|
|
|
final class DraftedFlagScope implements Scope |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* All of the extensions to be added to the builder. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $extensions = [ |
28
|
|
|
'UndoDraft', |
29
|
|
|
'Draft', |
30
|
|
|
'WithDrafted', |
31
|
|
|
'WithoutDrafted', |
32
|
|
|
'OnlyDrafted', |
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
|
|
|
if (method_exists($model, 'shouldApplyDraftedFlagScope') && !$model->shouldApplyDraftedFlagScope()) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$builder->where('is_drafted', 0); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Extend the query builder with the needed functions. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function extend(Builder $builder): void |
58
|
|
|
{ |
59
|
|
|
foreach ($this->extensions as $extension) { |
60
|
|
|
$this->{"add{$extension}"}($builder); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Add the `undoDraft` extension to the builder. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
protected function addUndoDraft(Builder $builder): void |
71
|
|
|
{ |
72
|
|
|
$builder->macro('undoDraft', function (Builder $builder) { |
73
|
|
|
$builder->withDrafted(); |
74
|
|
|
|
75
|
|
|
return $builder->update(['is_drafted' => 0]); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add the `draft` extension to the builder. |
81
|
|
|
* |
82
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function addDraft(Builder $builder): void |
86
|
|
|
{ |
87
|
|
|
$builder->macro('draft', function (Builder $builder) { |
88
|
|
|
return $builder->update(['is_drafted' => 1]); |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add the `withDrafted` extension to the builder. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected function addWithDrafted(Builder $builder): void |
99
|
|
|
{ |
100
|
|
|
$builder->macro('withDrafted', function (Builder $builder) { |
101
|
|
|
return $builder->withoutGlobalScope($this); |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Add the `withoutDrafted` extension to the builder. |
107
|
|
|
* |
108
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
protected function addWithoutDrafted(Builder $builder): void |
112
|
|
|
{ |
113
|
|
|
$builder->macro('withoutDrafted', function (Builder $builder) { |
114
|
|
|
return $builder->withoutGlobalScope($this)->where('is_drafted', 0); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add the `onlyDrafted` extension to the builder. |
120
|
|
|
* |
121
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
protected function addOnlyDrafted(Builder $builder): void |
125
|
|
|
{ |
126
|
|
|
$builder->macro('onlyDrafted', function (Builder $builder) { |
127
|
|
|
return $builder->withoutGlobalScope($this)->where('is_drafted', 1); |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|