ldc-legaulois/app/Casts/HtmlColor.php
2026-09-25 16:25:31 +02:00

38 lines
1 KiB
PHP

<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
/**
* Custom cast Html Color
*/
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceAfterLastUsed
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
class HtmlColor implements CastsAttributes
{
/**
* Cast the given value.
*/
public function get(Model $model, string $key, mixed $value, array $attributes): string
{
return '#' . $this->zeropad(dechex($value), 6);
}
/**
* Prepare the given value for storage.
*/
public function set(Model $model, string $key, mixed $value, array $attributes): int
{
return \intval(hexdec(ltrim($value, '#')));
}
/**
* Pad with zeros a numeric string
*/
protected function zeropad(string $num, int $lim): string
{
return (strlen($num) >= $lim) ? $num : $this->zeropad('0' . $num, $lim);
}
}