File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ZhenMu \Support \Utils ;
4
+
5
+ use Illuminate \Support \Arr ;
6
+
7
+ class Json
8
+ {
9
+ protected $ filepath ;
10
+
11
+ protected $ data = [];
12
+
13
+ public function __construct (?string $ filepath = null )
14
+ {
15
+ $ this ->filepath = $ filepath ;
16
+
17
+ $ this ->decode ();
18
+ }
19
+
20
+ public static function make (?string $ filepath = null )
21
+ {
22
+ return new static ($ filepath );
23
+ }
24
+
25
+ public function decode (?string $ content = null )
26
+ {
27
+ if ($ this ->filepath && file_exists ($ this ->filepath )) {
28
+ $ content = @file_get_contents ($ this ->filepath );
29
+ }
30
+
31
+ if (!$ content ) {
32
+ $ content = '' ;
33
+ }
34
+
35
+ $ this ->data = json_decode ($ content , true ) ?? [];
36
+
37
+ return $ this ;
38
+ }
39
+
40
+ public function get (?string $ key = null , $ default = null )
41
+ {
42
+ if (!$ key ) {
43
+ return $ this ->data ;
44
+ }
45
+
46
+ return Arr::get ($ this ->data , $ key , $ default );
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments