|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AbstractValidateCommand class file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\JsonSchemaBundle\Command; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\JsonSchemaBundle\Exception\ValidationExceptionError; |
|
9
|
|
|
use Graviton\JsonSchemaBundle\Validator\ValidatorInterface; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
12
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Abstract validate JSON definition |
|
19
|
|
|
* |
|
20
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
22
|
|
|
* @link http://swisscom.ch |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractValidateCommand extends Command |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ValidatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $validator; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Load JSON definitions |
|
33
|
|
|
* |
|
34
|
|
|
* @param InputInterface $input Command input |
|
35
|
|
|
* @return SplFileInfo[] |
|
36
|
|
|
*/ |
|
37
|
|
|
abstract protected function loadDefinitions(InputInterface $input); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Set validator |
|
41
|
|
|
* |
|
42
|
|
|
* @param ValidatorInterface $validator Definition validator |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setValidator(ValidatorInterface $validator) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->validator = $validator; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Executes command |
|
52
|
|
|
* |
|
53
|
|
|
* @param InputInterface $input input |
|
54
|
|
|
* @param OutputInterface $output output |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
58
|
|
|
{ |
|
59
|
|
|
$hasErrors = array_reduce( |
|
60
|
|
|
$this->loadDefinitions($input), |
|
61
|
|
|
function ($hasErrors, SplFileInfo $fileInfo) use ($output) { |
|
62
|
|
|
$errors = $this->validateJsonDefinitionFile($fileInfo->getContents()); |
|
63
|
|
|
if (!empty($errors)) { |
|
64
|
|
|
$hasErrors = true; |
|
65
|
|
|
$this->outputErrors($output, $fileInfo, $errors); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $hasErrors; |
|
69
|
|
|
}, |
|
70
|
|
|
false |
|
71
|
|
|
); |
|
72
|
|
|
return $hasErrors ? 1 : 0; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Validate JSON definition |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $json JSON definition |
|
79
|
|
|
* @return ValidationExceptionError[] |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function validateJsonDefinitionFile($json) |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
|
|
return $this->validator->validateJsonDefinition($json); |
|
85
|
|
|
} catch (\Exception $e) { |
|
86
|
|
|
return [new ValidationExceptionError(['message' => $e->getMessage()])]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Convert errors to table rows |
|
92
|
|
|
* |
|
93
|
|
|
* @param OutputInterface $output Output |
|
94
|
|
|
* @param SplFileInfo $fileInfo File info |
|
95
|
|
|
* @param ValidationExceptionError[] $errors Errors |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function outputErrors(OutputInterface $output, SplFileInfo $fileInfo, array $errors) |
|
99
|
|
|
{ |
|
100
|
|
|
$rows = []; |
|
101
|
|
|
foreach ($errors as $error) { |
|
102
|
|
|
$rows[] = new TableSeparator(); |
|
103
|
|
|
$rows[] = [ |
|
104
|
|
|
$error->getProperty(), |
|
105
|
|
|
wordwrap($error->getMessage(), 80, PHP_EOL, false), |
|
106
|
|
|
]; |
|
107
|
|
|
} |
|
108
|
|
|
array_shift($rows); |
|
109
|
|
|
|
|
110
|
|
|
$output->writeln('<comment>'.$fileInfo->getRelativePathname().'</comment>'); |
|
111
|
|
|
(new Table($output)) |
|
112
|
|
|
->setHeaders(['Path', 'Error']) |
|
113
|
|
|
->setRows($rows) |
|
114
|
|
|
->render(); |
|
115
|
|
|
$output->writeln(''); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: