Skip to content

Commit

Permalink
Updated the Slug value object to be castable.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbushell committed May 20, 2024
1 parent 39e246f commit 1f36839
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Behaviours/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Eloquence\Behaviours;

use Hashids\Hashids;
use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Support\Str;

class Slug implements Jsonable
class Slug implements Castable, Jsonable
{
/**
* @var string
Expand Down Expand Up @@ -71,4 +73,22 @@ public function toJson($options = 0): string
{
return $this->__toString();
}

public static function castUsing(array $arguments): CastsAttributes
{
return new class implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?Slug
{
return null === $value ? $value : new Slug($value);
}

public function set($model, string $key, $value, array $attributes): array
{
return [
$key => null === $value ? $value : (string) $value
];
}
};
}
}

0 comments on commit 1f36839

Please sign in to comment.