|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2018 VectorNetworkProject. All rights reserved. MIT license. |
| 4 | + * |
| 5 | + * GitHub: https://github.com/VectorNetworkProject/TheMix |
| 6 | + * Website: https://www.vector-network.tk |
| 7 | + */ |
| 8 | + |
| 9 | +namespace VectorNetworkProject\TheMix\provider; |
| 10 | + |
| 11 | + |
| 12 | +use pocketmine\utils\Config; |
| 13 | + |
| 14 | +class JSON extends Provider |
| 15 | +{ |
| 16 | + /* @var string $path */ |
| 17 | + private $path; |
| 18 | + |
| 19 | + /* @var string $file */ |
| 20 | + private $file; |
| 21 | + |
| 22 | + /** |
| 23 | + * JSON constructor. |
| 24 | + * @param string $xuid |
| 25 | + * @param string $file |
| 26 | + */ |
| 27 | + public function __construct(string $xuid, string $file) |
| 28 | + { |
| 29 | + $this->path = self::getPath('datas', 'json') . $xuid . '/'; |
| 30 | + $this->file = $file . '.json'; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @param array $table |
| 35 | + * @return void |
| 36 | + */ |
| 37 | + public function createTable(array $table = []): void |
| 38 | + { |
| 39 | + @mkdir($this->path); |
| 40 | + $config = new Config($this->path . $this->file, Config::JSON, $table); |
| 41 | + $config->save(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return bool |
| 46 | + */ |
| 47 | + public function hasTable(): bool |
| 48 | + { |
| 49 | + return file_exists($this->path . $this->file) |
| 50 | + ? true |
| 51 | + : false; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return bool |
| 56 | + */ |
| 57 | + public function deleteTable(): bool |
| 58 | + { |
| 59 | + return unlink($this->path . $this->file) |
| 60 | + ? true |
| 61 | + : false; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param string $key |
| 66 | + * @param bool|mixed $data |
| 67 | + */ |
| 68 | + public function set(string $key, $data): void |
| 69 | + { |
| 70 | + $config = new Config($this->path . $this->file, Config::JSON); |
| 71 | + $config->set($key, $data); |
| 72 | + $config->save(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param string $key |
| 77 | + * @return mixed |
| 78 | + */ |
| 79 | + public function get(string $key) |
| 80 | + { |
| 81 | + $config = new Config($this->path . $this->file, Config::JSON); |
| 82 | + return $config->get($key); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + public function getAll(): array |
| 89 | + { |
| 90 | + $config = new Config($this->path . $this->file, Config::JSON); |
| 91 | + return $config->getAll(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function getKeys(): array |
| 98 | + { |
| 99 | + $config = new Config($this->path . $this->file, Config::JSON); |
| 100 | + return $config->getAll(true); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param string $key |
| 105 | + * @return bool |
| 106 | + */ |
| 107 | + public function has(string $key): bool |
| 108 | + { |
| 109 | + $config = new Config($this->path . $this->file, Config::JSON); |
| 110 | + return $config->exists($key) |
| 111 | + ? true |
| 112 | + : false; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @param string $key |
| 117 | + * @return void |
| 118 | + */ |
| 119 | + public function remove(string $key): void |
| 120 | + { |
| 121 | + $config = new Config($this->path . $this->file, Config::JSON); |
| 122 | + $config->remove($key); |
| 123 | + } |
| 124 | +} |
0 commit comments