CnpjCast.php 633 B

123456789101112131415161718192021222324
  1. <?php
  2. namespace App\Casts;
  3. use App\ValueObjects\Cnpj;
  4. use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
  5. use Illuminate\Database\Eloquent\Model;
  6. class CnpjCast implements CastsAttributes
  7. {
  8. public function get(Model $model, string $key, mixed $value, array $attributes): ?Cnpj
  9. {
  10. return $value === null ? null : new Cnpj((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 Cnpj ? $value : new Cnpj((string) $value))->value();
  18. }
  19. }