2017 © Pedro Peláez
 

library command-bus-validator

Tactician CommandBus middleware to validate commands using the Symfony Validator

image

dmt-software/command-bus-validator

Tactician CommandBus middleware to validate commands using the Symfony Validator

  • Wednesday, July 4, 2018
  • by proggeler
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Command-Bus-Validator

Build Status Scrutinizer Code Quality Code Coverage, (*1)

Install

composer require dmt-software/command-bus-validator, (*2)

Usage

Default usage

By default this middleware uses the StaticMethodLoader of the Symfony Validator component. If you have installed both doctrine/annotations and doctrine/cache, this default behaviour is extended with the AnnotationLoader., (*3)

Configure and adding this middleware to the commandBus:, (*4)

<?php // src/CommandBus/builder.php

use DMT\CommandBus\Validator\ValidationMiddleware;
use League\Tactician\CommandBus;
use League\Tactician\Handler\CommandHandlerMiddleware;

/** @var CommandHandlerMiddleware $commandHandlerMiddleware */
$commandBus = new CommandBus(
  [
      new ValidationMiddleware(),
      $commandHandlerMiddleware 
  ]
);

After the CommandBus is added, the commands it receives will be validated when the handle method is called:, (*5)

<?php

use DMT\CommandBus\Validator\ValidationException;
use League\Tactician\CommandBus;

try {
    /** @var object $command */
    /** @var CommandBus $commandBus */
    $result = $commandBus->handle($command);
} catch (ValidationException $exception) {
    $violations = $exception->getViolations();
    foreach ($violations as $violation) {
        echo $violation->getMessage(); // outputs: the violation message(s)
    }
}

Using custom configured validator

The validator can also be plugged unto the middleware by providing it to the middleware constructor., (*6)

This example uses a FileLoader to determine the constraints for a command., (*7)

<?php // src/CommandBus/builder.php

use DMT\CommandBus\Validator\ValidationMiddleware;
use League\Tactician\CommandBus;
use League\Tactician\Handler\CommandHandlerMiddleware;
use Symfony\Component\Validator\ValidatorBuilder;

$validator = (new ValidatorBuilder())
    ->addYamlMapping('config/validation.yaml')
    ->getValidator();

/** @var CommandHandlerMiddleware $commandHandlerMiddleware */
$commandBus = new CommandBus(
    [
        new ValidationMiddleware($validator),
        $commandHandlerMiddleware 
    ]
);

Further reading

The Versions

04/07 2018

dev-master

9999999-dev

Tactician CommandBus middleware to validate commands using the Symfony Validator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bas de Mes

middleware validator symfony tactician

01/06 2018

v1.0.0

1.0.0.0

A CommandBus middleware to validate commands using the Symfony Validator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Bas de Mes