UserStorage.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\ColumnSize; |
| 4 | |
| 5 | use AC\Preferences\Preference; |
| 6 | use AC\Preferences\SiteFactory; |
| 7 | use AC\Type\ColumnId; |
| 8 | use AC\Type\ColumnWidth; |
| 9 | use AC\Type\ListScreenId; |
| 10 | |
| 11 | class UserStorage |
| 12 | { |
| 13 | |
| 14 | private const OPTION_UNIT = 'unit'; |
| 15 | private const OPTION_VALUE = 'value'; |
| 16 | |
| 17 | private SiteFactory $storage_factory; |
| 18 | |
| 19 | public function __construct(SiteFactory $storage_factory) |
| 20 | { |
| 21 | $this->storage_factory = $storage_factory; |
| 22 | } |
| 23 | |
| 24 | private function create_storage(): Preference |
| 25 | { |
| 26 | return $this->storage_factory->create('column_widths'); |
| 27 | } |
| 28 | |
| 29 | public function save(ListScreenId $list_id, ColumnId $column_name, ColumnWidth $column_width): void |
| 30 | { |
| 31 | $widths = $this->create_storage()->find((string)$list_id) ?: []; |
| 32 | |
| 33 | $widths[(string)$column_name] = [ |
| 34 | self::OPTION_UNIT => $column_width->get_unit(), |
| 35 | self::OPTION_VALUE => $column_width->get_value(), |
| 36 | ]; |
| 37 | |
| 38 | $this->create_storage()->save( |
| 39 | (string)$list_id, |
| 40 | $widths |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | public function exists(ListScreenId $list_id): bool |
| 45 | { |
| 46 | return null !== $this->create_storage()->find((string)$list_id); |
| 47 | } |
| 48 | |
| 49 | public function get(ListScreenId $list_id, ColumnId $column_id): ?ColumnWidth |
| 50 | { |
| 51 | $widths = $this->create_storage()->find((string)$list_id); |
| 52 | |
| 53 | $name = (string)$column_id; |
| 54 | |
| 55 | if ( ! isset($widths[$name])) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | return new ColumnWidth( |
| 60 | $widths[$name][self::OPTION_UNIT], |
| 61 | $widths[$name][self::OPTION_VALUE] |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param ListScreenId $list_id |
| 67 | * |
| 68 | * @return ColumnWidth[] |
| 69 | */ |
| 70 | public function get_all(ListScreenId $list_id): array |
| 71 | { |
| 72 | $widths = $this->create_storage()->find((string)$list_id); |
| 73 | |
| 74 | if ( ! $widths) { |
| 75 | return []; |
| 76 | } |
| 77 | |
| 78 | $column_widths = []; |
| 79 | |
| 80 | foreach ($widths as $column_name => $width) { |
| 81 | $column_widths[$column_name] = new ColumnWidth( |
| 82 | $width[self::OPTION_UNIT], |
| 83 | $width[self::OPTION_VALUE] |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | return $column_widths; |
| 88 | } |
| 89 | |
| 90 | public function delete(ListScreenId $list_id, string $column_name): void |
| 91 | { |
| 92 | $widths = $this->create_storage()->find( |
| 93 | (string)$list_id |
| 94 | ); |
| 95 | |
| 96 | if ( ! $widths) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | unset($widths[$column_name]); |
| 101 | |
| 102 | $widths |
| 103 | ? $this->create_storage()->save((string)$list_id, $widths) |
| 104 | : $this->delete_by_list_id($list_id); |
| 105 | } |
| 106 | |
| 107 | public function delete_by_list_id(ListScreenId $list_id): void |
| 108 | { |
| 109 | $this->create_storage()->delete( |
| 110 | (string)$list_id |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | } |