Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
Chris@0
|
4
|
Chris@0
|
5 use Consolidation\AnnotatedCommand\AnnotationData;
|
Chris@0
|
6 use Consolidation\AnnotatedCommand\CommandData;
|
Chris@0
|
7 use Consolidation\AnnotatedCommand\CommandError;
|
Chris@0
|
8 use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
Chris@0
|
9 use Consolidation\AnnotatedCommand\Hooks\ValidatorInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Call hooks
|
Chris@0
|
13 */
|
Chris@0
|
14 class ValidateHookDispatcher extends HookDispatcher implements ValidatorInterface
|
Chris@0
|
15 {
|
Chris@0
|
16 public function validate(CommandData $commandData)
|
Chris@0
|
17 {
|
Chris@0
|
18 $hooks = [
|
Chris@0
|
19 HookManager::PRE_ARGUMENT_VALIDATOR,
|
Chris@0
|
20 HookManager::ARGUMENT_VALIDATOR,
|
Chris@0
|
21 HookManager::POST_ARGUMENT_VALIDATOR,
|
Chris@0
|
22 HookManager::PRE_COMMAND_HOOK,
|
Chris@0
|
23 HookManager::COMMAND_HOOK,
|
Chris@0
|
24 ];
|
Chris@0
|
25 $validators = $this->getHooks($hooks, $commandData->annotationData());
|
Chris@0
|
26 foreach ($validators as $validator) {
|
Chris@0
|
27 $validated = $this->callValidator($validator, $commandData);
|
Chris@0
|
28 if ($validated === false) {
|
Chris@0
|
29 return new CommandError();
|
Chris@0
|
30 }
|
Chris@0
|
31 if (is_object($validated)) {
|
Chris@0
|
32 return $validated;
|
Chris@0
|
33 }
|
Chris@0
|
34 }
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 protected function callValidator($validator, CommandData $commandData)
|
Chris@0
|
38 {
|
Chris@0
|
39 if ($validator instanceof ValidatorInterface) {
|
Chris@0
|
40 return $validator->validate($commandData);
|
Chris@0
|
41 }
|
Chris@0
|
42 if (is_callable($validator)) {
|
Chris@0
|
43 return $validator($commandData);
|
Chris@0
|
44 }
|
Chris@0
|
45 }
|
Chris@0
|
46 }
|