mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
- updated api docs - fixed top reviews endpoint - fixed reviews parsing - added "contextual" boolean query string parameters, so params without value can be interpreted as "boolean". E.g. ?sfw or ?kid in the url would add "true" value to their corresponding field in the DTO - fixed typesense issues
38 lines
899 B
PHP
38 lines
899 B
PHP
<?php
|
|
|
|
namespace App\Casts;
|
|
|
|
use BackedEnum;
|
|
use Spatie\LaravelData\Casts\Cast;
|
|
use Spatie\LaravelData\Casts\Uncastable;
|
|
use Spatie\LaravelData\Exceptions\CannotCastEnum;
|
|
use Spatie\LaravelData\Support\DataProperty;
|
|
use Throwable;
|
|
|
|
final class EnumCast implements Cast
|
|
{
|
|
public function __construct(
|
|
protected ?string $type = null
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @throws CannotCastEnum
|
|
*/
|
|
public function cast(DataProperty $property, mixed $value, array $context): mixed
|
|
{
|
|
$type = $this->type ?? $property->type->findAcceptedTypeForBaseType(BackedEnum::class);
|
|
|
|
if ($type === null) {
|
|
return Uncastable::create();
|
|
}
|
|
|
|
try {
|
|
/** @noinspection PhpUndefinedMethodInspection */
|
|
return $type::from($value);
|
|
} catch (Throwable $e) {
|
|
throw CannotCastEnum::create($type, $value);
|
|
}
|
|
}
|
|
}
|