1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client\Concerns; |
6
|
|
|
|
7
|
|
|
trait GuardsAttributes |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The attributes that are mass assignable. |
11
|
|
|
* |
12
|
|
|
* @var string[] |
13
|
|
|
*/ |
14
|
|
|
protected $fillable = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The attributes that aren't mass assignable. |
18
|
|
|
* |
19
|
|
|
* @var string[] |
20
|
|
|
*/ |
21
|
|
|
protected $guarded = ['id']; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Indicates if all mass assignment is enabled. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected static $unguarded = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the fillable attributes for the model. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
692 |
|
public function getFillable() |
36
|
|
|
{ |
37
|
692 |
|
return $this->fillable; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set the fillable attributes for the model. |
42
|
|
|
* |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
8 |
|
public function fillable(array $fillable) |
47
|
|
|
{ |
48
|
8 |
|
$this->fillable = $fillable; |
49
|
|
|
|
50
|
8 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Merge new fillable attributes with existing fillable attributes on the model. |
55
|
|
|
* |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
4 |
|
public function mergeFillable(array $fillable) |
60
|
|
|
{ |
61
|
4 |
|
$this->fillable = array_merge($this->fillable, $fillable); |
62
|
|
|
|
63
|
4 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the guarded attributes for the model. |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
620 |
|
public function getGuarded() |
72
|
|
|
{ |
73
|
620 |
|
return $this->guarded; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the guarded attributes for the model. |
78
|
|
|
* |
79
|
|
|
* @param string[] $guarded |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
8 |
|
public function guard(array $guarded) |
83
|
|
|
{ |
84
|
8 |
|
$this->guarded = $guarded; |
85
|
|
|
|
86
|
8 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Merge new guarded attributes with existing guarded attributes on the model. |
91
|
|
|
* |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
4 |
|
public function mergeGuarded(array $guarded) |
96
|
|
|
{ |
97
|
4 |
|
$this->guarded = array_merge($this->guarded, $guarded); |
98
|
|
|
|
99
|
4 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Disable all mass assignable restrictions. |
104
|
|
|
* |
105
|
|
|
* @param bool $state |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
12 |
|
public static function unguard($state = true) |
109
|
|
|
{ |
110
|
12 |
|
static::$unguarded = $state; |
111
|
6 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Enable the mass assignment restrictions. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
12 |
|
public static function reguard() |
119
|
|
|
{ |
120
|
12 |
|
static::$unguarded = false; |
121
|
6 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Determine if current state is "unguarded". |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
4 |
|
public static function isUnguarded() |
129
|
|
|
{ |
130
|
4 |
|
return static::$unguarded; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Run the given callable while being unguarded. |
135
|
|
|
* |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
8 |
|
public static function unguarded(callable $callback) |
140
|
|
|
{ |
141
|
8 |
|
if (static::$unguarded) { |
142
|
4 |
|
return $callback(); |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
static::unguard(); |
146
|
|
|
|
147
|
|
|
try { |
148
|
4 |
|
return $callback(); |
149
|
|
|
} finally { |
150
|
4 |
|
static::reguard(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Determine if the given attribute may be mass assigned. |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
244 |
|
public function isFillable($key) |
161
|
|
|
{ |
162
|
244 |
|
if (static::$unguarded) { |
163
|
8 |
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// If the key is in the "fillable" array, we can of course assume that it's |
167
|
|
|
// a fillable attribute. Otherwise, we will check the guarded array when |
168
|
|
|
// we need to determine if the attribute is black-listed on the model. |
169
|
240 |
|
if (in_array($key, $this->getFillable())) { |
170
|
12 |
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// If the attribute is explicitly listed in the "guarded" array then we can |
174
|
|
|
// return false immediately. This means this attribute is definitely not |
175
|
|
|
// fillable and there is no point in going any further in this method. |
176
|
228 |
|
if ($this->isGuarded($key)) { |
177
|
52 |
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
192 |
|
return empty($this->getFillable()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Determine if the given key is guarded. |
185
|
|
|
* |
186
|
|
|
* @param string $key |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
236 |
|
public function isGuarded($key) |
190
|
|
|
{ |
191
|
236 |
|
if (empty($this->getGuarded())) { |
192
|
4 |
|
return false; |
193
|
|
|
} |
194
|
|
|
|
195
|
236 |
|
return $this->getGuarded() == ['*'] |
196
|
236 |
|
|| ! empty(preg_grep('/^'.preg_quote($key, '/').'$/i', $this->getGuarded())); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Determine if the model is totally guarded. |
201
|
|
|
* |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
692 |
|
public function totallyGuarded() |
205
|
|
|
{ |
206
|
692 |
|
return count($this->getFillable()) === 0 && $this->getGuarded() == ['*']; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the fillable attributes of a given array. |
211
|
|
|
* |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
692 |
|
protected function fillableFromArray(array $attributes) |
216
|
|
|
{ |
217
|
692 |
|
if (count($this->getFillable()) > 0 && ! static::$unguarded) { |
218
|
84 |
|
return array_intersect_key($attributes, array_flip($this->getFillable())); |
219
|
|
|
} |
220
|
|
|
|
221
|
620 |
|
return $attributes; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|