|
| 1 | +# Command-Bus-Validator |
| 2 | + |
| 3 | +## Install |
| 4 | +`composer require dmt-software/command-bus-validator` |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +### Default usage |
| 9 | + |
| 10 | +By default this middleware uses the StaticMethodLoader of the [Symfony Validator](https://symfony.com/doc/current/components/validator.html) component. |
| 11 | +If you have installed both `doctrine/annotations` and `doctrine/cache`, this default behaviour is extended with the AnnotationLoader. |
| 12 | + |
| 13 | +Configure and adding this middleware to the commandBus: |
| 14 | +```php |
| 15 | +<?php // src/CommandBus/builder.php |
| 16 | + |
| 17 | +use DMT\CommandBus\Validator\ValidationMiddleware; |
| 18 | +use League\Tactician\CommandBus; |
| 19 | +use League\Tactician\Handler\CommandHandlerMiddleware; |
| 20 | + |
| 21 | +/** @var CommandHandlerMiddleware $commandHandlerMiddleware */ |
| 22 | +$commandBus = new CommandBus( |
| 23 | + [ |
| 24 | + new ValidationMiddleware(), |
| 25 | + $commandHandlerMiddleware |
| 26 | + ] |
| 27 | +); |
| 28 | +``` |
| 29 | +After the CommandBus is added, the commands it receives will be validated when the `handle` method is called: |
| 30 | +```php |
| 31 | +<?php |
| 32 | + |
| 33 | +use DMT\CommandBus\Validator\ValidationException; |
| 34 | +use League\Tactician\CommandBus; |
| 35 | + |
| 36 | +try { |
| 37 | + /** @var object $command */ |
| 38 | + /** @var CommandBus $commandBus */ |
| 39 | + $result = $commandBus->handle($command); |
| 40 | +} catch (ValidationException $exception) { |
| 41 | + $violations = $exception->getViolations(); |
| 42 | + foreach ($violations as $violation) { |
| 43 | + echo $violation->getMessage(); // outputs: the violation message(s) |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +### Using custom configured validator |
| 49 | + |
| 50 | +The validator can also be plugged unto the middleware by providing it to the middleware constructor. |
| 51 | + |
| 52 | +This example uses a FileLoader to determine the constraints for a command. |
| 53 | +```php |
| 54 | +<?php // src/CommandBus/builder.php |
| 55 | + |
| 56 | +use DMT\CommandBus\Validator\ValidationMiddleware; |
| 57 | +use League\Tactician\CommandBus; |
| 58 | +use League\Tactician\Handler\CommandHandlerMiddleware; |
| 59 | +use Symfony\Component\Validator\ValidatorBuilder; |
| 60 | + |
| 61 | +$validator = (new ValidatorBuilder()) |
| 62 | + ->addYamlMapping('config/validation.yaml') |
| 63 | + ->getValidator(); |
| 64 | + |
| 65 | +/** @var CommandHandlerMiddleware $commandHandlerMiddleware */ |
| 66 | +$commandBus = new CommandBus( |
| 67 | + [ |
| 68 | + new ValidationMiddleware($validator), |
| 69 | + $commandHandlerMiddleware |
| 70 | + ] |
| 71 | +); |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +## Further reading |
| 76 | + |
| 77 | +- [Tactician CommandBus](http://tactician.thephpleague.com/) |
| 78 | +- [Validator Loaders](https://symfony.com/doc/current/components/validator/resources.html) |
| 79 | + |
0 commit comments