EncodedData.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\ColumnRepository; |
| 6 | |
| 7 | use AC\Collection\ColumnFactories; |
| 8 | use AC\Column; |
| 9 | use AC\ColumnCollection; |
| 10 | use AC\ColumnRepository; |
| 11 | use AC\Setting\Config; |
| 12 | use AC\Setting\ConfigCollection; |
| 13 | use AC\Storage\Repository\OriginalColumnsRepository; |
| 14 | use AC\TableScreen; |
| 15 | |
| 16 | class EncodedData implements ColumnRepository |
| 17 | { |
| 18 | private ColumnFactories $factories; |
| 19 | |
| 20 | private ConfigCollection $configs; |
| 21 | |
| 22 | private OriginalColumnsRepository $original_columns_repository; |
| 23 | |
| 24 | private TableScreen $table_screen; |
| 25 | |
| 26 | public function __construct( |
| 27 | ColumnFactories $factories, |
| 28 | ConfigCollection $configs, |
| 29 | OriginalColumnsRepository $original_columns_repository, |
| 30 | TableScreen $table_screen |
| 31 | ) { |
| 32 | $this->configs = $configs; |
| 33 | $this->factories = $factories; |
| 34 | $this->original_columns_repository = $original_columns_repository; |
| 35 | $this->table_screen = $table_screen; |
| 36 | } |
| 37 | |
| 38 | public function find_all(): ColumnCollection |
| 39 | { |
| 40 | $columns = new ColumnCollection(); |
| 41 | |
| 42 | foreach ($this->configs as $config) { |
| 43 | $config = $this->modify_config($config); |
| 44 | |
| 45 | $column = $this->find((string)$config->get('type'), $config); |
| 46 | |
| 47 | if ($column) { |
| 48 | $columns->add($column); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return $columns; |
| 53 | } |
| 54 | |
| 55 | private function modify_config(Config $config): Config |
| 56 | { |
| 57 | // In some rare cases the stored 'name' can have a mismatch with it's 'type' for original columns |
| 58 | if ($this->original_columns_repository->find($this->table_screen->get_id(), (string)$config->get('type'))) { |
| 59 | $data = $config->all(); |
| 60 | $data['name'] = $data['type']; |
| 61 | |
| 62 | return new Config($data); |
| 63 | } |
| 64 | |
| 65 | return $config; |
| 66 | } |
| 67 | |
| 68 | private function find(string $type, Config $config): ?Column |
| 69 | { |
| 70 | foreach ($this->factories as $factory) { |
| 71 | if ($type === $factory->get_column_type()) { |
| 72 | return $factory->create($config); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 |