|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Alt Three Validator. |
| 5 | + * |
| 6 | + * (c) Alt Three LTD <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace AltThree\Validator; |
| 13 | + |
| 14 | +use Closure; |
| 15 | +use Illuminate\Contracts\Validation\Factory; |
| 16 | +use ReflectionClass; |
| 17 | +use ReflectionProperty; |
| 18 | + |
| 19 | +/** |
| 20 | + * This is the command validating middleware class. |
| 21 | + * |
| 22 | + * @author Graham Campbell <[email protected]> |
| 23 | + */ |
| 24 | +class ValidatingMiddleware |
| 25 | +{ |
| 26 | + /** |
| 27 | + * The validation factory instance. |
| 28 | + * |
| 29 | + * @var \Illuminate\Contracts\Validation\Factory |
| 30 | + */ |
| 31 | + protected $factory; |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a new run analysis job handler instance. |
| 35 | + * |
| 36 | + * @param \Illuminate\Contracts\Validation\Factory $factory |
| 37 | + * |
| 38 | + * @return void |
| 39 | + */ |
| 40 | + public function __construct(Factory $factory) |
| 41 | + { |
| 42 | + $this->factory = $factory; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Validate the command before execution. |
| 47 | + * |
| 48 | + * @param object $command |
| 49 | + * @param \Closure $next |
| 50 | + * |
| 51 | + * @throws \Watson\Validating\ValidationException |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function handle($command, Closure $next) |
| 56 | + { |
| 57 | + if (property_exists($command, 'rules') && is_array($command->rules)) { |
| 58 | + $this->validate($command); |
| 59 | + } |
| 60 | + |
| 61 | + return $next($command); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Validate the command. |
| 66 | + * |
| 67 | + * @param object $command |
| 68 | + * |
| 69 | + * @throws \Watson\Validating\ValidationException |
| 70 | + * |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + protected function validate($command) |
| 74 | + { |
| 75 | + $messages = property_exists($command, 'validationMessages') ? $command->validationMessages : []; |
| 76 | + |
| 77 | + $validator = $this->factory->make($this->getData($command), $command->rules, $messages); |
| 78 | + |
| 79 | + if ($validator->fails()) { |
| 80 | + throw new ValidationException($validator->getMessageBag()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the data to be validated. |
| 86 | + * |
| 87 | + * @param object $command |
| 88 | + * |
| 89 | + * @return array |
| 90 | + */ |
| 91 | + protected function getData($command) |
| 92 | + { |
| 93 | + $data = []; |
| 94 | + |
| 95 | + foreach ((new ReflectionClass($command))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
| 96 | + $name = $property->getName(); |
| 97 | + $value = $property->getValue($command); |
| 98 | + |
| 99 | + if (in_array($name, ['rules', 'validationMessages'], true) || is_object($value)) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + $data[$name] = $value; |
| 104 | + } |
| 105 | + |
| 106 | + return $data; |
| 107 | + } |
| 108 | +} |
0 commit comments