Completed
Push — master ( 6b2337...35aa85 )
by Jasper
14s queued 11s
created

GuardsAttributes::isUnguarded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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