Skip to content

Commit d180a94

Browse files
authored
Create Json.php
1 parent 4e7e689 commit d180a94

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/Utils/Json.php

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

0 commit comments

Comments
 (0)