1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resta\Support; |
4
|
|
|
|
5
|
|
|
use Resta\Exception\FileNotFoundException; |
6
|
|
|
|
7
|
|
|
class JsonHandler |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
public static $file; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private static $singleton; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* checks according to the path and extension of the file. |
21
|
|
|
* |
22
|
|
|
* @return string|void |
23
|
|
|
*/ |
24
|
|
|
private static function checkFile() |
25
|
|
|
{ |
26
|
|
|
if(!is_null(self::$singleton)) return self::$singleton; |
|
|
|
|
27
|
|
|
|
28
|
|
|
$filePortions = explode(DIRECTORY_SEPARATOR,self::$file); |
29
|
|
|
$pop = array_pop($filePortions); |
30
|
|
|
|
31
|
|
|
if(file_exists(implode(DIRECTORY_SEPARATOR,$filePortions)) |
32
|
|
|
&& preg_match('@[a-zA-Z0-9]+\.json@',$pop)){ |
33
|
|
|
self::$singleton = self::$file; |
34
|
|
|
return self::$singleton; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
exception()->runtime('file path is invalid for json handler'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* create if not file exits |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public static function createIfNotFileExist() |
46
|
|
|
{ |
47
|
|
|
$file = self::checkFile(); |
48
|
|
|
|
49
|
|
|
if(!file_exists($file)){ |
50
|
|
|
files()->put($file,self::encode([])); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* makes decode as json the given array |
56
|
|
|
* |
57
|
|
|
* @param $data |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public static function decode($data) |
61
|
|
|
{ |
62
|
|
|
return json_decode($data,1); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* delete key from json file |
67
|
|
|
* |
68
|
|
|
* @param $key |
69
|
|
|
* @param null $arrayKey |
|
|
|
|
70
|
|
|
* @return bool |
71
|
|
|
* |
72
|
|
|
* @throws FileNotFoundException |
73
|
|
|
*/ |
74
|
|
|
public static function delete($key,$arrayKey=null) |
75
|
|
|
{ |
76
|
|
|
$data = self::get(); |
77
|
|
|
|
78
|
|
|
if(is_null($arrayKey)){ |
|
|
|
|
79
|
|
|
|
80
|
|
|
if(isset($data[$key])){ |
81
|
|
|
unset($data[$key]); |
82
|
|
|
files()->put(self::checkFile(),self::encode($data),true); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// if the data to be deleted |
88
|
|
|
// in json data contains a nested array data. |
89
|
|
|
if(!is_null($arrayKey) && is_string($arrayKey)){ |
|
|
|
|
90
|
|
|
|
91
|
|
|
if(isset($data[$key][$arrayKey])){ |
92
|
|
|
unset($data[$key][$arrayKey]); |
93
|
|
|
files()->put(self::checkFile(),self::encode($data),true); |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* makes encode as json the given array |
103
|
|
|
* |
104
|
|
|
* @param $data |
105
|
|
|
* @return false|string |
106
|
|
|
*/ |
107
|
|
|
public static function encode($data) |
108
|
|
|
{ |
109
|
|
|
return json_encode($data,JSON_PRETTY_PRINT); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* get json data |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
* |
117
|
|
|
* @throws FileNotFoundException |
118
|
|
|
*/ |
119
|
|
|
public static function get() |
120
|
|
|
{ |
121
|
|
|
self::createIfNotFileExist(); |
122
|
|
|
|
123
|
|
|
return self::decode(files()->get(self::checkFile())); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* set key value into json file |
128
|
|
|
* |
129
|
|
|
* @param $key |
130
|
|
|
* @param $value |
131
|
|
|
* @return false|string |
132
|
|
|
* |
133
|
|
|
* @throws FileNotFoundException |
134
|
|
|
*/ |
135
|
|
|
public static function set($key,$value) |
136
|
|
|
{ |
137
|
|
|
self::createIfNotFileExist(); |
138
|
|
|
|
139
|
|
|
$file = self::get(); |
140
|
|
|
|
141
|
|
|
if(isset($file[$key]) && is_array($value)){ |
142
|
|
|
$file[$key] = array_merge($file[$key],$value); |
143
|
|
|
files()->put(self::checkFile(),self::encode($file)); |
144
|
|
|
} |
145
|
|
|
else{ |
146
|
|
|
files()->put(self::checkFile(),self::encode(array_merge($file,[$key=>$value]))); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return self::get(); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |