Preference.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Preferences; |
| 6 | |
| 7 | use AC\Storage\UserData; |
| 8 | |
| 9 | final class Preference |
| 10 | { |
| 11 | |
| 12 | private UserData $storage; |
| 13 | |
| 14 | public function __construct(UserData $storage) |
| 15 | { |
| 16 | $this->storage = $storage; |
| 17 | } |
| 18 | |
| 19 | public function find_all(): array |
| 20 | { |
| 21 | return $this->storage->get() ?: []; |
| 22 | } |
| 23 | |
| 24 | public function find(string $option) |
| 25 | { |
| 26 | $data = $this->find_all(); |
| 27 | |
| 28 | return $data[$option] ?? null; |
| 29 | } |
| 30 | |
| 31 | public function save(string $option, $value): void |
| 32 | { |
| 33 | $data = $this->find_all(); |
| 34 | |
| 35 | $data[$option] = $value; |
| 36 | |
| 37 | $this->storage->save($data); |
| 38 | } |
| 39 | |
| 40 | public function delete(string $option): void |
| 41 | { |
| 42 | $data = $this->find_all(); |
| 43 | |
| 44 | unset($data[$option]); |
| 45 | |
| 46 | $data |
| 47 | ? $this->storage->save($data) |
| 48 | : $this->storage->delete(); |
| 49 | } |
| 50 | |
| 51 | public function delete_all(): void |
| 52 | { |
| 53 | $this->storage->delete(); |
| 54 | } |
| 55 | |
| 56 | } |