EditButton.php
2 days ago
EditorFavorites.php
2 days ago
EditorMenuStatus.php
2 days ago
IntegrationStatus.php
2 days ago
ListColumnOrder.php
2 days ago
ListScreenOrder.php
2 days ago
OriginalColumnsRepository.php
2 days ago
TableListOrder.php
2 days ago
UserColumnOrder.php
2 days ago
OriginalColumnsRepository.php
144 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Storage\Repository; |
| 6 | |
| 7 | use AC\Storage\Option; |
| 8 | use AC\Type\OriginalColumn; |
| 9 | use AC\Type\OriginalColumns; |
| 10 | use AC\Type\TableId; |
| 11 | use Exception; |
| 12 | |
| 13 | final class OriginalColumnsRepository |
| 14 | { |
| 15 | private function storage(TableId $id): Option |
| 16 | { |
| 17 | return new Option( |
| 18 | sprintf('_ac_columns_default_%s', $id) |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | public function update(TableId $id, OriginalColumns $columns): void |
| 23 | { |
| 24 | $data = []; |
| 25 | |
| 26 | foreach ($columns as $column) { |
| 27 | $args = [ |
| 28 | 'label' => $column->get_label(), |
| 29 | ]; |
| 30 | |
| 31 | if ($column->is_sortable()) { |
| 32 | $args['sortable'] = true; |
| 33 | } |
| 34 | |
| 35 | $data[$column->get_name()] = $args; |
| 36 | } |
| 37 | |
| 38 | $this->storage($id)->save($data); |
| 39 | } |
| 40 | |
| 41 | public function exists(TableId $id): bool |
| 42 | { |
| 43 | return false !== $this->get_cached($id); |
| 44 | } |
| 45 | |
| 46 | public function delete(TableId $id): void |
| 47 | { |
| 48 | $this |
| 49 | ->storage($id) |
| 50 | ->delete(); |
| 51 | } |
| 52 | |
| 53 | public function find(TableId $id, string $type): ?OriginalColumn |
| 54 | { |
| 55 | $data = $this->get_cached($id); |
| 56 | |
| 57 | if (! $data) { |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | $column_data = $data[$type] ?? null; |
| 62 | |
| 63 | return $column_data && is_array($column_data) |
| 64 | ? $this->create_column($type, $column_data) |
| 65 | : null; |
| 66 | } |
| 67 | |
| 68 | public function find_all_cached(TableId $id): OriginalColumns |
| 69 | { |
| 70 | $columns = []; |
| 71 | |
| 72 | $data = $this->get_cached($id); |
| 73 | |
| 74 | if ($data) { |
| 75 | foreach ($data as $type => $column_data) { |
| 76 | if ('cb' === $type) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | $column = $this->create_column($type, $column_data); |
| 81 | |
| 82 | if (! $column) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | $columns[] = $column; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return new OriginalColumns($columns); |
| 91 | } |
| 92 | |
| 93 | private function get_cached(TableId $id) |
| 94 | { |
| 95 | static $cached_storage; |
| 96 | |
| 97 | if (! isset($cached_storage[(string)$id])) { |
| 98 | $cached_storage[(string)$id] = $this->storage($id)->get(); |
| 99 | } |
| 100 | |
| 101 | return $cached_storage[(string)$id]; |
| 102 | } |
| 103 | |
| 104 | public function find_all(TableId $id): OriginalColumns |
| 105 | { |
| 106 | $columns = []; |
| 107 | |
| 108 | foreach ($this->get($id) as $type => $data) { |
| 109 | if ('cb' === $type) { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | $column = $this->create_column($type, $data); |
| 114 | |
| 115 | if (! $column) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | $columns[] = $column; |
| 120 | } |
| 121 | |
| 122 | return new OriginalColumns($columns); |
| 123 | } |
| 124 | |
| 125 | private function create_column(string $type, array $data): ?OriginalColumn |
| 126 | { |
| 127 | try { |
| 128 | return new OriginalColumn( |
| 129 | $type, |
| 130 | (string)($data['label'] ?? ''), |
| 131 | (bool)($data['sortable'] ?? false) |
| 132 | ); |
| 133 | } catch (Exception $e) { |
| 134 | return null; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private function get(TableId $id): array |
| 139 | { |
| 140 | return $this->storage($id)->get() ?: []; |
| 141 | } |
| 142 | |
| 143 | } |
| 144 |