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