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