62 lines
2.2 KiB
PHP
62 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Casts;
|
|
|
|
/**
|
|
* Custom cast RGBA Color
|
|
* https://laravel.com/docs/8.x/eloquent-mutators#custom-casts
|
|
*/
|
|
class RgbaColor extends HtmlColor
|
|
{
|
|
/**
|
|
* Cast the given value.
|
|
*
|
|
* @param Model $model
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param array $attributes
|
|
* @return string
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
|
|
*/
|
|
public function get($model, $key, $value, $attributes)
|
|
{
|
|
// phpcs:enable
|
|
$intValues = collect(\str_split($this->zeropad(\dechex(\intval($value)), 8), 2))->map(function ($hex) {
|
|
return intval(\hexdec($hex));
|
|
});
|
|
// * Set Transparency as float
|
|
$hexString = $intValues->push(\round($intValues->pop() / 100, 2))->implode(',');
|
|
return "rgba($hexString)";
|
|
}
|
|
|
|
/**
|
|
* Prepare the given value for storage.
|
|
*
|
|
* @param Model $model
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param array $attributes
|
|
* @return integer
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
|
|
*/
|
|
public function set($model, $key, $value, $attributes)
|
|
{
|
|
// phpcs:enable
|
|
$value = \rtrim(\ltrim(\str_replace(' ', '', \str_replace('rgba', '', \strval($value))), '('), ')');
|
|
$intValues = collect(\explode(',', $value))->map(function ($val) {
|
|
return \floatval($val);
|
|
});
|
|
// * Set Transparency as int
|
|
$hexConcat = $intValues->push($intValues->pop() * 100)->map(function ($value) {
|
|
return $this->zeropad(dechex(\intval($value)), 2);
|
|
})->implode('');
|
|
|
|
return \intval(\hexdec($hexConcat));
|
|
}
|
|
}
|