50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Casts;
|
|
|
|
use App\Lib\Discount;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class AsArrayOfDiscount implements CastsAttributes
|
|
{
|
|
/**
|
|
* Cast the given value.
|
|
*
|
|
* @param Model $model
|
|
* @param mixed $value
|
|
* @return array<Discount>
|
|
*
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
|
*/
|
|
public function get($model, string $key, $value, array $attributes): array
|
|
{
|
|
// phpcs:enable
|
|
return \collect(\json_decode($value, true))->map(function ($input) use ($model, $key, $attributes): Discount {
|
|
$inputCast = new AsDiscount();
|
|
|
|
return $inputCast->get($model, $key, \json_encode($input), $attributes);
|
|
})->all();
|
|
}
|
|
|
|
/**
|
|
* Prepare the given value for storage.
|
|
*
|
|
* @param Model $model
|
|
* @param Discount $value
|
|
* @return string Json encoded array<\App\Lib\Discount>
|
|
*
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
|
|
*/
|
|
public function set($model, string $key, $value, array $attributes)
|
|
{
|
|
// phpcs:enable
|
|
return \json_encode(\collect($value)->map(function ($input) use ($model, $key, $attributes) {
|
|
$inputCast = new AsDiscount();
|
|
|
|
return \json_decode($inputCast->set($model, $key, $input, $attributes));
|
|
})->all());
|
|
}
|
|
}
|