File tree Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ /vendor /
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " dinosdev/json" ,
3
+ "description" : " A PHP Library for Modify JSON" ,
4
+ "license" : " MIT" ,
5
+ "autoload" : {
6
+ "psr-4" : {
7
+ "DinoDev\\ Json\\ " : " src/"
8
+ }
9
+ },
10
+ "authors" : [
11
+ {
12
+ "name" : " DinosDev" ,
13
+
14
+ }
15
+ ],
16
+ "require" : {}
17
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace DinoDev \Json \Classes ;
4
+
5
+ use Exception ;
6
+
7
+ class Json
8
+ {
9
+ /**
10
+ * Get a JSON and Return It
11
+ */
12
+ public function getJson (string $ jsonPath )
13
+ {
14
+ $ fileExists = @file_exists ($ jsonPath );
15
+ $ fileContent = @file_get_contents ($ jsonPath );
16
+ $ jsonArray = @json_decode ($ fileContent , true );
17
+
18
+ $ basename = basename ($ jsonPath );
19
+
20
+ //Check File Exists
21
+ if (!$ fileExists ) throw new Exception ("File \"$ basename \" Not Exists " );
22
+
23
+ //Check Read File
24
+ if (!$ fileContent ) throw new Exception ("File \"$ basename \" Cannot be Open " );
25
+
26
+ //Check Array Convertion
27
+ if (!$ jsonArray ) throw new Exception ("File \"$ basename \" Cannot be Converted to Array " );
28
+
29
+ return $ jsonArray ;
30
+ }
31
+
32
+ /**
33
+ * Write data in a JSON File
34
+ */
35
+ public function writeJson (string $ jsonPath , array $ data )
36
+ {
37
+ $ fileExists = @file_exists ($ jsonPath );
38
+
39
+ $ basename = basename ($ jsonPath );
40
+
41
+ //Check File Exists
42
+ if (!$ fileExists ) throw new Exception ("File \"$ basename \" Not Exists " );
43
+
44
+ return @file_put_contents ($ jsonPath , json_encode ($ data ));
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments