ListStorage.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\ColumnSize; |
| 4 | |
| 5 | use AC\Column; |
| 6 | use AC\Column\SettingUpdater; |
| 7 | use AC\ColumnIterator; |
| 8 | use AC\Type\ColumnId; |
| 9 | use AC\Type\ColumnWidth; |
| 10 | use AC\Type\ListScreenId; |
| 11 | use InvalidArgumentException; |
| 12 | |
| 13 | class ListStorage |
| 14 | { |
| 15 | |
| 16 | private SettingUpdater $setting_updater; |
| 17 | |
| 18 | public function __construct(SettingUpdater $setting_updater) |
| 19 | { |
| 20 | $this->setting_updater = $setting_updater; |
| 21 | } |
| 22 | |
| 23 | public function save(ListScreenId $list_id, ColumnId $column_id, ColumnWidth $column_width): void |
| 24 | { |
| 25 | $this->setting_updater->update($list_id, $column_id, [ |
| 26 | 'width' => (string) $column_width->get_value(), |
| 27 | 'width_unit' => $column_width->get_unit(), |
| 28 | ]); |
| 29 | } |
| 30 | |
| 31 | public function get(Column $column): ?ColumnWidth |
| 32 | { |
| 33 | return $this->create($column); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @return ColumnWidth[] |
| 38 | */ |
| 39 | public function get_all(ColumnIterator $columns): array |
| 40 | { |
| 41 | $results = []; |
| 42 | |
| 43 | foreach ($columns as $column) { |
| 44 | $width = $this->create($column); |
| 45 | |
| 46 | if ($width) { |
| 47 | $results[(string) $column->get_id()] = $width; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return $results; |
| 52 | } |
| 53 | |
| 54 | private function create(Column $column): ?ColumnWidth |
| 55 | { |
| 56 | $width_setting = $column->get_setting('width'); |
| 57 | |
| 58 | if ( ! $width_setting) { |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | $width = (int) $width_setting->get_input()->get_value(); |
| 63 | |
| 64 | if ($width < 1) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | $unit = (string) $column->get_setting('width_unit')->get_input()->get_value(); |
| 69 | |
| 70 | try { |
| 71 | $width = new ColumnWidth($unit, $width); |
| 72 | } catch (InvalidArgumentException $e) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | return $width; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 |