diff --git a/README.md b/README.md index cd61f53..11264f4 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ This component allow you to inject DTO object mapped from the Request to the action. -## How to use? +## 1. How to use? -### Create DTO from the Request +### 1.1. Create DTO from the Request -#### Easy way (in case if you want to map data from the $request->all()) +#### 1.1.1 Easy way (in case if you want to map data from the $request->all()) You should extend DataTransferObject.php class and define your rules for mapping and validation. Example: ```PHP @@ -51,7 +51,41 @@ final class RoomSearchRequestData extends DataTransferObject } ``` -### Change rendering of the exception +#### 1.1.2 Configure DTO parameter in the construct via ServiceProvider + +```PHP + $request->all(), // RoomSearchRequestData DTO class + ]; + } +} + +``` + + +### 1.2. Change rendering of the exception 1. Create Exception which will extend /Exception/AbstractException.php and implement toResponse method @@ -88,7 +122,6 @@ return [ ``` -## Todo +## 1.3. Todo - check nested DTO validation - contextual binding -- re-check DTO creating via ServiceProvider define diff --git a/src/RequestMapperResolver.php b/src/RequestMapperResolver.php index fecd221..b200eff 100644 --- a/src/RequestMapperResolver.php +++ b/src/RequestMapperResolver.php @@ -22,7 +22,7 @@ class RequestMapperResolver /** * @var array */ - private $map; + private $map = []; /** * @var Config @@ -58,10 +58,14 @@ public function __construct(Config $config, Container $container, ValidatorInter */ public function map(array $map): void { - $this->map = $map; + $this->map = array_merge($this->map, $map); foreach ($map as $className => $arguments) { $this->container->singleton($className, function () use ($className, $arguments) { - return new $className(... $arguments); + if (!\is_array($arguments)) { + throw new \InvalidArgumentException('$arguments should be array'); + } + + return new $className($arguments); }); $this->container->afterResolving($className, function ($resolved) { $this->applyAfterResolvingValidation($resolved); @@ -74,7 +78,7 @@ public function map(array $map): void */ public function getRegisterClass(): array { - return array_keys($this->map ?? []); + return array_keys($this->map); } /**