You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
956 B
37 lines
956 B
<?php
|
|
|
|
namespace App\Domains\System\Casts;
|
|
|
|
use App\Domains\System\Support\ValueObjects\ByteUsage;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use InvalidArgumentException;
|
|
|
|
class ByteHumanReadable implements CastsAttributes
|
|
{
|
|
public function get(Model $model, string $key, mixed $value, array $attributes): ?ByteUsage
|
|
{
|
|
if (is_null($value)) {
|
|
return null;
|
|
}
|
|
|
|
return new ByteUsage((int) $value);
|
|
}
|
|
|
|
public function set(Model $model, string $key, mixed $value, array $attributes): ?int
|
|
{
|
|
if (is_null($value)) {
|
|
return null;
|
|
}
|
|
|
|
if ($value instanceof ByteUsage) {
|
|
return $value->bytes;
|
|
}
|
|
|
|
if (is_numeric($value)) {
|
|
return (int) $value;
|
|
}
|
|
|
|
throw new InvalidArgumentException("The {$key} attribute must be an integer or an instance of ByteUsage.");
|
|
}
|
|
}
|
|
|