Completed
Push — master ( d390f4...706501 )
by Matt
06:36
created

Util::schemaPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
            $error['value'] = is_object($error['value']) || is_array($error['value']) ? json_encode($error['value']) : JsonGuard\as_string($error['value']);
25 9
            return array_values($error);
26 9
        }, $errors);
27
    }
28
29 36
    public static function normalizeJsonArgument($json)
30
    {
31 36
        if (file_exists($json)) {
32 24
            $json = file_get_contents($json);
33 24
        }
34
35 36
        if ($parseException = (new JsonParser())->lint($json)) {
36 6
            throw $parseException;
37
        }
38
39 33
        return JsonGuard\json_decode($json, false, 512, JSON_BIGINT_AS_STRING);
40
    }
41
42 9
    public static function renderErrorTable($output, $errors)
43
    {
44 9
        (new Table($output))
45 9
            ->setHeaders(['Code', 'Message', 'Pointer', 'Value', 'Constraints'])
46 9
            ->setRows(static::printableErrors($errors))
47 9
            ->render();
48 9
    }
49
50 36
    public static function isLoaderPath($path)
51
    {
52 36
        return preg_match('#^[^{\n\r]+\:\/\/[^}\n\r]*#', $path);
53
    }
54
55 12
    public static function schemaPath($file = '')
56
    {
57 12
        return realpath(__DIR__ . '/../schema/' . $file);
58
    }
59
60
    public static function load($path)
61
    {
62
        list($prefix, $path) = explode('://', $path, 2);
63
64
        switch ($prefix) {
65
            case 'http':
66
            case 'https':
67
                if (function_exists('curl_init')) {
68
                    $loader = new CurlWebLoader($prefix . '://');
69
                } else {
70
                    $loader = new FileGetContentsWebLoader($prefix . '://');
71
                }
72
                break;
73
            case 'file':
74
                $loader = new FileLoader();
75
                break;
76
            default:
77
                throw new \RuntimeException(sprintf('No loader registered for the prefix "%s"', $prefix));
78
        }
79
80
        return $loader->load($path);
81
    }
82
}
83