Completed
Push — master ( ef23ad...7d5a65 )
by Antonio Carlos
01:10
created

update.php ➔ readJsonFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
$path = __DIR__.'/trivia/en/todo';
4
5
file_put_contents(__DIR__.'/trivia.json', json_encode(load($path)));
6
7
// ------------------------------------------------------------------------------------------------------------
8
9
function load($path)
10
{
11
    $questions = [];
12
13
    foreach (scandir($path) as $file) {
14
        if (!ends_with('.json', $file)) {
15
            continue;
16
        }
17
18
        $questions = array_merge($questions, readJsonFile("$path/$file"));
19
    }
20
21
    return $questions;
22
}
23
24
function ends_with($needle, $haystack)
25
{
26
    $length = strlen($needle);
27
28
    return $length === 0 ||
29
        (substr($haystack, -$length) === $needle);
30
}
31
32
function readJsonFile($file)
33
{
34
    echo "Reading file $file\n";
35
36
    $string = json_decode(loadAndFixJson($file), true);
37
38
    if (jsonError() !== false) {
39
        echo jsonError();
40
        die;
41
    }
42
43
    return $string;
44
}
45
46
function jsonError()
47
{
48
    switch (json_last_error()) {
49
        case JSON_ERROR_NONE:
50
            return false;
51
        case JSON_ERROR_DEPTH:
52
            return ' - Maximum stack depth exceeded';
53
            break;
54
        case JSON_ERROR_STATE_MISMATCH:
55
            return ' - Underflow or the modes mismatch';
56
            break;
57
        case JSON_ERROR_CTRL_CHAR:
58
            return ' - Unexpected control character found';
59
            break;
60
        case JSON_ERROR_SYNTAX:
61
            return ' - Syntax error, malformed JSON';
62
            break;
63
        case JSON_ERROR_UTF8:
64
            return ' - Malformed UTF-8 characters, possibly incorrectly encoded';
65
            break;
66
        default:
67
            return ' - Unknown error';
68
            break;
69
    }
70
71
    return false;
72
}
73
74
function loadAndFixJson($file)
75
{
76
    $result = [];
77
78
    $file = file($file);
79
80
    if (trim($file[count($file)-1]) == ']') {
81
        unset($file[count($file)-1]);
82
    }
83
84
    if (trim($file[0]) == '[') {
85
        unset($file[0]);
86
    }
87
88
    foreach ($file as $key => $line) {
89
        $line = removeLastComma(trim($line));
90
91
        if (isValidJson($line)) {
92
            $result[] = "$line";
93
        }
94
    }
95
96
    return '[ ' . implode(",\n", $result) . ']';
97
}
98
99
function removeLastComma($jsonData)
100
{
101
    $result = preg_replace("/(},)/", "}", $jsonData);
102
103
    return $result;
104
}
105
106
function dd($line)
107
{
108
    d($line);
109
    die;
110
}
111
112
function d($line)
113
{
114
    var_dump($line);
115
}
116
117
function isValidJson($string)
118
{
119
    $string = '[ ' . removeLastComma($string) . ']';
120
121
    return is_array(json_decode($string, true));
122
}
123