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