Completed
Push — master ( 4b8be1...121055 )
by Matt
12:46
created

Util   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 76.74%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 77
ccs 33
cts 43
cp 0.7674
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A printableErrors() 0 12 1
B loadJson() 0 17 5
A renderErrorTable() 0 12 1
A isLoaderPath() 0 4 1
A schemaPath() 0 4 1
A loadPath() 0 10 1
A getStdin() 0 9 2
1
<?php
2
3
namespace Yuloh\JsonGuardCli;
4
5
use League\JsonGuard\ValidationError;
6
use League\JsonReference\Dereferencer;
7
use Seld\JsonLint\JsonParser;
8
use Symfony\Component\Console\Helper\Table;
9
10
class Util
11
{
12
    public static function printableErrors(array $errors)
13
    {
14 9
        return array_map(function (ValidationError $error) {
15
            $context = $error->getContext();
16 9
            return [
17 9
                ValidationError::MESSAGE     => $error->getMessage(),
18 9
                ValidationError::SCHEMA_PATH => $context[ValidationError::SCHEMA_PATH],
19 9
                ValidationError::DATA_PATH   => $context[ValidationError::DATA_PATH],
20 9
                ValidationError::CAUSE       => $context[ValidationError::CAUSE],
21 9
            ];
22
        }, $errors);
23
    }
24 42
25
    public static function loadJson($json)
26
    {
27 42
        // If it's a loader path just load it since we can't lint that.
28 12
        if (static::isLoaderPath($json)) {
29 33
            return static::loadPath($json);
30 21
        } elseif (file_exists($json)) {
31 33
            $json = file_get_contents($json);
32
        } elseif ($json === '-') {
33
            $json = static::getStdin();
34
        }
35 33
36 6
        if ($parseException = (new JsonParser())->lint($json)) {
37
            throw $parseException;
38
        }
39 30
40
        return json_decode($json, false, 512, JSON_BIGINT_AS_STRING);
41
    }
42 9
43
    public static function renderErrorTable($output, $errors)
44 9
    {
45 9
        (new Table($output))
46 9
            ->setHeaders([
47 9
                'Message',
48 9
                'Schema Path',
49
                'Data Path',
50 42
                'Cause',
51
            ])
52 42
            ->setRows(static::printableErrors($errors))
53
            ->render();
54
    }
55 18
56
    public static function isLoaderPath($path)
57 18
    {
58
        return preg_match('#^[^{\n\r]+\:\/\/[^}\n\r]*#', $path);
59
    }
60 12
61
    public static function schemaPath($file = '')
62 12
    {
63
        return realpath(__DIR__ . '/../schema/' . $file);
64 12
    }
65
66 12
    public static function loadPath($path)
67
    {
68
        list($prefix, $path) = explode('://', $path, 2);
69 9
70
        $loader = Dereferencer::draft4()
71 9
            ->getLoaderManager()
72 9
            ->getLoader($prefix);
73
74
        return $loader->load($path);
75
    }
76
77
    public static function getStdin()
78
    {
79
        $json = '';
80
        $fh   = fopen('php://stdin', 'r');
81
        while ($line = fgets($fh)) {
82
            $json .= $line;
83
        }
84
        return $json;
85
    }
86
}
87