Skip to content

Commit

Permalink
[mapping] Allow to determinate request-mapping arguments as a one arg…
Browse files Browse the repository at this point in the history
…ument (not array)
  • Loading branch information
TBlindaruk committed Dec 16, 2018
1 parent a9aaa7d commit b9e2441
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
<?php
declare(strict_types = 1);

namespace App\Http;

use Illuminate\Http\Request;
use Maksi\RequestMapperL\Support\RequestMapperServiceProvider;

/**
* Class RequestMapperProvider
*
* @package App\Http
*/
class RequestMapperProvider extends RequestMapperServiceProvider
{
/**
* @param Request $request
*
* @return array
*/
protected function resolveMap(Request $request): array
{
return [
RoomSearchRequestData::class => $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

Expand Down Expand Up @@ -88,7 +122,6 @@ return [

```

## Todo
## 1.3. Todo
- check nested DTO validation
- contextual binding
- re-check DTO creating via ServiceProvider define
12 changes: 8 additions & 4 deletions src/RequestMapperResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RequestMapperResolver
/**
* @var array
*/
private $map;
private $map = [];

/**
* @var Config
Expand Down Expand Up @@ -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);
Expand All @@ -74,7 +78,7 @@ public function map(array $map): void
*/
public function getRegisterClass(): array
{
return array_keys($this->map ?? []);
return array_keys($this->map);
}

/**
Expand Down

0 comments on commit b9e2441

Please sign in to comment.