Passed
Push — master ( 82e9fe...cecb4c )
by Sheldon
04:44
created

JsonLoader::arrayToFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: sheldon
5
 * Date: 18-6-12
6
 * Time: 下午4:08.
7
 */
8
9
namespace MiotApi\Util\Jsoner;
10
11
use MiotApi\Exception\JsonException;
12
13
class JsonLoader
14
{
15
    /**
16
     * Creating JSON file from data.
17
     *
18
     * @param string $data → JSON data
19
     * @param string $file → path to the file
20
     *
21
     * @throws JsonException
22
     *
23
     * @return array
24
     */
25
    public static function dataToFile($data, $file)
26
    {
27
        $array = json_decode($data, true);
28
29
        return self::arrayToFile($array, $file);
30
    }
31
32
    /**
33
     * Creating JSON file from array.
34
     *
35
     * @param array  $array → array to be converted to JSON
36
     * @param string $file  → path to the file
37
     *
38
     * @throws JsonException
39
     *
40
     * @return array
41
     */
42
    public static function arrayToFile($array, $file)
43
    {
44
        self::createDirectory($file);
45
        $lastError = JsonLastError::check();
46
        $json = json_encode($lastError ? $lastError : $array, JSON_PRETTY_PRINT);
47
        self::saveFile($file, $json);
48
        if (is_null($lastError)) {
49
            return $array;
50
        } else {
51
            throw new JsonException($lastError['message'] . ' ' . $file);
52
        }
53
    }
54
55
    /**
56
     * Create directory recursively if it doesn't exist.
57
     *
58
     *
59
     * @param string $file → path to the directory
60
     *
61
     * @throws JsonException → couldn't create directory
62
     */
63
    private static function createDirectory($file)
64
    {
65
        $basename = is_string($file) ? basename($file) : '';
0 ignored issues
show
introduced by
The condition is_string($file) is always true.
Loading history...
66
        $path = str_replace($basename, '', $file);
67
        if (!empty($path) && !is_dir($path)) {
68
            if (!mkdir($path, 0755, true)) {
69
                $message = 'Could not create directory in';
70
71
                throw new JsonException($message . ' ' . $path);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Save file.
78
     *
79
     *
80
     * @param string $file → path to the file
81
     * @param string $json → json string
82
     *
83
     * @throws JsonException → couldn't create file
84
     */
85
    private static function saveFile($file, $json)
86
    {
87
        if (file_put_contents($file, $json) === false) {
88
            $message = 'Could not create file in';
89
90
            throw new JsonException($message . ' ' . $file);
91
        }
92
    }
93
94
    /**
95
     * Save to array the JSON file content.
96
     *
97
     * @param string $file → path or external url to JSON file
98
     *
99
     * @throws JsonException
100
     *
101
     * @return array|false
102
     */
103
    public static function fileToArray($file)
104
    {
105
        if (!is_file($file) && !filter_var($file, FILTER_VALIDATE_URL)) {
106
            self::arrayToFile([], $file);
107
        }
108
        $json = file_get_contents($file);
109
        $array = json_decode($json, true);
110
        $lastError = JsonLastError::check();
111
112
        return $array === null || !is_null($lastError) ? false : $array;
113
    }
114
}
115