Skip to content

Commit 12960d6

Browse files
committed
Update Json.php
1 parent 5280551 commit 12960d6

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
"email": "[email protected]"
14+
}
15+
],
16+
"require": {}
17+
}

src/classes/Json.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)