| 1: | <?php |
| 2: | |
| 3: | declare(strict_types = 1); |
| 4: | |
| 5: | namespace CloudCastle\FileSystem; |
| 6: | |
| 7: | use \stdClass; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class Json |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | public static function read(?string $jsonFile = null): stdClass |
| 26: | { |
| 27: | $data = new stdClass; |
| 28: | if (File::has($jsonFile)) { |
| 29: | $info = File::info($jsonFile); |
| 30: | if ($info->extension === 'json') { |
| 31: | $data = json_decode(File::read($jsonFile)); |
| 32: | } |
| 33: | } |
| 34: | return $data; |
| 35: | } |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public static function create(string $jsonFile, $data = false): bool |
| 44: | { |
| 45: | $content = false; |
| 46: | if (is_object($data) OR is_array($data)) { |
| 47: | $content = json_encode($data, JSON_PRETTY_PRINT); |
| 48: | } |
| 49: | if ( ! File::has($jsonFile) AND $content) { |
| 50: | return File::create($jsonFile, $content); |
| 51: | } |
| 52: | return false; |
| 53: | } |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public static function rewrite(string $jsonFile, $data = false): bool |
| 62: | { |
| 63: | if (is_array($data) OR is_object($data)) { |
| 64: | if (File::has($jsonFile) AND $data AND File::delete($jsonFile)) { |
| 65: | return self::create($jsonFile, $data); |
| 66: | } |
| 67: | } |
| 68: | return self::create($jsonFile, $data); |
| 69: | } |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public static function delete(string $jsonFile): bool |
| 77: | { |
| 78: | if (self::has($jsonFile)) { |
| 79: | return File::delete($jsonFile); |
| 80: | } |
| 81: | return false; |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public static function has(string $jsonFile): bool |
| 90: | { |
| 91: | $info = File::info($jsonFile); |
| 92: | if ($info->extension === 'json') { |
| 93: | return true; |
| 94: | } |
| 95: | return false; |
| 96: | } |
| 97: | |
| 98: | } |
| 99: | |