Completed
Push — master ( a6c0da...a5dd95 )
by ARCANEDEV
10s
created

Json::getFilesystem()   A

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
     * 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)
54
    {
55 54
        $this->setFilesystem($filesystem ?: new Filesystem);
56 54
        $this->loadFile($path);
57 54
    }
58
59
    /**
60
     * Load the json file.
61
     *
62
     * @param  string  $path
63
     *
64
     * @return self
65
     */
66 54
    public function loadFile($path)
67
    {
68 54
        $content = '{}';
69
70 54
        if ( ! is_null($path)) {
71 54
            $this->setPath($path);
72
73 54
            $content = $this->filesystem->get($this->getPath());
74 18
        }
75
76 54
        return $this->loadContent($content);
77
    }
78
79
    /**
80
     * Load the json content.
81
     *
82
     * @param  string  $content
83
     *
84
     * @return self
85
     */
86 54
    public function loadContent($content)
87
    {
88 54
        $this->attributes = Collection::make(
89 54
            $this->decode($content)
90 18
        );
91
92 54
        return $this;
93
    }
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)
107
    {
108 54
        $this->filesystem = $filesystem;
109
110 54
        return $this;
111
    }
112
113
    /**
114
     * Get path.
115
     *
116
     * @return string
117
     */
118 54
    public function getPath()
119
    {
120 54
        return $this->path;
121
    }
122
123
    /**
124
     * Set path.
125
     *
126
     * @param  mixed  $path
127
     *
128
     * @return self
129
     */
130 54
    public function setPath($path)
131
    {
132 54
        $this->path = (string) $path;
133
134 54
        return $this;
135
    }
136
137
    /**
138
     * Get file contents as Collection.
139
     *
140
     * @return \Illuminate\Support\Collection
141
     */
142 12
    public function attributes()
143
    {
144 12
        return $this->attributes;
145
    }
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)
162
    {
163 54
        $decoded = json_decode($content, $assoc, $depth, $options);
164
165
        // Check if json decode has errors
166 54
        if (($lastError = json_last_error()) !== JSON_ERROR_NONE)
167 28
            JsonErrorHandler::handleDecodeErrors($lastError, $options, [
168 15
                'depth'             => $depth,
169 15
                'last_error_msg'    => json_last_error_msg(),
170 15
                'last_error_number' => $lastError,
171 5
            ]);
172
173 54
        return $decoded;
174
    }
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)
186
    {
187 21
        $encoded = json_encode($content, $options, $depth);
188
189
        // Check if json encode has errors
190 21
        if (($lastError = json_last_error()) !== JSON_ERROR_NONE)
191 15
            JsonErrorHandler::handleEncodeErrors($lastError, $options, [
192 12
                'depth'             => $depth,
193 12
                'last_error_msg'    => json_last_error_msg(),
194 12
                'last_error_number' => $lastError,
195 4
            ]);
196
197 9
        return $encoded;
198
    }
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)
209
    {
210 9
        return new static($path, $filesystem);
211
    }
212
213
    /**
214
     * Load json content.
215
     *
216
     * @param  string  $content
217
     *
218
     * @return static
219
     */
220 3
    public static function fromContent($content)
221
    {
222 3
        return static::make()->loadContent($content);
223
    }
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)
234
    {
235 3
        return $this->attributes->get($key, $default);
236
    }
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)
247
    {
248 3
        $this->attributes->put($key, $value);
249
250 3
        return $this;
251
    }
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)
261
    {
262 3
        $this->attributes = $this->attributes->merge($items);
263
264 3
        return $this;
265
    }
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)
276
    {
277 3
        return $this->merge($items)->save($path);
278
    }
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)
288
    {
289 6
        return $this->filesystem->put($path ?: $this->getPath(), $this->toJsonPretty());
290
    }
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 = [])
301
    {
302 3
        return method_exists($this, $method)
303 1
            ? call_user_func_array([$this, $method], $arguments)
304 3
            : $this->attributes->get($method);
305
    }
306
307
    /**
308
     * Handle magic method __get.
309
     *
310
     * @param  string  $key
311
     *
312
     * @return mixed
313
     */
314 3
    public function __get($key)
315
    {
316 3
        return $this->get($key);
317
    }
318
319
    /**
320
     * Handle call to __toString method.
321
     *
322
     * @return string
323
     */
324 6
    public function __toString()
325
    {
326 6
        return $this->toJsonPretty();
327
    }
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)
337
    {
338 9
        return static::encode($data ?: $this->attributes, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
339
    }
340
341
    /**
342
     * Convert to array.
343
     *
344
     * @return array
345
     */
346 9
    public function toArray()
347
    {
348 9
        return $this->attributes()->toArray();
349
    }
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)
359
    {
360 3
        return json_encode($this->toArray(), $options);
361
    }
362
363
    /**
364
     * Allow to encode the json object to json file.
365
     *
366
     * @return array
367
     */
368 3
    public function jsonSerialize()
369
    {
370 3
        return $this->toArray();
371
    }
372
}
373