1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yuloh\JsonGuardCli; |
4
|
|
|
|
5
|
|
|
use League\JsonGuard; |
6
|
|
|
use Seld\JsonLint\JsonParser; |
7
|
|
|
use Symfony\Component\Console\Helper\Table; |
8
|
|
|
use League\JsonGuard\Loaders\CurlWebLoader; |
9
|
|
|
use League\JsonGuard\Loaders\FileGetContentsWebLoader; |
10
|
|
|
use League\JsonGuard\Loaders\FileLoader; |
11
|
|
|
|
12
|
|
|
class Util |
13
|
|
|
{ |
14
|
9 |
|
public static function printableErrors(array $errors) |
15
|
|
|
{ |
16
|
|
|
return array_map(function ($error) { |
17
|
9 |
|
$error = $error->toArray(); |
18
|
9 |
|
$error['constraints'] = implode( |
19
|
9 |
|
',', |
20
|
9 |
|
array_map(function ($k, $v) { |
21
|
9 |
|
return $k . ':' . JsonGuard\as_string($v); |
22
|
9 |
|
}, array_keys($error['constraints']), $error['constraints']) |
23
|
9 |
|
); |
24
|
9 |
|
if (is_array($error['value']) || is_object($error['value'])) { |
25
|
6 |
|
$error['value'] = json_encode($error['value']); |
26
|
6 |
|
} else { |
27
|
9 |
|
$error['value'] = JsonGuard\as_string($error['value']); |
28
|
|
|
} |
29
|
9 |
|
return array_values($error); |
30
|
9 |
|
}, $errors); |
31
|
|
|
} |
32
|
|
|
|
33
|
36 |
|
public static function normalizeJsonArgument($json) |
34
|
|
|
{ |
35
|
36 |
|
if (file_exists($json)) { |
36
|
24 |
|
$json = file_get_contents($json); |
37
|
24 |
|
} |
38
|
|
|
|
39
|
36 |
|
if ($parseException = (new JsonParser())->lint($json)) { |
40
|
6 |
|
throw $parseException; |
41
|
|
|
} |
42
|
|
|
|
43
|
33 |
|
return JsonGuard\json_decode($json, false, 512, JSON_BIGINT_AS_STRING); |
44
|
|
|
} |
45
|
|
|
|
46
|
9 |
|
public static function renderErrorTable($output, $errors) |
47
|
|
|
{ |
48
|
9 |
|
(new Table($output)) |
49
|
9 |
|
->setHeaders(['Code', 'Message', 'Pointer', 'Value', 'Constraints']) |
50
|
9 |
|
->setRows(static::printableErrors($errors)) |
51
|
9 |
|
->render(); |
52
|
9 |
|
} |
53
|
|
|
|
54
|
42 |
|
public static function isLoaderPath($path) |
55
|
|
|
{ |
56
|
42 |
|
return preg_match('#^[^{\n\r]+\:\/\/[^}\n\r]*#', $path); |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
public static function schemaPath($file = '') |
60
|
|
|
{ |
61
|
18 |
|
return realpath(__DIR__ . '/../schema/' . $file); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
public static function load($path) |
65
|
|
|
{ |
66
|
6 |
|
list($prefix, $path) = explode('://', $path, 2); |
67
|
|
|
|
68
|
|
|
switch ($prefix) { |
69
|
6 |
|
case 'http': |
70
|
6 |
|
case 'https': |
71
|
3 |
|
if (function_exists('curl_init')) { |
72
|
3 |
|
$loader = new CurlWebLoader($prefix . '://'); |
73
|
3 |
|
} else { |
74
|
|
|
$loader = new FileGetContentsWebLoader($prefix . '://'); |
75
|
|
|
} |
76
|
3 |
|
break; |
77
|
3 |
|
case 'file': |
78
|
3 |
|
$loader = new FileLoader(); |
79
|
3 |
|
break; |
80
|
|
|
default: |
81
|
|
|
throw new \RuntimeException(sprintf('No loader registered for the prefix "%s"', $prefix)); |
82
|
|
|
} |
83
|
|
|
|
84
|
6 |
|
return $loader->load($path); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|