Yeelight /
miot-api
| 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 | * Use redis or file ? |
||||
| 17 | */ |
||||
| 18 | const USE_REDIS = true; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * Creating JSON file from data. |
||||
| 22 | * |
||||
| 23 | * @param string $data β JSON data |
||||
| 24 | * @param string $file β path to the file |
||||
| 25 | * |
||||
| 26 | * @throws JsonException |
||||
| 27 | * |
||||
| 28 | * @return array |
||||
| 29 | */ |
||||
| 30 | public static function dataToFile($data, $file) |
||||
| 31 | { |
||||
| 32 | $array = json_decode($data, true); |
||||
| 33 | |||||
| 34 | return self::arrayToFile($array, $file); |
||||
| 35 | } |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * Creating JSON file from array. |
||||
| 39 | * |
||||
| 40 | * @param array $array β array to be converted to JSON |
||||
| 41 | * @param string $file β path to the file |
||||
| 42 | * |
||||
| 43 | * @throws JsonException |
||||
| 44 | * |
||||
| 45 | * @return array |
||||
| 46 | */ |
||||
| 47 | public static function arrayToFile($array, $file) |
||||
| 48 | { |
||||
| 49 | $lastError = JsonLastError::check(); |
||||
| 50 | $json = json_encode($lastError ? $lastError : $array, JSON_PRETTY_PRINT); |
||||
| 51 | self::saveFile($file, $json); |
||||
| 52 | if (is_null($lastError)) { |
||||
| 53 | return $array; |
||||
| 54 | } else { |
||||
| 55 | throw new JsonException($lastError['message'].' '.$file); |
||||
| 56 | } |
||||
| 57 | } |
||||
| 58 | |||||
| 59 | /** |
||||
| 60 | * Create directory recursively if it doesn't exist. |
||||
| 61 | * |
||||
| 62 | * |
||||
| 63 | * @param string $file β path to the directory |
||||
| 64 | * |
||||
| 65 | * @throws JsonException β couldn't create directory |
||||
| 66 | */ |
||||
| 67 | private static function createDirectory($file) |
||||
| 68 | { |
||||
| 69 | $basename = is_string($file) ? basename($file) : ''; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 70 | $path = str_replace($basename, '', $file); |
||||
| 71 | if (!empty($path) && !is_dir($path)) { |
||||
| 72 | if (!mkdir($path, 0755, true)) { |
||||
| 73 | $message = 'Could not create directory in'; |
||||
| 74 | |||||
| 75 | throw new JsonException($message.' '.$path); |
||||
| 76 | } |
||||
| 77 | } |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Save file. |
||||
| 82 | * |
||||
| 83 | * |
||||
| 84 | * @param string $file β path to the file |
||||
| 85 | * @param string $json β json string |
||||
| 86 | * |
||||
| 87 | * @throws JsonException β couldn't create file |
||||
| 88 | */ |
||||
| 89 | private static function saveFile($file, $json) |
||||
| 90 | { |
||||
| 91 | if (self::putContent($file, $json) === false) { |
||||
| 92 | $message = 'Could not create file in'; |
||||
| 93 | |||||
| 94 | throw new JsonException($message.' '.$file); |
||||
| 95 | } |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Save to array the JSON file content. |
||||
| 100 | * |
||||
| 101 | * @param string $file β path or external url to JSON file |
||||
| 102 | * |
||||
| 103 | * @throws JsonException |
||||
| 104 | * |
||||
| 105 | * @return array|false |
||||
| 106 | */ |
||||
| 107 | public static function fileToArray($file) |
||||
| 108 | { |
||||
| 109 | $json = self::getContent($file); |
||||
| 110 | $array = json_decode($json, true); |
||||
| 111 | $lastError = JsonLastError::check(); |
||||
| 112 | |||||
| 113 | return $array === null || !is_null($lastError) ? false : $array; |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | private static function putContent($file, $json) |
||||
| 117 | { |
||||
| 118 | if (self::USE_REDIS) { |
||||
| 119 | if (\Redis::ping()) { |
||||
|
0 ignored issues
–
show
The method
Redis::ping() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 120 | $file = str_replace(Jsoner::getCacheDir(), 'miot_json_cache:', $file); |
||||
| 121 | $file = str_replace(['/', '\\'], ':', $file); |
||||
| 122 | |||||
| 123 | return \Redis::set($file, $json); |
||||
|
0 ignored issues
–
show
The method
Redis::set() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 124 | } else { |
||||
| 125 | self::createDirectory($file); |
||||
| 126 | |||||
| 127 | return file_put_contents($file, $json); |
||||
| 128 | } |
||||
| 129 | } |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | private static function getContent($file) |
||||
| 133 | { |
||||
| 134 | if (self::USE_REDIS) { |
||||
| 135 | if (\Redis::ping()) { |
||||
|
0 ignored issues
–
show
The method
Redis::ping() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 136 | $file = str_replace(Jsoner::getCacheDir(), 'miot_json_cache:', $file); |
||||
| 137 | $file = str_replace(['/', '\\'], ':', $file); |
||||
| 138 | |||||
| 139 | return \Redis::get($file); |
||||
|
0 ignored issues
–
show
The method
Redis::get() is not static, but was called statically.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 140 | } else { |
||||
| 141 | if (!is_file($file) && !filter_var($file, FILTER_VALIDATE_URL)) { |
||||
| 142 | self::arrayToFile([], $file); |
||||
| 143 | } |
||||
| 144 | |||||
| 145 | return file_get_contents($file); |
||||
| 146 | } |
||||
| 147 | } |
||||
| 148 | } |
||||
| 149 | } |
||||
| 150 |