Salah3id /
address-domains
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Salah3id\Domains; |
||||
| 4 | |||||
| 5 | use Illuminate\Filesystem\Filesystem; |
||||
| 6 | use Salah3id\Domains\Exceptions\InvalidJsonException; |
||||
| 7 | |||||
| 8 | class Json |
||||
| 9 | { |
||||
| 10 | /** |
||||
| 11 | * The file path. |
||||
| 12 | * |
||||
| 13 | * @var string |
||||
| 14 | */ |
||||
| 15 | protected $path; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * The laravel filesystem instance. |
||||
| 19 | * |
||||
| 20 | * @var \Illuminate\Filesystem\Filesystem |
||||
| 21 | */ |
||||
| 22 | protected $filesystem; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * The attributes collection. |
||||
| 26 | * |
||||
| 27 | * @var \Illuminate\Support\Collection |
||||
| 28 | */ |
||||
| 29 | protected $attributes; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The constructor. |
||||
| 33 | * |
||||
| 34 | * @param mixed $path |
||||
| 35 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||||
| 36 | */ |
||||
| 37 | public function __construct($path, Filesystem $filesystem = null) |
||||
| 38 | { |
||||
| 39 | $this->path = (string) $path; |
||||
| 40 | $this->filesystem = $filesystem ?: new Filesystem(); |
||||
| 41 | $this->attributes = Collection::make($this->getAttributes()); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * Get filesystem. |
||||
| 46 | * |
||||
| 47 | * @return Filesystem |
||||
| 48 | */ |
||||
| 49 | public function getFilesystem() |
||||
| 50 | { |
||||
| 51 | return $this->filesystem; |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * Set filesystem. |
||||
| 56 | * |
||||
| 57 | * @param Filesystem $filesystem |
||||
| 58 | * |
||||
| 59 | * @return $this |
||||
| 60 | */ |
||||
| 61 | public function setFilesystem(Filesystem $filesystem) |
||||
| 62 | { |
||||
| 63 | $this->filesystem = $filesystem; |
||||
| 64 | |||||
| 65 | return $this; |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | /** |
||||
| 69 | * Get path. |
||||
| 70 | * |
||||
| 71 | * @return string |
||||
| 72 | */ |
||||
| 73 | public function getPath() |
||||
| 74 | { |
||||
| 75 | return $this->path; |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Set path. |
||||
| 80 | * |
||||
| 81 | * @param mixed $path |
||||
| 82 | * |
||||
| 83 | * @return $this |
||||
| 84 | */ |
||||
| 85 | public function setPath($path) |
||||
| 86 | { |
||||
| 87 | $this->path = (string) $path; |
||||
| 88 | |||||
| 89 | return $this; |
||||
| 90 | } |
||||
| 91 | |||||
| 92 | /** |
||||
| 93 | * Make new instance. |
||||
| 94 | * |
||||
| 95 | * @param string $path |
||||
| 96 | * @param \Illuminate\Filesystem\Filesystem $filesystem |
||||
| 97 | * |
||||
| 98 | * @return static |
||||
| 99 | */ |
||||
| 100 | public static function make($path, Filesystem $filesystem = null) |
||||
| 101 | { |
||||
| 102 | return new static($path, $filesystem); |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | /** |
||||
| 106 | * Get file content. |
||||
| 107 | * |
||||
| 108 | * @return string |
||||
| 109 | */ |
||||
| 110 | public function getContents() |
||||
| 111 | { |
||||
| 112 | return $this->filesystem->get($this->getPath()); |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * Get file contents as array. |
||||
| 117 | * @return array |
||||
| 118 | * @throws \Exception |
||||
| 119 | */ |
||||
| 120 | public function getAttributes() |
||||
| 121 | { |
||||
| 122 | $attributes = json_decode($this->getContents(), 1); |
||||
| 123 | |||||
| 124 | // any JSON parsing errors should throw an exception |
||||
| 125 | if (json_last_error() > 0) { |
||||
| 126 | throw new InvalidJsonException('Error processing file: ' . $this->getPath() . '. Error: ' . json_last_error_msg()); |
||||
| 127 | } |
||||
| 128 | |||||
| 129 | if (config('domains.cache.enabled') === false) { |
||||
| 130 | return $attributes; |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | return app('cache')->store(config('domains.cache.driver'))->remember($this->getPath(), config('domains.cache.lifetime'), function () use ($attributes) { |
||||
| 134 | return $attributes; |
||||
| 135 | }); |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | /** |
||||
| 139 | * Convert the given array data to pretty json. |
||||
| 140 | * |
||||
| 141 | * @param array $data |
||||
| 142 | * |
||||
| 143 | * @return string |
||||
| 144 | */ |
||||
| 145 | public function toJsonPretty(array $data = null) |
||||
| 146 | { |
||||
| 147 | return json_encode($data ?: $this->attributes, JSON_PRETTY_PRINT); |
||||
| 148 | } |
||||
| 149 | |||||
| 150 | /** |
||||
| 151 | * Update json contents from array data. |
||||
| 152 | * |
||||
| 153 | * @param array $data |
||||
| 154 | * |
||||
| 155 | * @return bool |
||||
| 156 | */ |
||||
| 157 | public function update(array $data) |
||||
| 158 | { |
||||
| 159 | $this->attributes = new Collection(array_merge($this->attributes->toArray(), $data)); |
||||
|
0 ignored issues
–
show
array_merge($this->attributes->toArray(), $data) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Salah3id\Domains\Collection::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 160 | |||||
| 161 | return $this->save(); |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * Set a specific key & value. |
||||
| 166 | * |
||||
| 167 | * @param string $key |
||||
| 168 | * @param mixed $value |
||||
| 169 | * |
||||
| 170 | * @return $this |
||||
| 171 | */ |
||||
| 172 | public function set($key, $value) |
||||
| 173 | { |
||||
| 174 | $this->attributes->offsetSet($key, $value); |
||||
|
0 ignored issues
–
show
$key of type string is incompatible with the type Illuminate\Support\TKey|null expected by parameter $key of Illuminate\Support\Collection::offsetSet().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 175 | |||||
| 176 | return $this; |
||||
| 177 | } |
||||
| 178 | |||||
| 179 | /** |
||||
| 180 | * Save the current attributes array to the file storage. |
||||
| 181 | * |
||||
| 182 | * @return bool |
||||
| 183 | */ |
||||
| 184 | public function save() |
||||
| 185 | { |
||||
| 186 | return $this->filesystem->put($this->getPath(), $this->toJsonPretty()); |
||||
|
0 ignored issues
–
show
|
|||||
| 187 | } |
||||
| 188 | |||||
| 189 | /** |
||||
| 190 | * Handle magic method __get. |
||||
| 191 | * |
||||
| 192 | * @param string $key |
||||
| 193 | * |
||||
| 194 | * @return mixed |
||||
| 195 | */ |
||||
| 196 | public function __get($key) |
||||
| 197 | { |
||||
| 198 | return $this->get($key); |
||||
| 199 | } |
||||
| 200 | |||||
| 201 | /** |
||||
| 202 | * Get the specified attribute from json file. |
||||
| 203 | * |
||||
| 204 | * @param $key |
||||
| 205 | * @param null $default |
||||
|
0 ignored issues
–
show
|
|||||
| 206 | * |
||||
| 207 | * @return mixed |
||||
| 208 | */ |
||||
| 209 | public function get($key, $default = null) |
||||
| 210 | { |
||||
| 211 | return $this->attributes->get($key, $default); |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | /** |
||||
| 215 | * Handle call to __call method. |
||||
| 216 | * |
||||
| 217 | * @param string $method |
||||
| 218 | * @param array $arguments |
||||
| 219 | * |
||||
| 220 | * @return mixed |
||||
| 221 | */ |
||||
| 222 | public function __call($method, $arguments = []) |
||||
| 223 | { |
||||
| 224 | if (method_exists($this, $method)) { |
||||
| 225 | return call_user_func_array([$this, $method], $arguments); |
||||
| 226 | } |
||||
| 227 | |||||
| 228 | return call_user_func_array([$this->attributes, $method], $arguments); |
||||
| 229 | } |
||||
| 230 | |||||
| 231 | /** |
||||
| 232 | * Handle call to __toString method. |
||||
| 233 | * |
||||
| 234 | * @return string |
||||
| 235 | */ |
||||
| 236 | public function __toString() |
||||
| 237 | { |
||||
| 238 | return $this->getContents(); |
||||
| 239 | } |
||||
| 240 | } |
||||
| 241 |