Repository: laravel/framework
File: src/Illuminate/Database/Eloquent/Casts/AsBinary.php (PHP)

Code before:
<?php

namespace Illuminate\Database\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\Binary;
use InvalidArgumentException;

class AsBinary implements Castable
{
    public static function castUsing(array $arguments)
    {
        return new class($arguments) implements CastsAttributes {
            private string $format;

            public function __construct(protected array $arguments)
            {
                $arguments = array_pad(array_values($this->arguments), 1, '');
                $this->format = $arguments[0] ?: throw new InvalidArgumentException('The binary format is required.');
                $allowedFormats = array_keys(Binary::formats());

                if (! in_array($this->format, $allowedFormats, true)) {
                    throw new InvalidArgumentException('The provided format [' . $this->format . '] is invalid.');
                }
            }

            public function get($model, $key, $value, $attributes)
            {
                if (! isset($attributes[$key])) {
                    return;
                }

                return Binary::decode($attributes[$key], $this->format);
            }

            public function set($model, $key, $value, $attributes)
            {
                return [$key => Binary::encode($value, $this->format)];
            }
        };
    }

    public static function uuid(): string
    {
        return self::of('uuid');
    }

    public static function ulid(): string

Review comment (suggestion):
The error message could provide a list of allowed values, which might be helpful:

```suggestion
                    throw new InvalidArgumentException('The provided format [' . $this->format . '] is invalid. Please choose one of: ' . implode(', ', $allowedFormats));
```

Code after:
<?php

namespace Illuminate\Database\Eloquent\Casts;

use Illuminate\Contracts\Database\Eloquent\Castable;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Support\BinaryCodec;
use InvalidArgumentException;

class AsBinary implements Castable
{
    /**
     * Get the caster class to use when casting from / to this cast target.
     *
     * @param  array{string}  $arguments
     * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes
     */
    public static function castUsing(array $arguments)
    {
        return new class($arguments) implements CastsAttributes
        {
            protected string $format;

            public function __construct(protected array $arguments)
            {
                $this->format = $this->arguments[0]
                    ?? throw new InvalidArgumentException('The binary codec format is required.');

                if (! in_array($this->format, BinaryCodec::formats(), true)) {
                    throw new InvalidArgumentException(sprintf(
                        'Unsupported binary codec format [%s]. Allowed formats are: %s.',
                        $this->format,
                        implode(', ', BinaryCodec::formats()),
                    ));
                }
            }

            public function get($model, $key, $value, $attributes)
            {
                return BinaryCodec::decode($attributes[$key] ?? null, $this->format);
            }

            public function set($model, $key, $value, $attributes)
            {
                return [$key => BinaryCodec::encode($value, $this->format)];
            }
        };
    }