1 | <?php namespace Arcanedev\Json; |
||
16 | class Json implements JsonContract, Arrayable, Jsonable, JsonSerializable |
||
17 | { |
||
18 | /* ----------------------------------------------------------------- |
||
19 | | Properties |
||
20 | | ----------------------------------------------------------------- |
||
21 | */ |
||
22 | |||
23 | /** |
||
24 | * The file path. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $path; |
||
29 | |||
30 | /** |
||
31 | * The laravel filesystem instance. |
||
32 | * |
||
33 | * @var \Illuminate\Filesystem\Filesystem |
||
34 | */ |
||
35 | protected $filesystem; |
||
36 | |||
37 | /** |
||
38 | * The attributes collection. |
||
39 | * |
||
40 | * @var \Illuminate\Support\Collection |
||
41 | */ |
||
42 | protected $attributes; |
||
43 | |||
44 | /* ----------------------------------------------------------------- |
||
45 | | Constructor |
||
46 | | ----------------------------------------------------------------- |
||
47 | */ |
||
48 | |||
49 | /** |
||
50 | * Construct the Json instance. |
||
51 | * |
||
52 | * @param string $path |
||
53 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
54 | */ |
||
55 | 57 | public function __construct($path, Filesystem $filesystem = null) |
|
60 | |||
61 | /* ----------------------------------------------------------------- |
||
62 | | Getters & Setters |
||
63 | | ----------------------------------------------------------------- |
||
64 | */ |
||
65 | |||
66 | /** |
||
67 | * Set the filesystem instance. |
||
68 | * |
||
69 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
70 | * |
||
71 | * @return static |
||
72 | */ |
||
73 | 57 | public function setFilesystem(Filesystem $filesystem) |
|
79 | |||
80 | /** |
||
81 | * Get path. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 57 | public function getPath() |
|
89 | |||
90 | /** |
||
91 | * Set path. |
||
92 | * |
||
93 | * @param mixed $path |
||
94 | * |
||
95 | * @return static |
||
96 | */ |
||
97 | 57 | public function setPath($path) |
|
103 | |||
104 | /** |
||
105 | * Get file contents as Collection. |
||
106 | * |
||
107 | * @return \Illuminate\Support\Collection |
||
108 | */ |
||
109 | 15 | public function attributes() |
|
113 | |||
114 | /* ----------------------------------------------------------------- |
||
115 | | Main Methods |
||
116 | | ----------------------------------------------------------------- |
||
117 | */ |
||
118 | |||
119 | /** |
||
120 | * Decode ths json content. |
||
121 | * |
||
122 | * @param string $content |
||
123 | * @param bool $assoc |
||
124 | * @param int $depth |
||
125 | * @param int $options |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | 57 | public static function decode($content, $assoc = true, $options = 0, $depth = 512) |
|
130 | { |
||
131 | 57 | $decoded = json_decode($content, $assoc, $depth, $options); |
|
132 | |||
133 | // Check if json decode has errors |
||
134 | 57 | if (($lastError = json_last_error()) !== JSON_ERROR_NONE) |
|
135 | 15 | JsonErrorHandler::handleDecodeErrors($lastError, $options, [ |
|
136 | 15 | 'depth' => $depth, |
|
137 | 15 | 'last_error_msg' => json_last_error_msg(), |
|
138 | 15 | 'last_error_number' => $lastError, |
|
139 | ]); |
||
140 | |||
141 | 57 | return $decoded; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Encode to json content. |
||
146 | * |
||
147 | * @param mixed $content |
||
148 | * @param int $options |
||
149 | * @param int $depth |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 21 | public function encode($content, $options = 0, $depth = 512) |
|
154 | { |
||
155 | 21 | $encoded = json_encode($content, $options, $depth); |
|
156 | |||
157 | // Check if json encode has errors |
||
158 | 21 | if (($lastError = json_last_error()) !== JSON_ERROR_NONE) |
|
159 | 12 | JsonErrorHandler::handleEncodeErrors($lastError, $options, [ |
|
160 | 12 | 'depth' => $depth, |
|
161 | 12 | 'last_error_msg' => json_last_error_msg(), |
|
162 | 12 | 'last_error_number' => $lastError, |
|
163 | ]); |
||
164 | |||
165 | 9 | return $encoded; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Make new instance. |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
173 | * |
||
174 | * @return static |
||
175 | */ |
||
176 | 12 | public static function make($path = null, Filesystem $filesystem = null) |
|
180 | |||
181 | /** |
||
182 | * Load json content. |
||
183 | * |
||
184 | * @param string $content |
||
185 | * |
||
186 | * @return static |
||
187 | */ |
||
188 | 3 | public static function fromContent($content) |
|
192 | |||
193 | /** |
||
194 | * Load the json file. |
||
195 | * |
||
196 | * @param string $path |
||
197 | * |
||
198 | * @return static |
||
199 | */ |
||
200 | 57 | public function loadFile($path) |
|
201 | { |
||
202 | 57 | $content = '{}'; |
|
203 | |||
204 | 57 | if ( ! is_null($path)) { |
|
205 | 57 | $this->setPath($path); |
|
206 | |||
207 | 57 | $content = $this->filesystem->get($this->getPath()); |
|
208 | } |
||
209 | |||
210 | 57 | return $this->loadContent($content); |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Load the json content. |
||
215 | * |
||
216 | * @param string $content |
||
217 | * |
||
218 | * @return static |
||
219 | */ |
||
220 | 57 | public function loadContent($content) |
|
221 | { |
||
222 | 57 | $this->attributes = Collection::make( |
|
223 | 57 | $this->decode($content) |
|
224 | ); |
||
225 | |||
226 | 57 | return $this; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get the specified attribute from json file. |
||
231 | * |
||
232 | * @param string $key |
||
233 | * @param mixed|null $default |
||
234 | * |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 3 | public function get($key, $default = null) |
|
241 | |||
242 | /** |
||
243 | * Set a specific key & value. |
||
244 | * |
||
245 | * @param string $key |
||
246 | * @param mixed $value |
||
247 | * |
||
248 | * @return static |
||
249 | */ |
||
250 | 3 | public function set($key, $value) |
|
256 | |||
257 | /** |
||
258 | * Merge json attributes with a given array items. |
||
259 | * |
||
260 | * @param array $items |
||
261 | * |
||
262 | * @return static |
||
263 | */ |
||
264 | 3 | public function merge(array $items) |
|
270 | |||
271 | /** |
||
272 | * Update & save json contents from array items. |
||
273 | * |
||
274 | * @param array $items |
||
275 | * @param string $path |
||
276 | * |
||
277 | * @return int |
||
278 | */ |
||
279 | 3 | public function update(array $items, $path = null) |
|
283 | |||
284 | /** |
||
285 | * Save the current attributes array to the file storage. |
||
286 | * |
||
287 | * @param string|null $path |
||
288 | * |
||
289 | * @return int |
||
290 | */ |
||
291 | 6 | public function save($path = null) |
|
295 | |||
296 | /** |
||
297 | * Handle call to __call method. |
||
298 | * |
||
299 | * @param string $method |
||
300 | * @param array $arguments |
||
301 | * |
||
302 | * @return mixed |
||
303 | */ |
||
304 | 3 | public function __call($method, $arguments = []) |
|
305 | { |
||
306 | 3 | if ( ! method_exists($this, $method)) { |
|
307 | 3 | $arguments = [$method]; |
|
308 | 3 | $method = 'get'; |
|
309 | } |
||
310 | |||
311 | 3 | return call_user_func_array([$this, $method], $arguments); |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Handle magic method __get. |
||
316 | * |
||
317 | * @param string $key |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | 3 | public function __get($key) |
|
325 | |||
326 | /** |
||
327 | * Handle call to __toString method. |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | 6 | public function __toString() |
|
335 | |||
336 | /** |
||
337 | * Convert the given array data to pretty json. |
||
338 | * |
||
339 | * @param array $data |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 9 | public function toJsonPretty(array $data = null) |
|
347 | |||
348 | /** |
||
349 | * Convert to array. |
||
350 | * |
||
351 | * @return array |
||
352 | */ |
||
353 | 12 | public function toArray() |
|
357 | |||
358 | /** |
||
359 | * Convert the object to its JSON representation. |
||
360 | * |
||
361 | * @param int $options |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | 3 | public function toJson($options = 0) |
|
369 | |||
370 | /** |
||
371 | * Allow to encode the json object to json file. |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | 3 | public function jsonSerialize() |
|
379 | } |
||
380 |