Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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