ldc-gen-game-web/app/Casts/AsArrayOfDiscount.php

51 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2023-12-07 14:28:29 +01:00
<?php
2026-10-02 12:09:46 +02:00
declare(strict_types=1);
2023-12-07 14:28:29 +01:00
namespace App\Casts;
2026-10-02 12:09:46 +02:00
use App\Lib\Discount;
2023-12-07 14:28:29 +01:00
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
2026-10-02 12:09:46 +02:00
use Illuminate\Database\Eloquent\Model;
2023-12-07 14:28:29 +01:00
class AsArrayOfDiscount implements CastsAttributes
{
/**
* Cast the given value.
*
2026-10-02 12:09:46 +02:00
* @param Model $model
* @param mixed $value
* @return array<Discount>
*
2023-12-07 14:28:29 +01:00
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*/
public function get($model, string $key, $value, array $attributes): array
{
// phpcs:enable
2026-10-02 12:09:46 +02:00
return \collect(\json_decode($value, true))->map(function ($input) use ($model, $key, $attributes): Discount {
2023-12-07 14:28:29 +01:00
$inputCast = new AsDiscount();
2026-10-02 12:09:46 +02:00
2023-12-07 14:28:29 +01:00
return $inputCast->get($model, $key, \json_encode($input), $attributes);
})->all();
}
/**
* Prepare the given value for storage.
*
2026-10-02 12:09:46 +02:00
* @param Model $model
* @param Discount $value
2023-12-07 14:28:29 +01:00
* @return string Json encoded array<\App\Lib\Discount>
2026-10-02 12:09:46 +02:00
*
2023-12-07 14:28:29 +01:00
* @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();
2026-10-02 12:09:46 +02:00
2023-12-07 14:28:29 +01:00
return \json_decode($inputCast->set($model, $key, $input, $attributes));
})->all());
}
}