Skip to content

Commit f7e4d29

Browse files
committed
feat: add new features and refactorings
Add new features and refactor the code. #15
1 parent e6d450f commit f7e4d29

File tree

1 file changed

+199
-47
lines changed

1 file changed

+199
-47
lines changed

src/Json.php

Lines changed: 199 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* PHP simple library for managing JSON files.
57
*
@@ -12,91 +14,241 @@
1214

1315
namespace Josantonius\Json;
1416

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;
1622

17-
/**
18-
* Json handler.
19-
*/
2023
class Json
2124
{
2225
/**
23-
* Creating JSON file from array.
26+
* If the file path is a URL.
2427
*
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
2734
*
28-
* @return boolean → true if the file is created without errors
35+
* @throws CreateDirectoryException
36+
* @throws CreateFileException
37+
* @throws JsonErrorException
2938
*/
30-
public static function arrayToFile($array, $file)
39+
public function __construct(private string $filepath)
3140
{
32-
self::createDirectory($file);
41+
$this->isUrl = filter_var($filepath, FILTER_VALIDATE_URL) !== false;
3342

34-
$lastError = JsonLastError::check();
43+
if (!$this->isUrl) {
44+
$this->createFileIfNotExists($filepath);
45+
}
46+
}
3547

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+
}
3760

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+
}
3974

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;
4192
}
4293

4394
/**
44-
* Save to array the JSON file content.
95+
* Push on the JSON file.
4596
*
46-
* @param string $file → path or external url to JSON file
97+
* @since 2.0.0
4798
*
48-
* @return array|false
99+
* @throws CreateFileException
100+
* @throws GetFileException
101+
* @throws JsonErrorException
102+
* @throws UnavailableMethodException
49103
*/
50-
public static function fileToArray($file)
104+
public function push(array|object $content): array
51105
{
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+
}
55134

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);
59148

60-
return $array === null || !is_null($lastError) ? false : $array;
149+
return (new Json($file))->get();
61150
}
62151

63152
/**
64-
* Create directory recursively if it doesn't exist.
153+
* Create file if not exists.
65154
*
66-
* @since 1.1.3
155+
* @since 2.0.0
67156
*
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.
69171
*
70-
* @throws JsonException → couldn't create directory
172+
* @since 2.0.0
173+
*
174+
* @throws CreateDirectoryException
71175
*/
72-
private static function createDirectory($file)
176+
private function createDirIfNotExists(): void
73177
{
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);
82182
}
83183
}
84184

85185
/**
86-
* Save file.
186+
* Get the content of the JSON file or a remote JSON file.
87187
*
88-
* @since 1.1.3
188+
* @since 2.0.0
89189
*
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
92231
*
93-
* @throws JsonException → couldn't create file
232+
* @throws JsonErrorException
94233
*/
95-
private static function saveFile($file, $json)
234+
private function checkJsonLastError(): void
96235
{
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();
100238
}
101239
}
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+
}
102254
}

0 commit comments

Comments
 (0)