|
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 EndedAtScope implements Scope |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* All of the extensions to be added to the builder. |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $extensions = [ |
|
29
|
|
|
'UndoEnd', |
|
30
|
|
|
'End', |
|
31
|
|
|
'WithEnded', |
|
32
|
|
|
'WithoutEnded', |
|
33
|
|
|
'OnlyEnded', |
|
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, 'shouldApplyEndedAtScope') && !$model->shouldApplyEndedAtScope()) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$builder->whereNull('ended_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 `undoEnd` extension to the builder. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function addUndoEnd(Builder $builder): void |
|
72
|
|
|
{ |
|
73
|
|
|
$builder->macro('undoEnd', function (Builder $builder) { |
|
74
|
|
|
$builder->withEnded(); |
|
75
|
|
|
|
|
76
|
|
|
return $builder->update(['ended_at' => null]); |
|
77
|
|
|
}); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Add the `end` extension to the builder. |
|
82
|
|
|
* |
|
83
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function addEnd(Builder $builder): void |
|
87
|
|
|
{ |
|
88
|
|
|
$builder->macro('end', function (Builder $builder) { |
|
89
|
|
|
return $builder->update(['ended_at' => Date::now()]); |
|
90
|
|
|
}); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Add the `withEnded` extension to the builder. |
|
95
|
|
|
* |
|
96
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function addWithEnded(Builder $builder): void |
|
100
|
|
|
{ |
|
101
|
|
|
$builder->macro('withEnded', function (Builder $builder) { |
|
102
|
|
|
return $builder->withoutGlobalScope($this); |
|
103
|
|
|
}); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add the `withoutEnded` extension to the builder. |
|
108
|
|
|
* |
|
109
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
110
|
|
|
* @return void |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function addWithoutEnded(Builder $builder): void |
|
113
|
|
|
{ |
|
114
|
|
|
$builder->macro('withoutEnded', function (Builder $builder) { |
|
115
|
|
|
return $builder->withoutGlobalScope($this)->whereNull('ended_at'); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add the `onlyEnded` extension to the builder. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function addOnlyEnded(Builder $builder): void |
|
126
|
|
|
{ |
|
127
|
|
|
$builder->macro('onlyEnded', function (Builder $builder) { |
|
128
|
|
|
return $builder->withoutGlobalScope($this)->whereNotNull('ended_at'); |
|
129
|
|
|
}); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|