-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPurgingModelTrait.php
More file actions
193 lines (169 loc) · 4.23 KB
/
PurgingModelTrait.php
File metadata and controls
193 lines (169 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace Esensi\Model\Traits;
use Esensi\Model\Observers\PurgingModelObserver;
use Illuminate\Support\Str;
/**
* Trait that implements the Purging Model Interface.
*
*/
trait PurgingModelTrait
{
/**
* Whether the model is purging or not.
*
* @var bool
*/
protected $purging = true;
/**
* Boot the trait's observers.
*/
public static function bootPurgingModelTrait()
{
static::observe(new PurgingModelObserver());
}
/**
* Get the purgeable attributes.
*
* @return array
*/
public function getPurgeable()
{
return $this->purgeable ?: [];
}
/**
* Set the purgeable attributes.
*
* @param array $attributes to purge
*/
public function setPurgeable(array $attributes)
{
$this->purgeable = $attributes;
}
/**
* Add an attribute to the purgeable array.
*
* @example addPurgeable( string $attribute, ... )
*
* @param string $attribute to purge
*/
public function addPurgeable($attribute)
{
$this->mergePurgeable(func_get_args());
}
/**
* Remove an attribute from the purgeable array.
*
* @example removePurgeable( string $attribute, ... )
*
* @param string $attribute to purge
*/
public function removePurgeable($attribute)
{
$this->purgeable = array_diff($this->purgeable, func_get_args());
}
/**
* Merge an array of attributes with the purgeable array.
*
* @param array $attributes to purge
*/
public function mergePurgeable(array $attributes)
{
$this->purgeable = array_merge($this->purgeable, $attributes);
}
/**
* Returns whether or not the model will purge
* attributes before saving.
*
* @return bool
*/
public function getPurging()
{
return $this->purging;
}
/**
* Set whether or not the model will purge attributes
* before saving.
*
* @param bool
*/
public function setPurging($value)
{
$this->purging = (bool) $value;
}
/**
* Returns whether the attribute is purgeable.
*
* @param string $attribute name
*
* @return bool
*/
public function isPurgeable($attribute)
{
return $this->getPurging()
&& in_array($attribute, $this->getPurgeable());
}
/**
* Unset attributes that should be purged.
*/
public function purgeAttributes()
{
// Get the attribute keys
$keys = array_keys($this->getAttributes());
// Filter out keys that should purged
$attributes = array_filter($keys, function ($key) {
// Remove attributes that should be purged
if (in_array($key, $this->getPurgeable())) {
return false;
}
// Remove attributes ending with _confirmation
if (Str::endsWith($key, '_confirmation')) {
return false;
}
// Remove attributes starting with _ prefix
if (Str::startsWith($key, '_')) {
return false;
}
return true;
});
// Keep only the attributes that were not purged
$this->attributes = array_intersect_key($this->getAttributes(), array_flip($attributes));
}
/**
* Save with purging even if purging is disabled.
*
* @return bool
*/
public function saveWithPurging()
{
// Turn purging on
return $this->setPurgingAndSave(true);
}
/**
* Save without purging even if purging is enabled.
*
* @return bool
*/
public function saveWithoutPurging()
{
// Turn purging off
return $this->setPurgingAndSave(false);
}
/**
* Set purging state and then save and then reset it.
*
* @param bool $purge
*
* @return bool
*/
protected function setPurgingAndSave($purge)
{
// Set purging state
$purging = $this->getPurging();
$this->setPurging($purge);
// Save the model
$result = $this->save();
// Reset purging back to it's previous state
$this->setPurging($purging);
return $result;
}
}