63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Icefox\DTO;
|
|
|
|
use Icefox\DTO\Support\RuleFactory;
|
|
use Illuminate\Support\Collection;
|
|
use phpDocumentor\Reflection\PseudoTypes\Generic;
|
|
|
|
class Config
|
|
{
|
|
/**
|
|
* @param class-string $class
|
|
**/
|
|
public static function getCaster(string $class): ?callable
|
|
{
|
|
return config('dto.cast.' . $class, null);
|
|
}
|
|
|
|
/**
|
|
* @param class-string $class
|
|
**/
|
|
public static function getRules(string $class): ?callable
|
|
{
|
|
if ($userDefined = config('dto.rules.' . $class, null)) {
|
|
return $userDefined;
|
|
}
|
|
return match ($class) {
|
|
Collection::class => static::rulesIlluminateCollection(...),
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
|
|
/**
|
|
* @return array<string,string[]>
|
|
*/
|
|
private static function rulesIlluminateCollection(ParameterMeta $parameter, RuleFactory $factory): array
|
|
{
|
|
if (is_null($parameter->tag)) {
|
|
return [];
|
|
}
|
|
|
|
$type = $parameter->tag->getType();
|
|
if (!$type instanceof Generic) {
|
|
return [];
|
|
}
|
|
|
|
$subtypes = $type->getTypes();
|
|
|
|
if (count($subtypes) == 0) {
|
|
return ['' => ['array']];
|
|
}
|
|
|
|
$subtype = count($subtypes) == 1 ? $subtypes[0] : $subtypes[1];
|
|
|
|
return $factory->mergeRules(
|
|
['' => ['array']],
|
|
$factory->getRulesFromDocBlock($subtype, '.*'),
|
|
);
|
|
}
|
|
}
|