Refactored exception handling in various cast classes to use fully qualified class names for InvalidArgumentException and UnexpectedValueException.
88 lines
3.7 KiB
PHP
88 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Casts;
|
|
|
|
use Domain\Models\ValueObjects\NutritionalValues;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Arr;
|
|
|
|
/** @implements CastsAttributes<NutritionalValues|null,NutritionalValues|null> */
|
|
class NutritionalValuesCast implements CastsAttributes
|
|
{
|
|
/** @param array<string, mixed> $attributes */
|
|
public function get(Model $model, string $key, mixed $value, array $attributes): NutritionalValues
|
|
{
|
|
return new NutritionalValues(
|
|
refWeightG: $this->getFloatOrNull($attributes, 'nv_ref_weight_g'),
|
|
kj: $this->getFloatOrNull($attributes, 'nv_kj'),
|
|
kcal: $this->getFloatOrNull($attributes, 'nv_kcal'),
|
|
fatG: $this->getFloatOrNull($attributes, 'nv_fat_g'),
|
|
satFatG: $this->getFloatOrNull($attributes, 'nv_sat_fat_g'),
|
|
carbG: $this->getFloatOrNull($attributes, 'nv_carb_g'),
|
|
sugarG: $this->getFloatOrNull($attributes, 'nv_sugar_g'),
|
|
fiberG: $this->getFloatOrNull($attributes, 'nv_fiber_g'),
|
|
proteinG: $this->getFloatOrNull($attributes, 'nv_protein_g'),
|
|
saltG: $this->getFloatOrNull($attributes, 'nv_salt_g'),
|
|
servingSizeG: $this->getFloatOrNull($attributes, 'nv_serving_size_g'),
|
|
servingKj: $this->getFloatOrNull($attributes, 'nv_serving_kj'),
|
|
servingKcal: $this->getFloatOrNull($attributes, 'nv_serving_kcal'),
|
|
servingFatG: $this->getFloatOrNull($attributes, 'nv_serving_fat_g'),
|
|
servingSatFatG: $this->getFloatOrNull($attributes, 'nv_serving_sat_fat_g'),
|
|
servingCarbG: $this->getFloatOrNull($attributes, 'nv_serving_carb_g'),
|
|
servingSugarG: $this->getFloatOrNull($attributes, 'nv_serving_sugar_g'),
|
|
servingFiberG: $this->getFloatOrNull($attributes, 'nv_serving_fiber_g'),
|
|
servingProteinG: $this->getFloatOrNull($attributes, 'nv_serving_protein_g'),
|
|
servingSaltG: $this->getFloatOrNull($attributes, 'nv_serving_salt_g'),
|
|
dailyEnergy: $this->getFloatOrNull($attributes, 'nv_daily_energy'),
|
|
dailyFat: $this->getFloatOrNull($attributes, 'nv_daily_fat'),
|
|
dailySatFat: $this->getFloatOrNull($attributes, 'nv_daily_sat_fat'),
|
|
dailyCarb: $this->getFloatOrNull($attributes, 'nv_daily_carb'),
|
|
dailySugar: $this->getFloatOrNull($attributes, 'nv_daily_sugar'),
|
|
dailyFiber: $this->getFloatOrNull($attributes, 'nv_daily_fiber'),
|
|
dailyProtein: $this->getFloatOrNull($attributes, 'nv_daily_protein'),
|
|
dailySalt: $this->getFloatOrNull($attributes, 'nv_daily_salt'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $attributes
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function set(Model $model, string $key, mixed $value, array $attributes): array
|
|
{
|
|
if ($value === null) {
|
|
return new NutritionalValues()->toArray();
|
|
}
|
|
|
|
if (! $value instanceof NutritionalValues) {
|
|
throw new \InvalidArgumentException('The given value is not a NutritionalValues instance.');
|
|
}
|
|
|
|
return $value->toArray();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $attributes
|
|
*
|
|
* @throws \InvalidArgumentException
|
|
*/
|
|
private function getFloatOrNull(array $attributes, string $key): ?float
|
|
{
|
|
$value = Arr::get($attributes, $key);
|
|
|
|
if ((is_string($value) && ctype_digit($value)) || is_float($value)) {
|
|
return (float) $value;
|
|
}
|
|
|
|
throw_if(
|
|
$value !== null,
|
|
\InvalidArgumentException::class,
|
|
"Expected `{$key}` to be numeric of null"
|
|
);
|
|
|
|
return null;
|
|
}
|
|
}
|