Skip to content

Commit

Permalink
feat: support strict models by handling MissingAttributeException
Browse files Browse the repository at this point in the history
This update ensures compatibility with Laravel's `Model::shouldBeStrict()` feature.

For example, the following code may throw a `MissingAttributeException` if any defined attribute event attributes are not fetched:

```php
Order::query()->pluck('status', 'id');
```

To address this, each `getAttribute()` call is now wrapped in a try-catch block, preventing exceptions and maintaining functionality in strict mode.
  • Loading branch information
maartenpaauw committed Nov 16, 2024
1 parent a8d74ae commit f82fdc8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/AttributeEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kleemans;

use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

Expand All @@ -28,7 +29,12 @@ private function fireAttributeEvents(): void
{
foreach ($this->getAttributeEvents() as $change => $event) {
[$attribute, $expected] = explode(':', $change);
$value = $this->getAttribute($attribute);

try {
$value = $this->getAttribute($attribute);
} catch (MissingAttributeException) {
continue;
}

// Accessor
if ($this->hasAccessor($attribute)) {
Expand All @@ -46,7 +52,11 @@ private function fireAttributeEvents(): void
continue; // Not changed
}

$value = Arr::get($this->getAttribute($attribute), $path);
try {
$value = Arr::get($this->getAttribute($attribute), $path);
} catch (MissingAttributeException) {
continue;
}
}

// Regular attribute
Expand Down Expand Up @@ -100,7 +110,12 @@ private function syncOriginalAccessors(): void
continue; // Attribute does not have accessor
}

$value = $this->getAttribute($attribute);
try {
$value = $this->getAttribute($attribute);
} catch (MissingAttributeException) {
continue;
}

if ($value === null) {
continue; // Attribute does not exist
}
Expand All @@ -116,15 +131,25 @@ public function isDirtyAccessor(string $attribute): bool
}

$originalValue = $this->originalAccessors[$attribute];
$currentValue = $this->getAttribute($attribute);

try {
$currentValue = $this->getAttribute($attribute);
} catch (MissingAttributeException) {
return false;
}

return $originalValue !== $currentValue;
}

public function isDirtyNested(string $attribute, string $path): bool
{
$originalValue = Arr::get($this->getOriginal($attribute), $path);
$currentValue = Arr::get($this->getAttribute($attribute), $path);

try {
$currentValue = Arr::get($this->getAttribute($attribute), $path);
} catch (MissingAttributeException) {
return false;
}

if ($currentValue === null) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions tests/AttributeEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function setUp(): void
{
$this->initEventDispatcher();
$this->initDatabase();

Model::shouldBeStrict();
}

public function test_it_still_dispatches_native_events(): void
Expand Down

0 comments on commit f82fdc8

Please sign in to comment.