1 | <?php namespace Arcanedev\Json; |
||
16 | class Json implements JsonContract, Arrayable, Jsonable, JsonSerializable |
||
17 | { |
||
18 | /* ------------------------------------------------------------------------------------------------ |
||
19 | | Properties |
||
20 | | ------------------------------------------------------------------------------------------------ |
||
21 | */ |
||
22 | /** |
||
23 | * The file path. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $path; |
||
28 | |||
29 | /** |
||
30 | * The laravel filesystem instance. |
||
31 | * |
||
32 | * @var \Illuminate\Filesystem\Filesystem |
||
33 | */ |
||
34 | protected $filesystem; |
||
35 | |||
36 | /** |
||
37 | * The attributes collection. |
||
38 | * |
||
39 | * @var \Illuminate\Support\Collection |
||
40 | */ |
||
41 | protected $attributes; |
||
42 | |||
43 | /* ------------------------------------------------------------------------------------------------ |
||
44 | | Constructor |
||
45 | | ------------------------------------------------------------------------------------------------ |
||
46 | */ |
||
47 | /** |
||
48 | * Construct the Json instance. |
||
49 | * |
||
50 | * @param string $path |
||
51 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
52 | */ |
||
53 | 54 | public function __construct($path, Filesystem $filesystem = null) |
|
58 | |||
59 | /** |
||
60 | * Load the json file. |
||
61 | * |
||
62 | * @param string $path |
||
63 | * |
||
64 | * @return self |
||
65 | */ |
||
66 | 54 | public function loadFile($path) |
|
78 | |||
79 | /** |
||
80 | * Load the json content. |
||
81 | * |
||
82 | * @param string $content |
||
83 | * |
||
84 | * @return self |
||
85 | */ |
||
86 | 54 | public function loadContent($content) |
|
94 | |||
95 | /* ------------------------------------------------------------------------------------------------ |
||
96 | | Getters & Setters |
||
97 | | ------------------------------------------------------------------------------------------------ |
||
98 | */ |
||
99 | /** |
||
100 | * Set the filesystem instance. |
||
101 | * |
||
102 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
103 | * |
||
104 | * @return self |
||
105 | */ |
||
106 | 54 | public function setFilesystem(Filesystem $filesystem) |
|
112 | |||
113 | /** |
||
114 | * Get path. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 54 | public function getPath() |
|
122 | |||
123 | /** |
||
124 | * Set path. |
||
125 | * |
||
126 | * @param mixed $path |
||
127 | * |
||
128 | * @return self |
||
129 | */ |
||
130 | 54 | public function setPath($path) |
|
136 | |||
137 | /** |
||
138 | * Get file contents as Collection. |
||
139 | * |
||
140 | * @return \Illuminate\Support\Collection |
||
141 | */ |
||
142 | 12 | public function attributes() |
|
146 | |||
147 | /* ------------------------------------------------------------------------------------------------ |
||
148 | | Main Functions |
||
149 | | ------------------------------------------------------------------------------------------------ |
||
150 | */ |
||
151 | /** |
||
152 | * Decode ths json content. |
||
153 | * |
||
154 | * @param string $content |
||
155 | * @param bool $assoc |
||
156 | * @param int $depth |
||
157 | * @param int $options |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | 54 | public static function decode($content, $assoc = true, $options = 0, $depth = 512) |
|
175 | |||
176 | /** |
||
177 | * Encode to json content. |
||
178 | * |
||
179 | * @param mixed $content |
||
180 | * @param int $options |
||
181 | * @param int $depth |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | 21 | public function encode($content, $options = 0, $depth = 512) |
|
199 | |||
200 | /** |
||
201 | * Make new instance. |
||
202 | * |
||
203 | * @param string $path |
||
204 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||
205 | * |
||
206 | * @return static |
||
207 | */ |
||
208 | 9 | public static function make($path = null, Filesystem $filesystem = null) |
|
212 | |||
213 | /** |
||
214 | * Load json content. |
||
215 | * |
||
216 | * @param string $content |
||
217 | * |
||
218 | * @return static |
||
219 | */ |
||
220 | 3 | public static function fromContent($content) |
|
224 | |||
225 | /** |
||
226 | * Get the specified attribute from json file. |
||
227 | * |
||
228 | * @param string $key |
||
229 | * @param mixed|null $default |
||
230 | * |
||
231 | * @return mixed |
||
232 | */ |
||
233 | 3 | public function get($key, $default = null) |
|
237 | |||
238 | /** |
||
239 | * Set a specific key & value. |
||
240 | * |
||
241 | * @param string $key |
||
242 | * @param mixed $value |
||
243 | * |
||
244 | * @return self |
||
245 | */ |
||
246 | 3 | public function set($key, $value) |
|
252 | |||
253 | /** |
||
254 | * Merge json attributes with a given array items. |
||
255 | * |
||
256 | * @param array $items |
||
257 | * |
||
258 | * @return self |
||
259 | */ |
||
260 | 3 | public function merge(array $items) |
|
266 | |||
267 | /** |
||
268 | * Update & save json contents from array items. |
||
269 | * |
||
270 | * @param array $items |
||
271 | * @param string $path |
||
272 | * |
||
273 | * @return int |
||
274 | */ |
||
275 | 3 | public function update(array $items, $path = null) |
|
279 | |||
280 | /** |
||
281 | * Save the current attributes array to the file storage. |
||
282 | * |
||
283 | * @param string|null $path |
||
284 | * |
||
285 | * @return int |
||
286 | */ |
||
287 | 6 | public function save($path = null) |
|
291 | |||
292 | /** |
||
293 | * Handle call to __call method. |
||
294 | * |
||
295 | * @param string $method |
||
296 | * @param array $arguments |
||
297 | * |
||
298 | * @return mixed |
||
299 | */ |
||
300 | 3 | public function __call($method, $arguments = []) |
|
306 | |||
307 | /** |
||
308 | * Handle magic method __get. |
||
309 | * |
||
310 | * @param string $key |
||
311 | * |
||
312 | * @return mixed |
||
313 | */ |
||
314 | 3 | public function __get($key) |
|
318 | |||
319 | /** |
||
320 | * Handle call to __toString method. |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | 6 | public function __toString() |
|
328 | |||
329 | /** |
||
330 | * Convert the given array data to pretty json. |
||
331 | * |
||
332 | * @param array $data |
||
333 | * |
||
334 | * @return string |
||
335 | */ |
||
336 | 9 | public function toJsonPretty(array $data = null) |
|
340 | |||
341 | /** |
||
342 | * Convert to array. |
||
343 | * |
||
344 | * @return array |
||
345 | */ |
||
346 | 9 | public function toArray() |
|
350 | |||
351 | /** |
||
352 | * Convert the object to its JSON representation. |
||
353 | * |
||
354 | * @param int $options |
||
355 | * |
||
356 | * @return string |
||
357 | */ |
||
358 | 3 | public function toJson($options = 0) |
|
362 | |||
363 | /** |
||
364 | * Allow to encode the json object to json file. |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | 3 | public function jsonSerialize() |
|
372 | } |
||
373 |