PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / ListScreenRepository / Storage.php
codepress-admin-columns / classes / ListScreenRepository Last commit date
Filter 3 days ago Rule 3 days ago Sort 3 days ago Storage 3 days ago Base.php 3 days ago Database.php 3 days ago Filter.php 3 days ago Rule.php 3 days ago Rules.php 3 days ago Sort.php 3 days ago Storage.php 3 days ago Types.php 3 days ago
Storage.php
177 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\ListScreenRepository;
6
7 use AC\ListScreen;
8 use AC\ListScreenCollection;
9 use AC\ListScreenRepository;
10 use AC\ListScreenRepositoryWritable;
11 use AC\Type\ListScreenId;
12 use AC\Type\ListScreenStatus;
13 use AC\Type\TableId;
14 use LogicException;
15
16 final class Storage extends Base implements ListScreenRepositoryWritable
17 {
18 /**
19 * @var Storage\ListScreenRepository[]
20 */
21 private array $repositories = [];
22
23 /**
24 * @return Storage\ListScreenRepository[]
25 */
26 public function get_repositories(): array
27 {
28 return array_reverse($this->repositories);
29 }
30
31 public function set_repositories(array $repositories): void
32 {
33 foreach ($repositories as $repository) {
34 if (! $repository instanceof ListScreenRepository\Storage\ListScreenRepository) {
35 throw new LogicException('Expected a Storage\ListScreenRepository object.');
36 }
37 }
38
39 $this->repositories = array_reverse($repositories);
40 }
41
42 public function with_repository(string $name, ListScreenRepository\Storage\ListScreenRepository $repository): self
43 {
44 $repositories = $this->get_repositories();
45 $repositories[$name] = $repository;
46
47 $storage = new self();
48 $storage->set_repositories($repositories);
49
50 return $storage;
51 }
52
53 public function has_repository($key): bool
54 {
55 return array_key_exists($key, $this->repositories);
56 }
57
58 public function get_repository($key): Storage\ListScreenRepository
59 {
60 if (! $this->has_repository($key)) {
61 throw new LogicException(sprintf('Repository with key %s not found.', $key));
62 }
63
64 return $this->repositories[$key];
65 }
66
67 public function find(ListScreenId $id): ?ListScreen
68 {
69 foreach ($this->repositories as $repository) {
70 $list_screen = $repository->get_list_screen_repository()->find($id);
71
72 if ($list_screen) {
73 $list_screen->set_read_only(! $repository->is_writable());
74
75 return $list_screen;
76 }
77 }
78
79 return null;
80 }
81
82 public function find_all(?Sort $sort = null): ListScreenCollection
83 {
84 $collection = new ListScreenCollection();
85
86 foreach ($this->repositories as $repository) {
87 foreach ($repository->get_list_screen_repository()->find_all() as $list_screen) {
88 if (! $collection->contains($list_screen)) {
89 $list_screen->set_read_only(! $repository->is_writable());
90
91 $collection->add($list_screen);
92 }
93 }
94 }
95
96 return $this->sort($collection, $sort);
97 }
98
99 public function find_all_by_table_id(
100 TableId $table_id,
101 ?Sort $sort = null,
102 ?ListScreenStatus $status = null
103 ): ListScreenCollection {
104 $collection = new ListScreenCollection();
105
106 foreach ($this->repositories as $repository) {
107 $list_screens = $repository
108 ->get_list_screen_repository()
109 ->find_all_by_table_id($table_id, null, $status);
110
111 foreach ($list_screens as $list_screen) {
112 if (! $collection->contains($list_screen)) {
113 $list_screen->set_read_only(! $repository->is_writable());
114
115 $collection->add($list_screen);
116 }
117 }
118 }
119
120 return $this->sort($collection, $sort);
121 }
122
123 public function save(ListScreen $list_screen): void
124 {
125 $repository = $this->get_writable_repository($list_screen);
126
127 if ($repository) {
128 $repository->save($list_screen);
129 }
130 }
131
132 public function delete(ListScreen $list_screen): void
133 {
134 foreach ($this->get_writable_repositories($list_screen) as $repository) {
135 if ($repository->find($list_screen->get_id())) {
136 $repository->delete($list_screen);
137 break;
138 }
139 }
140 }
141
142 private function get_writable_repository(ListScreen $list_screen): ?ListScreenRepositoryWritable
143 {
144 return $this->get_writable_repositories($list_screen)[0] ?? null;
145 }
146
147 /**
148 * @return ListScreenRepositoryWritable[]
149 */
150 private function get_writable_repositories(ListScreen $list_screen): array
151 {
152 $repositories = [];
153
154 foreach ($this->repositories as $repository) {
155 $match = true;
156
157 if ($repository->has_rules()) {
158 $match = $repository->get_rules()->match([
159 Rule::ID => $list_screen->get_id(),
160 Rule::TYPE => (string)$list_screen->get_table_id(),
161 ]);
162 }
163
164 if ($match && $repository->is_writable()) {
165 $list_screen_repository = $repository->get_list_screen_repository();
166
167 if ($list_screen_repository instanceof ListScreenRepositoryWritable) {
168 $repositories[] = $list_screen_repository;
169 }
170 }
171 }
172
173 return $repositories;
174 }
175
176 }
177