refactor
This commit is contained in:
parent
367858c97c
commit
6b1a385292
13 changed files with 191 additions and 80 deletions
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Icefox\DTO;
|
||||
|
||||
use Icefox\DTO\Support\RuleFactory;
|
||||
use Icefox\DTO\RuleFactory;
|
||||
use Illuminate\Support\Collection;
|
||||
use phpDocumentor\Reflection\PseudoTypes\Generic;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Icefox\DTO\Factories;
|
||||
namespace Icefox\DTO;
|
||||
|
||||
use Icefox\DTO\ParameterMeta;
|
||||
use Icefox\DTO\Support\RuleFactory;
|
||||
use Icefox\DTO\RuleFactory;
|
||||
use phpDocumentor\Reflection\PseudoTypes\Generic;
|
||||
|
||||
class CollectionFactory
|
||||
class CustomHandlers
|
||||
{
|
||||
/**
|
||||
* @return array<string,string[]>
|
||||
*/
|
||||
public static function rules(ParameterMeta $parameter, RuleFactory $factory): array
|
||||
public static function CollectionRules(ParameterMeta $parameter, RuleFactory $factory): array
|
||||
{
|
||||
if (is_null($parameter->tag)) {
|
||||
return [];
|
||||
|
|
@ -36,3 +35,4 @@ class CollectionFactory
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -4,14 +4,14 @@ namespace Icefox\DTO;
|
|||
|
||||
use Icefox\DTO\Attributes\FromInput;
|
||||
use Icefox\DTO\Attributes\FromRouteParameter;
|
||||
use Icefox\DTO\Support\RuleFactory;
|
||||
use Icefox\DTO\Support\ValueFactory;
|
||||
use Icefox\DTO\RuleFactory;
|
||||
use Icefox\DTO\ValueFactory;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\Validation\Validator;
|
||||
use ReflectionClass;
|
||||
|
||||
class DataObjectFactory
|
||||
{
|
||||
|
|
@ -31,7 +31,7 @@ class DataObjectFactory
|
|||
*/
|
||||
public static function fromArray(string $class, array $input, array $routeParameters): ?object
|
||||
{
|
||||
$logger = new Log();
|
||||
$logger = Log::channel('dto');
|
||||
$parameters = ReflectionHelper::getParametersMeta($class);
|
||||
foreach ($parameters as $parameter) {
|
||||
$parameterName = $parameter->reflection->getName();
|
||||
|
|
@ -60,7 +60,7 @@ class DataObjectFactory
|
|||
// continue;
|
||||
// }
|
||||
}
|
||||
$logger->inputRaw($input);
|
||||
$logger->debug('input', $input);
|
||||
|
||||
$rules = (new RuleFactory($logger))->make($class);
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ class DataObjectFactory
|
|||
: App::makeWith(Validator::class, ['data' => $input, 'rules' => $rules]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$logger->validationErrors($validator->errors()->toArray());
|
||||
$logger->warning('validation error', $validator->errors()->toArray());
|
||||
if (method_exists($class, 'fails')) {
|
||||
return App::call("$class::fails", ['validator' => $validator ]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,41 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Icefox\DTO;
|
||||
|
||||
use Icefox\DTO\Attributes\FromInput;
|
||||
use Icefox\DTO\Attributes\FromRouteParameter;
|
||||
|
||||
class InputFactory
|
||||
{
|
||||
public function __construct(public readonly Log $log) {}
|
||||
|
||||
public function make(string $class): array
|
||||
{
|
||||
$map = [];
|
||||
$parameters = ReflectionHelper::getParametersMeta($class);
|
||||
foreach ($parameters as $parameter) {
|
||||
$name = $parameter->reflection->getName();
|
||||
|
||||
foreach ($parameter->reflection->getAttributes(FromRouteParameter::class) as $attr) {
|
||||
$map[$name][] = 'route_' . $attr->newInstance()->name;
|
||||
}
|
||||
|
||||
foreach ($parameter->reflection->getAttributes(FromInput::class) as $attr) {
|
||||
$map[$name][] = $attr->newInstance()->name;
|
||||
}
|
||||
|
||||
$map[$name][] = $name;
|
||||
}
|
||||
return $map;
|
||||
}
|
||||
|
||||
private static self $_instance;
|
||||
|
||||
public static function instance(): self
|
||||
{
|
||||
if (empty(self::$_instance)) {
|
||||
self::$_instance = new self(new Log());
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
}
|
||||
|
|
@ -45,4 +45,3 @@ class ReflectionHelper
|
|||
return self::$cache[$class];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Icefox\DTO\Support;
|
||||
namespace Icefox\DTO;
|
||||
|
||||
use Icefox\DTO\Attributes\Flat;
|
||||
use Icefox\DTO\Attributes\OverwriteRules;
|
||||
use Icefox\DTO\Config;
|
||||
use Icefox\DTO\ParameterMeta;
|
||||
use Icefox\DTO\ReflectionHelper;
|
||||
use Icefox\DTO\RuleFactory;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
|
@ -2,16 +2,14 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Icefox\DTO\Support;
|
||||
namespace Icefox\DTO;
|
||||
|
||||
use Icefox\DTO\Attributes\CastWith;
|
||||
use Icefox\DTO\Attributes\Flat;
|
||||
use Icefox\DTO\Config;
|
||||
use Icefox\DTO\ParameterMeta;
|
||||
use Icefox\DTO\ReflectionHelper;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use ReflectionNamedType;
|
||||
use ReflectionParameter;
|
||||
use phpDocumentor\Reflection\DocBlock\Tags\Param;
|
||||
use phpDocumentor\Reflection\PseudoTypes\Generic;
|
||||
use phpDocumentor\Reflection\Type;
|
||||
|
|
@ -102,6 +100,9 @@ class ValueFactory
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $input
|
||||
*/
|
||||
public static function make(string $class, array $input): object
|
||||
{
|
||||
$parameters = ReflectionHelper::getParametersMeta($class);
|
||||
Loading…
Add table
Add a link
Reference in a new issue