|
12 | 12 | use AustinHeap\Database\Encryption\EncryptionFacade as DatabaseEncryption;
|
13 | 13 | use Illuminate\Contracts\Encryption\DecryptException;
|
14 | 14 | use Illuminate\Contracts\Encryption\EncryptException;
|
| 15 | +use Illuminate\Support\Arr; |
15 | 16 | use Illuminate\Support\Facades\Crypt;
|
16 | 17 | use Illuminate\Support\Facades\Log;
|
17 | 18 |
|
@@ -286,6 +287,48 @@ public function getDirty()
|
286 | 287 | return $dirty;
|
287 | 288 | }
|
288 | 289 |
|
| 290 | + /** |
| 291 | + * Determine if the new and old values for a given key are equivalent. |
| 292 | + * |
| 293 | + * @param string $key |
| 294 | + * @return bool |
| 295 | + */ |
| 296 | + public function originalIsEquivalent($key) |
| 297 | + { |
| 298 | + if (! array_key_exists($key, $this->original)) { |
| 299 | + return false; |
| 300 | + } |
| 301 | + |
| 302 | + // Unfortunately this method isn't single responsibility, it retrieves and compares attributes, |
| 303 | + // therefore we have to copy the whole thing just to change the behaviour of one of its responsibilities. |
| 304 | + $attribute = $this->doDecryptAttribute($key, Arr::get($this->attributes, $key)); |
| 305 | + $original = Arr::get($this->original, $key); |
| 306 | + |
| 307 | + if ($attribute === $original) { |
| 308 | + return true; |
| 309 | + } elseif (is_null($attribute)) { |
| 310 | + return false; |
| 311 | + } elseif ($this->isDateAttribute($key)) { |
| 312 | + return $this->fromDateTime($attribute) === |
| 313 | + $this->fromDateTime($original); |
| 314 | + } elseif ($this->hasCast($key, ['object', 'collection'])) { |
| 315 | + return $this->castAttribute($key, $attribute) == |
| 316 | + $this->castAttribute($key, $original); |
| 317 | + } elseif ($this->hasCast($key, ['real', 'float', 'double'])) { |
| 318 | + if (($attribute === null && $original !== null) || ($attribute !== null && $original === null)) { |
| 319 | + return false; |
| 320 | + } |
| 321 | + |
| 322 | + return abs($this->castAttribute($key, $attribute) - $this->castAttribute($key, $original)) < PHP_FLOAT_EPSILON * 4; |
| 323 | + } elseif ($this->hasCast($key, static::$primitiveCastTypes)) { |
| 324 | + return $this->castAttribute($key, $attribute) === |
| 325 | + $this->castAttribute($key, $original); |
| 326 | + } |
| 327 | + |
| 328 | + return is_numeric($attribute) && is_numeric($original) |
| 329 | + && strcmp((string) $attribute, (string) $original) === 0; |
| 330 | + } |
| 331 | + |
289 | 332 | /**
|
290 | 333 | * Set a given attribute on the model.
|
291 | 334 | *
|
|
0 commit comments