Skip to content

Commit

Permalink
Merge pull request #9 from jpkleemans/laravel-9
Browse files Browse the repository at this point in the history
Add support for Laravel 9 style accessors
  • Loading branch information
jpkleemans committed Feb 19, 2022
2 parents 7a40e27 + e40ff2e commit da71a6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Product extends Model
}
```

> For more info on accessors, visit the <a href="https://laravel.com/docs/eloquent-mutators#defining-an-accessor" target="_blank">Laravel Docs</a>
> You can also use the [new way of defining accessors](https://laravel.com/docs/9.x/releases#eloquent-accessors-and-mutators) introduced in Laravel 9.
## Sponsors

Expand Down
18 changes: 16 additions & 2 deletions src/AttributeEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private function fireAttributeEvents(): void
$value = $this->getAttribute($attribute);

// Accessor
if ($this->hasGetMutator($attribute)) {
if ($this->hasAccessor($attribute)) {
if (!$this->isDirtyAccessor($attribute)) {
continue; // Not changed
}
Expand Down Expand Up @@ -73,7 +73,7 @@ private function syncOriginalAccessors(): void
foreach ($this->getAttributeEvents() as $change => $event) {
[$attribute] = explode(':', $change);

if (!$this->hasGetMutator($attribute)) {
if (!$this->hasAccessor($attribute)) {
continue; // Attribute does not have accessor
}

Expand Down Expand Up @@ -123,4 +123,18 @@ private function getAttributeEvents(): iterable
yield $change => $event;
}
}

private function hasAccessor(string $attribute): bool
{
if ($this->hasGetMutator($attribute)) {
return true;
}

// Check if `hasAttributeGetMutator` exists to maintain compatibility with versions before Laravel 9.
if (method_exists($this, 'hasAttributeGetMutator') && $this->hasAttributeGetMutator($attribute)) {
return true;
}

return false;
}
}
9 changes: 7 additions & 2 deletions tests/Fake/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kleemans\Tests\Fake;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Kleemans\AttributeEvents;

Expand Down Expand Up @@ -54,9 +55,13 @@ class Order extends Model
'meta->invoice->downloaded:true' => Events\InvoiceDownloaded::class,
];

public function getShippingCountryAttribute(): string
protected function shippingCountry(): Attribute
{
return substr($this->shipping_address, -2); // Last 2 characters of the address contain the country code.
return new Attribute(
get: function ($value, $attributes) {
return substr($attributes['shipping_address'], -2); // Last 2 characters of the address contain the country code.
}
);
}

public function getIsPaidAttribute(): bool
Expand Down

0 comments on commit da71a6d

Please sign in to comment.