PHP code example of dmt-software / command-bus-validator

1. Go to this page and download the library: Download dmt-software/command-bus-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dmt-software / command-bus-validator example snippets


 // 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 
  ]
);


 
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)
    }
}

 // 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 
    ]
);