Json::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\Json;
2
3
use Arcanedev\Json\Contracts\Json as JsonContract;
4
use Illuminate\Contracts\Support\Arrayable;
5
use Illuminate\Contracts\Support\Jsonable;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Collection;
8
use JsonSerializable;
9
10
/**
11
 * Class     Json
12
 *
13
 * @package  Arcanedev\Support
14
 * @author   ARCANEDEV <[email protected]>
15
 */
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)
56
    {
57 57
        $this->setFilesystem($filesystem ?: new Filesystem);
58 57
        $this->loadFile($path);
59 57
    }
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)
74
    {
75 57
        $this->filesystem = $filesystem;
76
77 57
        return $this;
78
    }
79
80
    /**
81
     * Get path.
82
     *
83
     * @return string
84
     */
85 57
    public function getPath()
86
    {
87 57
        return $this->path;
88
    }
89
90
    /**
91
     * Set path.
92
     *
93
     * @param  mixed  $path
94
     *
95
     * @return static
96
     */
97 57
    public function setPath($path)
98
    {
99 57
        $this->path = (string) $path;
100
101 57
        return $this;
102
    }
103
104
    /**
105
     * Get file contents as Collection.
106
     *
107
     * @return \Illuminate\Support\Collection
108
     */
109 15
    public function attributes()
110
    {
111 15
        return $this->attributes;
112
    }
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)
177
    {
178 12
        return new static($path, $filesystem);
179
    }
180
181
    /**
182
     * Load json content.
183
     *
184
     * @param  string  $content
185
     *
186
     * @return static
187
     */
188 3
    public static function fromContent($content)
189
    {
190 3
        return static::make()->loadContent($content);
191
    }
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)
238
    {
239 3
        return $this->attributes->get($key, $default);
240
    }
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)
251
    {
252 3
        $this->attributes->put($key, $value);
253
254 3
        return $this;
255
    }
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)
265
    {
266 3
        $this->attributes = $this->attributes->merge($items);
267
268 3
        return $this;
269
    }
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)
280
    {
281 3
        return $this->merge($items)->save($path);
282
    }
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)
292
    {
293 6
        return $this->filesystem->put($path ?: $this->getPath(), $this->toJsonPretty());
294
    }
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)
322
    {
323 3
        return $this->get($key);
324
    }
325
326
    /**
327
     * Handle call to __toString method.
328
     *
329
     * @return string
330
     */
331 6
    public function __toString()
332
    {
333 6
        return $this->toJsonPretty();
334
    }
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)
344
    {
345 9
        return static::encode($data ?: $this->attributes, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
346
    }
347
348
    /**
349
     * Convert to array.
350
     *
351
     * @return array
352
     */
353 12
    public function toArray()
354
    {
355 12
        return $this->attributes()->toArray();
356
    }
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)
366
    {
367 3
        return static::encode($this->toArray(), $options);
368
    }
369
370
    /**
371
     * Allow to encode the json object to json file.
372
     *
373
     * @return array
374
     */
375 3
    public function jsonSerialize()
376
    {
377 3
        return $this->toArray();
378
    }
379
}
380