1 | <?php |
||
10 | class Util |
||
11 | { |
||
12 | public static function printableErrors(array $errors) |
||
13 | { |
||
14 | 6 | return array_map(function (ValidationError $error) { |
|
15 | 6 | $context = $error->getContext(); |
|
16 | return [ |
||
17 | 6 | ValidationError::MESSAGE => $error->getMessage(), |
|
18 | 6 | ValidationError::SCHEMA_PATH => $context[ValidationError::SCHEMA_PATH], |
|
19 | 6 | ValidationError::DATA_PATH => $context[ValidationError::DATA_PATH], |
|
20 | 6 | ValidationError::CAUSE => $context[ValidationError::CAUSE], |
|
21 | 3 | ]; |
|
22 | 6 | }, $errors); |
|
23 | } |
||
24 | |||
25 | 28 | public static function loadJson($json) |
|
26 | { |
||
27 | // If it's a loader path just load it since we can't lint that. |
||
28 | 28 | if (static::isLoaderPath($json)) { |
|
29 | 8 | return static::loadPath($json); |
|
30 | 22 | } elseif (file_exists($json)) { |
|
31 | 14 | $json = file_get_contents($json); |
|
32 | 16 | } elseif ($json === '-') { |
|
33 | $json = static::getStdin(); |
||
34 | } |
||
35 | |||
36 | 22 | if ($parseException = (new JsonParser())->lint($json)) { |
|
37 | 4 | throw $parseException; |
|
38 | } |
||
39 | |||
40 | 20 | return json_decode($json, false, 512, JSON_BIGINT_AS_STRING); |
|
41 | } |
||
42 | |||
43 | 6 | public static function renderErrorTable($output, $errors) |
|
44 | { |
||
45 | 6 | (new Table($output)) |
|
46 | 6 | ->setHeaders([ |
|
47 | 6 | 'Message', |
|
48 | 3 | 'Schema Path', |
|
49 | 3 | 'Data Path', |
|
50 | 3 | 'Cause', |
|
51 | 3 | ]) |
|
52 | 6 | ->setRows(static::printableErrors($errors)) |
|
53 | 6 | ->render(); |
|
54 | 6 | } |
|
55 | |||
56 | 28 | public static function isLoaderPath($path) |
|
57 | { |
||
58 | 28 | return preg_match('#^[^{\n\r]+\:\/\/[^}\n\r]*#', $path); |
|
59 | } |
||
60 | |||
61 | 12 | public static function schemaPath($file = '') |
|
65 | |||
66 | 8 | public static function loadPath($path) |
|
67 | { |
||
68 | 8 | list($prefix, $path) = explode('://', $path, 2); |
|
69 | |||
70 | 8 | $loader = Dereferencer::draft4() |
|
71 | 8 | ->getLoaderManager() |
|
72 | 8 | ->getLoader($prefix); |
|
73 | |||
74 | 8 | return $loader->load($path); |
|
75 | } |
||
76 | |||
77 | public static function getStdin() |
||
86 | } |
||
87 |