1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MarcoTisi\SoftEnable; |
4
|
|
|
|
5
|
|
|
trait SoftEnable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Boot the soft enabling trait for a model. |
9
|
|
|
* |
10
|
|
|
* @return void |
11
|
|
|
*/ |
12
|
7 |
|
public static function bootSoftEnable() |
13
|
|
|
{ |
14
|
7 |
|
static::addGlobalScope(new SoftEnablingScope); |
15
|
7 |
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Enable a soft-enabled model instance. |
19
|
|
|
* |
20
|
|
|
* @return bool|null |
21
|
|
|
*/ |
22
|
3 |
|
public function enable() |
23
|
|
|
{ |
24
|
|
|
// If the enabling event does not return false, we will proceed with this |
25
|
|
|
// enable operation. Otherwise, we bail out so the developer will stop |
26
|
|
|
// the enable totally. We will set enabled to true and save. |
27
|
3 |
|
if ($this->fireModelEvent('enabling') === false) { |
28
|
1 |
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
$this->{$this->getEnabledColumn()} = true; |
32
|
|
|
|
33
|
|
|
// Once we have saved the model, we will fire the "enabled" event so this |
34
|
|
|
// developer will do anything they need to after an enable operation is |
35
|
|
|
// totally finished. Then we will return the result of the save call. |
36
|
2 |
|
$result = $this->save(); |
37
|
|
|
|
38
|
2 |
|
$this->fireModelEvent('enabled'); |
39
|
|
|
|
40
|
2 |
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Determine if the model instance is enabled. |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
2 |
|
public function isEnabled() |
49
|
|
|
{ |
50
|
2 |
|
return $this->{$this->getEnabledColumn()} === true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register a enabling model event with the dispatcher. |
55
|
|
|
* |
56
|
|
|
* @param \Closure|string $callback |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
2 |
|
public static function enabling($callback) |
60
|
|
|
{ |
61
|
2 |
|
static::registerModelEvent('enabling', $callback); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Register a enabled model event with the dispatcher. |
66
|
|
|
* |
67
|
|
|
* @param \Closure|string $callback |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
1 |
|
public static function enabled($callback) |
71
|
|
|
{ |
72
|
1 |
|
static::registerModelEvent('enabled', $callback); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Disable a soft-enabled model instance. |
77
|
|
|
* |
78
|
|
|
* @return bool|null |
79
|
|
|
*/ |
80
|
29 |
|
public function disable() |
81
|
|
|
{ |
82
|
|
|
// If the disabling event does not return false, we will proceed with this |
83
|
|
|
// enable operation. Otherwise, we bail out so the developer will stop |
84
|
|
|
// the enable totally. We will set enabled to false and save. |
85
|
29 |
|
if ($this->fireModelEvent('disabling') === false) { |
86
|
1 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
29 |
|
$this->{$this->getEnabledColumn()} = false; |
90
|
|
|
|
91
|
|
|
// Once we have saved the model, we will fire the "disabled" event so this |
92
|
|
|
// developer will do anything they need to after a disable operation is |
93
|
|
|
// totally finished. Then we will return the result of the save call. |
94
|
29 |
|
$result = $this->save(); |
95
|
|
|
|
96
|
29 |
|
$this->fireModelEvent('disabled'); |
97
|
|
|
|
98
|
29 |
|
return $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Determine if the model instance is disabled. |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
2 |
|
public function isDisabled() |
107
|
|
|
{ |
108
|
2 |
|
return is_null($this->{$this->getEnabledColumn()}) || $this->{$this->getEnabledColumn()} === false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Register a disabling model event with the dispatcher. |
113
|
|
|
* |
114
|
|
|
* @param \Closure|string $callback |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
2 |
|
public static function disabling($callback) |
118
|
|
|
{ |
119
|
2 |
|
static::registerModelEvent('disabling', $callback); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Register a disabled model event with the dispatcher. |
124
|
|
|
* |
125
|
|
|
* @param \Closure|string $callback |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
1 |
|
public static function disabled($callback) |
129
|
|
|
{ |
130
|
1 |
|
static::registerModelEvent('disabled', $callback); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the name of the "enabled" column. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
29 |
|
public function getEnabledColumn() |
139
|
|
|
{ |
140
|
29 |
|
return defined('static::ENABLED') ? static::ENABLED : 'enabled'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the fully qualified "enabled" column. |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
26 |
|
public function getQualifiedEnabledColumn() |
149
|
|
|
{ |
150
|
26 |
|
return $this->getTable().'.'.$this->getEnabledColumn(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the casts array. |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
29 |
|
public function getCasts() |
159
|
|
|
{ |
160
|
29 |
|
return array_merge([ |
161
|
29 |
|
$this->getEnabledColumn() => 'bool', |
162
|
29 |
|
], parent::getCasts()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the observable event names. |
167
|
|
|
* |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
2 |
|
public function getObservableEvents() |
171
|
|
|
{ |
172
|
2 |
|
return array_merge( |
173
|
|
|
[ |
174
|
2 |
|
'enabling', 'enabled', 'disabling', 'disabled', |
175
|
2 |
|
], |
176
|
2 |
|
parent::getObservableEvents() |
177
|
2 |
|
); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|