http tests
This commit is contained in:
parent
b827038df3
commit
30706c3521
8 changed files with 216 additions and 214 deletions
|
|
@ -9,7 +9,7 @@ use Illuminate\Http\Request;
|
|||
|
||||
trait DataObject
|
||||
{
|
||||
public static function fromRequest(Request $request): mixed
|
||||
public static function fromRequest(Request $request): ?static
|
||||
{
|
||||
return DataObjectFactory::fromRequest(static::class, $request);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -118,14 +118,18 @@ class DataObjectFactory
|
|||
}
|
||||
|
||||
if ($reflectionType instanceof ReflectionNamedType) {
|
||||
$input[$parameterName] = $reflectionType->isBuiltin()
|
||||
? $rawInput[$parameterName]
|
||||
: self::mapInput($reflectionType->__toString(), $rawInput[$parameterName], $routeParameters, $logger);
|
||||
|
||||
if ($reflectionType->isBuiltin()) {
|
||||
if (array_key_exists($parameterName, $rawInput)) {
|
||||
$input[$parameterName] = $rawInput[$parameterName];
|
||||
}
|
||||
} else {
|
||||
$input[$parameterName] = self::mapInput($reflectionType->__toString(), $rawInput[$parameterName], $routeParameters, $logger);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$input[$parameterName] = $rawInput[$parameterName];
|
||||
if (array_key_exists($parameterName, $rawInput)) {
|
||||
$input[$parameterName] = $rawInput[$parameterName];
|
||||
}
|
||||
}
|
||||
$logger->debug('input', $input);
|
||||
return $input;
|
||||
|
|
|
|||
|
|
@ -137,6 +137,6 @@ class ValueFactory
|
|||
|
||||
$arguments[$name] = $parameterArgs;
|
||||
}
|
||||
return App::makeWith($class, $arguments);
|
||||
return new $class(...$arguments);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
6
src/IDataObject.php
Normal file
6
src/IDataObject.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Icefox\DTO;
|
||||
|
||||
interface IDataObject {}
|
||||
|
||||
23
src/Providers/DataObjectServiceProvider.php
Normal file
23
src/Providers/DataObjectServiceProvider.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Icefox\DTO\Providers;
|
||||
|
||||
use Icefox\DTO\Factories\DataObjectFactory;
|
||||
use Icefox\DTO\IDataObject;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class DataObjectServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->beforeResolving(function ($abstract, $parameters, $app) {
|
||||
if ($app->has($abstract)) {
|
||||
return;
|
||||
}
|
||||
if (is_subclass_of($abstract, IDataObject::class)) {
|
||||
$app->bind($abstract, fn($container) => DataObjectFactory::fromRequest($abstract, $container['request']));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue