Completed
Push — master ( b3d024...7ceb75 )
by ARCANEDEV
04:24
created

Json::getContents()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php namespace Arcanedev\Support;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use Illuminate\Filesystem\Filesystem;
6
use JsonSerializable;
7
8
/**
9
 * Class     Json
10
 *
11
 * @package  Arcanedev\Support
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class Json implements Arrayable, Jsonable, JsonSerializable
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /**
21
     * The file path.
22
     *
23
     * @var string
24
     */
25
    protected $path;
26
27
    /**
28
     * The laravel filesystem instance.
29
     *
30
     * @var Filesystem
31
     */
32
    protected $filesystem;
33
34
    /**
35
     * The attributes collection.
36
     *
37
     * @var Collection
38
     */
39
    protected $attributes;
40
41
    /* ------------------------------------------------------------------------------------------------
42
     |  Constructor
43
     | ------------------------------------------------------------------------------------------------
44
     */
45
    /**
46
     * Construct the Json instance.
47
     *
48
     * @param  mixed       $path
49
     * @param  Filesystem  $filesystem
50
     */
51 56
    public function __construct($path, Filesystem $filesystem = null)
52
    {
53 56
        $this->path         = (string) $path;
54 56
        $this->filesystem   = $filesystem ?: new Filesystem;
55 56
        $this->attributes   = Collection::make($this->getAttributes());
56 56
    }
57
58
    /* ------------------------------------------------------------------------------------------------
59
     |  Getters & Setters
60
     | ------------------------------------------------------------------------------------------------
61
     */
62
    /**
63
     * Get the filesystem instance.
64
     *
65
     * @return Filesystem
66
     */
67 16
    public function getFilesystem()
68
    {
69 16
        return $this->filesystem;
70
    }
71
72
    /**
73
     * Set the filesystem instance.
74
     *
75
     * @param  Filesystem  $filesystem
76
     *
77
     * @return self
78
     */
79 8
    public function setFilesystem(Filesystem $filesystem)
80
    {
81 8
        $this->filesystem = $filesystem;
82
83 8
        return $this;
84
    }
85
86
    /**
87
     * Get path.
88
     *
89
     * @return string
90
     */
91 56
    public function getPath()
92
    {
93 56
        return $this->path;
94
    }
95
96
    /**
97
     * Set path.
98
     *
99
     * @param  mixed  $path
100
     *
101
     * @return self
102
     */
103 24
    public function setPath($path)
104
    {
105 24
        $this->path = (string) $path;
106
107 24
        return $this;
108
    }
109
110
    /* ------------------------------------------------------------------------------------------------
111
     |  Main Functions
112
     | ------------------------------------------------------------------------------------------------
113
     */
114
    /**
115
     * Make new instance.
116
     *
117
     * @param  string      $path
118
     * @param  Filesystem  $filesystem
119
     *
120
     * @return static
121
     */
122 8
    public static function make($path, Filesystem $filesystem = null)
123
    {
124 8
        return new static($path, $filesystem);
125
    }
126
127
    /**
128
     * Get file content.
129
     *
130
     * @return string
131
     */
132 56
    public function getContents()
133
    {
134 56
        return $this->filesystem->get($this->getPath());
135
    }
136
137
    /**
138
     * Update json contents from array data.
139
     *
140
     * @param  array  $data
141
     *
142
     * @return bool
143
     */
144 8
    public function update(array $data)
145
    {
146 8
        $this->attributes = new Collection(array_merge(
147 8
            $this->attributes->toArray(),
148
            $data
149 6
        ));
150
151 8
        return $this->save();
152
    }
153
154
    /**
155
     * Handle call to __call method.
156
     *
157
     * @param  string  $method
158
     * @param  array   $arguments
159
     *
160
     * @return mixed
161
     */
162 8
    public function __call($method, $arguments = [])
163
    {
164 8
        return method_exists($this, $method)
165 6
            ? call_user_func_array([$this, $method], $arguments)
166 8
            : $this->attributes->get($method);
167
    }
168
169
    /**
170
     * Handle magic method __get.
171
     *
172
     * @param  string  $key
173
     *
174
     * @return mixed
175
     */
176 8
    public function __get($key)
177
    {
178 8
        return $this->get($key);
179
    }
180
181
    /**
182
     * Get the specified attribute from json file.
183
     *
184
     * @param  string      $key
185
     * @param  mixed|null  $default
186
     *
187
     * @return mixed
188
     */
189 8
    public function get($key, $default = null)
190
    {
191 8
        return $this->attributes->get($key, $default);
192
    }
193
194
    /**
195
     * Set a specific key & value.
196
     *
197
     * @param  string  $key
198
     * @param  mixed   $value
199
     *
200
     * @return self
201
     */
202 8
    public function set($key, $value)
203
    {
204 8
        $this->attributes->offsetSet($key, $value);
205
206 8
        return $this;
207
    }
208
209
    /**
210
     * Save the current attributes array to the file storage.
211
     *
212
     * @return bool
213
     */
214 16
    public function save()
215
    {
216 16
        return $this->filesystem->put($this->getPath(), $this->toJsonPretty());
217
    }
218
219
    /**
220
     * Get file contents as array.
221
     *
222
     * @return array
223
     */
224 56
    public function getAttributes()
225
    {
226 56
        return json_decode($this->getContents(), true);
227
    }
228
229
    /**
230
     * Convert the given array data to pretty json.
231
     *
232
     * @param  array  $data
233
     *
234
     * @return string
235
     */
236 16
    public function toJsonPretty(array $data = null)
237
    {
238 16
        return json_encode($data ?: $this->attributes, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
239
    }
240
241
    /**
242
     * Handle call to __toString method.
243
     *
244
     * @return string
245
     */
246 8
    public function __toString()
247
    {
248 8
        return $this->getContents();
249
    }
250
251
    /**
252
     * Convert to array.
253
     *
254
     * @return array
255
     */
256 16
    public function toArray()
257
    {
258 16
        return $this->getAttributes();
259
    }
260
261
    /**
262
     * Convert the object to its JSON representation.
263
     *
264
     * @param  int  $options
265
     *
266
     * @return string
267
     */
268 8
    public function toJson($options = 0)
269
    {
270 8
        return json_encode($this->toArray(), $options);
271
    }
272
273
    /**
274
     * Allow to encode the json object to json file.
275
     *
276
     * @return array
277
     */
278 8
    public function jsonSerialize()
279
    {
280 8
        return $this->toArray();
281
    }
282
}
283