CpfCast.php 627 B

123456789101112131415161718192021222324
  1. <?php
  2. namespace App\Casts;
  3. use App\ValueObjects\Cpf;
  4. use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
  5. use Illuminate\Database\Eloquent\Model;
  6. class CpfCast implements CastsAttributes
  7. {
  8. public function get(Model $model, string $key, mixed $value, array $attributes): ?Cpf
  9. {
  10. return $value === null ? null : new Cpf((string) $value);
  11. }
  12. public function set(Model $model, string $key, mixed $value, array $attributes): ?string
  13. {
  14. if ($value === null) {
  15. return null;
  16. }
  17. return ($value instanceof Cpf ? $value : new Cpf((string) $value))->value();
  18. }
  19. }