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 | private UserData $storage; |
| 12 | |
| 13 | public function __construct(UserData $storage) |
| 14 | { |
| 15 | $this->storage = $storage; |
| 16 | } |
| 17 | |
| 18 | public function find_all(): array |
| 19 | { |
| 20 | return $this->storage->get() ?: []; |
| 21 | } |
| 22 | |
| 23 | public function find(string $option) |
| 24 | { |
| 25 | $data = $this->find_all(); |
| 26 | |
| 27 | return $data[$option] ?? null; |
| 28 | } |
| 29 | |
| 30 | public function save(string $option, $value): void |
| 31 | { |
| 32 | $data = $this->find_all(); |
| 33 | |
| 34 | $data[$option] = $value; |
| 35 | |
| 36 | $this->storage->save($data); |
| 37 | } |
| 38 | |
| 39 | public function delete(string $option): void |
| 40 | { |
| 41 | $data = $this->find_all(); |
| 42 | |
| 43 | unset($data[$option]); |
| 44 | |
| 45 | $data |
| 46 | ? $this->storage->save($data) |
| 47 | : $this->storage->delete(); |
| 48 | } |
| 49 | |
| 50 | public function delete_all(): void |
| 51 | { |
| 52 | $this->storage->delete(); |
| 53 | } |
| 54 | |
| 55 | } |
| 56 |