|
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 ArchivedFlagScope implements Scope |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* All of the extensions to be added to the builder. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $extensions = [ |
|
28
|
|
|
'UndoArchive', |
|
29
|
|
|
'Archive', |
|
30
|
|
|
'WithArchived', |
|
31
|
|
|
'WithoutArchived', |
|
32
|
|
|
'OnlyArchived', |
|
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, 'shouldApplyArchivedFlagScope') && $model->shouldApplyArchivedFlagScope()) { |
|
45
|
|
|
$builder->where('is_archived', 0); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Extend the query builder with the needed functions. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function extend(Builder $builder): void |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($this->extensions as $extension) { |
|
58
|
|
|
$this->{"add{$extension}"}($builder); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Add the `undoArchive` extension to the builder. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function addUndoArchive(Builder $builder): void |
|
69
|
|
|
{ |
|
70
|
|
|
$builder->macro('undoArchive', function (Builder $builder) { |
|
71
|
|
|
$builder->withArchived(); |
|
72
|
|
|
|
|
73
|
|
|
return $builder->update(['is_archived' => 0]); |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Add the `archive` extension to the builder. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function addArchive(Builder $builder): void |
|
84
|
|
|
{ |
|
85
|
|
|
$builder->macro('archive', function (Builder $builder) { |
|
86
|
|
|
return $builder->update(['is_archived' => 1]); |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Add the `withArchived` extension to the builder. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function addWithArchived(Builder $builder): void |
|
97
|
|
|
{ |
|
98
|
|
|
$builder->macro('withArchived', function (Builder $builder) { |
|
99
|
|
|
return $builder->withoutGlobalScope($this); |
|
100
|
|
|
}); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add the `withoutArchived` extension to the builder. |
|
105
|
|
|
* |
|
106
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function addWithoutArchived(Builder $builder): void |
|
110
|
|
|
{ |
|
111
|
|
|
$builder->macro('withoutArchived', function (Builder $builder) { |
|
112
|
|
|
return $builder->withoutGlobalScope($this)->where('is_archived', 0); |
|
113
|
|
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Add the `onlyArchived` extension to the builder. |
|
118
|
|
|
* |
|
119
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function addOnlyArchived(Builder $builder): void |
|
123
|
|
|
{ |
|
124
|
|
|
$builder->macro('onlyArchived', function (Builder $builder) { |
|
125
|
|
|
return $builder->withoutGlobalScope($this)->where('is_archived', 1); |
|
126
|
|
|
}); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|