|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | /** |
4 | 6 | * PHP simple library for managing JSON files. |
5 | 7 | * |
|
12 | 14 |
|
13 | 15 | namespace Josantonius\Json; |
14 | 16 |
|
15 | | -use Josantonius\Json\Exception\JsonException; |
| 17 | +use Josantonius\Json\Exception\CreateDirectoryException; |
| 18 | +use Josantonius\Json\Exception\CreateFileException; |
| 19 | +use Josantonius\Json\Exception\GetFileException; |
| 20 | +use Josantonius\Json\Exception\JsonErrorException; |
| 21 | +use Josantonius\Json\Exception\UnavailableMethodException; |
16 | 22 |
|
17 | | -/** |
18 | | - * Json handler. |
19 | | - */ |
20 | 23 | class Json |
21 | 24 | { |
22 | 25 | /** |
23 | | - * Creating JSON file from array. |
| 26 | + * If the file path is a URL. |
24 | 27 | * |
25 | | - * @param array $array → array to be converted to JSON |
26 | | - * @param string $file → path to the file |
| 28 | + * @since 2.0.0 |
| 29 | + */ |
| 30 | + private bool $isUrl; |
| 31 | + |
| 32 | + /** |
| 33 | + * @since 2.0.0 |
27 | 34 | * |
28 | | - * @return boolean → true if the file is created without errors |
| 35 | + * @throws CreateDirectoryException |
| 36 | + * @throws CreateFileException |
| 37 | + * @throws JsonErrorException |
29 | 38 | */ |
30 | | - public static function arrayToFile($array, $file) |
| 39 | + public function __construct(private string $filepath) |
31 | 40 | { |
32 | | - self::createDirectory($file); |
| 41 | + $this->isUrl = filter_var($filepath, FILTER_VALIDATE_URL) !== false; |
33 | 42 |
|
34 | | - $lastError = JsonLastError::check(); |
| 43 | + if (!$this->isUrl) { |
| 44 | + $this->createFileIfNotExists($filepath); |
| 45 | + } |
| 46 | + } |
35 | 47 |
|
36 | | - $json = json_encode($lastError ? $lastError : $array, JSON_PRETTY_PRINT); |
| 48 | + /** |
| 49 | + * Get the content of the JSON file or a remote JSON file. |
| 50 | + * |
| 51 | + * @since 2.0.0 |
| 52 | + * |
| 53 | + * @throws GetFileException |
| 54 | + * @throws JsonErrorException |
| 55 | + */ |
| 56 | + public function get(): array |
| 57 | + { |
| 58 | + return $this->getFileContents(); |
| 59 | + } |
37 | 60 |
|
38 | | - self::saveFile($file, $json); |
| 61 | + /** |
| 62 | + * Set the content of the JSON file. |
| 63 | + * |
| 64 | + * @since 2.0.0 |
| 65 | + * |
| 66 | + * @throws CreateFileException |
| 67 | + * @throws JsonErrorException |
| 68 | + * @throws UnavailableMethodException |
| 69 | + */ |
| 70 | + public function set(array|object $content): void |
| 71 | + { |
| 72 | + $this->isUrl ? $this->throwUnavailableMethodException() : $this->saveToJsonFile($content); |
| 73 | + } |
39 | 74 |
|
40 | | - return is_null($lastError); |
| 75 | + /** |
| 76 | + * Merge into JSON file. |
| 77 | + * |
| 78 | + * @since 2.0.0 |
| 79 | + * |
| 80 | + * @throws CreateFileException |
| 81 | + * @throws GetFileException |
| 82 | + * @throws JsonErrorException |
| 83 | + * @throws UnavailableMethodException |
| 84 | + */ |
| 85 | + public function merge(array|object $content): array |
| 86 | + { |
| 87 | + $content = array_merge($this->getFileContents(), (array) $content); |
| 88 | + |
| 89 | + $this->isUrl ? $this->throwUnavailableMethodException() : $this->saveToJsonFile($content); |
| 90 | + |
| 91 | + return $content; |
41 | 92 | } |
42 | 93 |
|
43 | 94 | /** |
44 | | - * Save to array the JSON file content. |
| 95 | + * Push on the JSON file. |
45 | 96 | * |
46 | | - * @param string $file → path or external url to JSON file |
| 97 | + * @since 2.0.0 |
47 | 98 | * |
48 | | - * @return array|false |
| 99 | + * @throws CreateFileException |
| 100 | + * @throws GetFileException |
| 101 | + * @throws JsonErrorException |
| 102 | + * @throws UnavailableMethodException |
49 | 103 | */ |
50 | | - public static function fileToArray($file) |
| 104 | + public function push(array|object $content): array |
51 | 105 | { |
52 | | - if (!is_file($file) && !filter_var($file, FILTER_VALIDATE_URL)) { |
53 | | - self::arrayToFile([], $file); |
54 | | - } |
| 106 | + $data = $this->getFileContents(); |
| 107 | + |
| 108 | + array_push($data, $content); |
| 109 | + |
| 110 | + $this->isUrl ? $this->throwUnavailableMethodException() : $this->saveToJsonFile($data); |
| 111 | + |
| 112 | + return $data; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Create JSON file from array. |
| 117 | + * |
| 118 | + * @deprecated |
| 119 | + * |
| 120 | + * @throws CreateFileException |
| 121 | + * @throws JsonErrorException |
| 122 | + * @throws UnavailableMethodException |
| 123 | + */ |
| 124 | + public static function arrayToFile(array|object $array, string $file): bool |
| 125 | + { |
| 126 | + $message = 'The "arrayToFile" method is deprecated and will be removed. Use "set" instead.'; |
| 127 | + $url = 'More information at: https://github.com/josantonius/php-json.'; |
| 128 | + trigger_error($message . ' ' . $url, E_USER_DEPRECATED); |
| 129 | + |
| 130 | + (new Json($file))->set($array); |
| 131 | + |
| 132 | + return true; |
| 133 | + } |
55 | 134 |
|
56 | | - $json = @file_get_contents($file); |
57 | | - $array = json_decode($json, true); |
58 | | - $lastError = JsonLastError::check(); |
| 135 | + /** |
| 136 | + * Get the content of the JSON file or a remote JSON file. |
| 137 | + * |
| 138 | + * @deprecated |
| 139 | + * |
| 140 | + * @throws GetFileException |
| 141 | + * @throws JsonErrorException |
| 142 | + */ |
| 143 | + public static function fileToArray($file): array |
| 144 | + { |
| 145 | + $message = 'The "fileToArray" method is deprecated and will be removed. Use "get" instead.'; |
| 146 | + $url = 'More information at: https://github.com/josantonius/php-json.'; |
| 147 | + trigger_error($message . ' ' . $url, E_USER_DEPRECATED); |
59 | 148 |
|
60 | | - return $array === null || !is_null($lastError) ? false : $array; |
| 149 | + return (new Json($file))->get(); |
61 | 150 | } |
62 | 151 |
|
63 | 152 | /** |
64 | | - * Create directory recursively if it doesn't exist. |
| 153 | + * Create file if not exists. |
65 | 154 | * |
66 | | - * @since 1.1.3 |
| 155 | + * @since 2.0.0 |
67 | 156 | * |
68 | | - * @param string $file → path to the directory |
| 157 | + * @throws CreateDirectoryException |
| 158 | + * @throws CreateFileException |
| 159 | + * @throws JsonErrorException |
| 160 | + */ |
| 161 | + private function createFileIfNotExists(): void |
| 162 | + { |
| 163 | + if (!file_exists($this->filepath)) { |
| 164 | + $this->createDirIfNotExists(); |
| 165 | + $this->saveToJsonFile([]); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Create directory if not exists. |
69 | 171 | * |
70 | | - * @throws JsonException → couldn't create directory |
| 172 | + * @since 2.0.0 |
| 173 | + * |
| 174 | + * @throws CreateDirectoryException |
71 | 175 | */ |
72 | | - private static function createDirectory($file) |
| 176 | + private function createDirIfNotExists(): void |
73 | 177 | { |
74 | | - $basename = is_string($file) ? basename($file) : ''; |
75 | | - $path = str_replace($basename, '', $file); |
76 | | - |
77 | | - if (!empty($path) && !is_dir($path)) { |
78 | | - if (!mkdir($path, 0755, true)) { |
79 | | - $message = 'Could not create directory in'; |
80 | | - throw new JsonException($message . ' ' . $path); |
81 | | - } |
| 178 | + $path = dirname($this->filepath) . DIRECTORY_SEPARATOR; |
| 179 | + |
| 180 | + if (!is_dir($path) && !@mkdir($path, 0777, true)) { |
| 181 | + throw new CreateDirectoryException($path); |
82 | 182 | } |
83 | 183 | } |
84 | 184 |
|
85 | 185 | /** |
86 | | - * Save file. |
| 186 | + * Get the content of the JSON file or a remote JSON file. |
87 | 187 | * |
88 | | - * @since 1.1.3 |
| 188 | + * @since 2.0.0 |
89 | 189 | * |
90 | | - * @param string $file → path to the file |
91 | | - * @param string $json → JSON string |
| 190 | + * @throws GetFileException |
| 191 | + * @throws JsonErrorException |
| 192 | + */ |
| 193 | + private function getFileContents(): array |
| 194 | + { |
| 195 | + $json = @file_get_contents($this->filepath); |
| 196 | + |
| 197 | + if ($json === false) { |
| 198 | + throw new GetFileException($this->filepath); |
| 199 | + } |
| 200 | + |
| 201 | + $array = json_decode($json, true); |
| 202 | + |
| 203 | + $this->checkJsonLastError(); |
| 204 | + |
| 205 | + return $array; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Save content in JSON file. |
| 210 | + * |
| 211 | + * @since 2.0.0 |
| 212 | + * |
| 213 | + * @throws CreateFileException |
| 214 | + * @throws JsonErrorException |
| 215 | + */ |
| 216 | + private function saveToJsonFile(array|object $array): void |
| 217 | + { |
| 218 | + $json = json_encode($array, JSON_PRETTY_PRINT); |
| 219 | + |
| 220 | + $this->checkJsonLastError(); |
| 221 | + |
| 222 | + if (@file_put_contents($this->filepath, $json) === false) { |
| 223 | + throw new CreateFileException($this->filepath); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Check for JSON errors. |
| 229 | + * |
| 230 | + * @since 2.0.0 |
92 | 231 | * |
93 | | - * @throws JsonException → couldn't create file |
| 232 | + * @throws JsonErrorException |
94 | 233 | */ |
95 | | - private static function saveFile($file, $json) |
| 234 | + private function checkJsonLastError(): void |
96 | 235 | { |
97 | | - if (@file_put_contents($file, $json) === false) { |
98 | | - $message = 'Could not create file in'; |
99 | | - throw new JsonException($message . ' ' . $file); |
| 236 | + if (json_last_error()) { |
| 237 | + throw new JsonErrorException(); |
100 | 238 | } |
101 | 239 | } |
| 240 | + |
| 241 | + /** |
| 242 | + * Throw exception if the method is not available for remote JSON files. |
| 243 | + * |
| 244 | + * @since 2.0.0 |
| 245 | + * |
| 246 | + * @throws UnavailableMethodException |
| 247 | + */ |
| 248 | + private function throwUnavailableMethodException(): void |
| 249 | + { |
| 250 | + $method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; |
| 251 | + |
| 252 | + throw new UnavailableMethodException($method); |
| 253 | + } |
102 | 254 | } |
0 commit comments