PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.0.19
Admin Columns v7.0.19
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 1 month ago Rule 1 month ago Sort 1 month ago Storage 1 month ago Base.php 1 month ago Database.php 1 month ago Filter.php 1 month ago Rule.php 1 month ago Rules.php 1 month ago Sort.php 1 month ago Storage.php 1 month ago Types.php 1 month ago
Storage.php
173 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 /**
20 * @var Storage\ListScreenRepository[]
21 */
22 private array $repositories = [];
23
24 /**
25 * @return Storage\ListScreenRepository[]
26 */
27 public function get_repositories(): array
28 {
29 return array_reverse($this->repositories);
30 }
31
32 public function set_repositories(array $repositories): void
33 {
34 foreach ($repositories as $repository) {
35 if ( ! $repository instanceof ListScreenRepository\Storage\ListScreenRepository) {
36 throw new LogicException('Expected a Storage\ListScreenRepository object.');
37 }
38 }
39
40 $this->repositories = array_reverse($repositories);
41 }
42
43 public function with_repository(string $name, ListScreenRepository\Storage\ListScreenRepository $repository): self
44 {
45 $repositories = $this->get_repositories();
46 $repositories[$name] = $repository;
47
48 $storage = new self();
49 $storage->set_repositories($repositories);
50
51 return $storage;
52 }
53
54 public function has_repository($key): bool
55 {
56 return array_key_exists($key, $this->repositories);
57 }
58
59 public function get_repository($key): Storage\ListScreenRepository
60 {
61 if ( ! $this->has_repository($key)) {
62 throw new LogicException(sprintf('Repository with key %s not found.', $key));
63 }
64
65 return $this->repositories[$key];
66 }
67
68 public function find(ListScreenId $id): ?ListScreen
69 {
70 foreach ($this->repositories as $repository) {
71 $list_screen = $repository->get_list_screen_repository()->find($id);
72
73 if ($list_screen) {
74 $list_screen->set_read_only(! $repository->is_writable());
75
76 return $list_screen;
77 }
78 }
79
80 return null;
81 }
82
83 public function find_all(?Sort $sort = null): ListScreenCollection
84 {
85 $collection = new ListScreenCollection();
86
87 foreach ($this->repositories as $repository) {
88 foreach ($repository->get_list_screen_repository()->find_all() as $list_screen) {
89 if ( ! $collection->contains($list_screen)) {
90 $list_screen->set_read_only(! $repository->is_writable());
91
92 $collection->add($list_screen);
93 }
94 }
95 }
96
97 return $this->sort($collection, $sort);
98 }
99
100 public function find_all_by_table_id(
101 TableId $table_id,
102 ?Sort $sort = null,
103 ?ListScreenStatus $status = null
104 ): ListScreenCollection {
105 $collection = new ListScreenCollection();
106
107 foreach ($this->repositories as $repository) {
108 $list_screens = $repository
109 ->get_list_screen_repository()
110 ->find_all_by_table_id($table_id, null, $status);
111
112 foreach ($list_screens as $list_screen) {
113 if ( ! $collection->contains($list_screen)) {
114 $list_screen->set_read_only(! $repository->is_writable());
115
116 $collection->add($list_screen);
117 }
118 }
119 }
120
121 return $this->sort($collection, $sort);
122 }
123
124 public function save(ListScreen $list_screen): void
125 {
126 $repository = $this->get_writable_repository($list_screen);
127
128 if ($repository) {
129 $repository->save($list_screen);
130 }
131 }
132
133 public function delete(ListScreen $list_screen): void
134 {
135 foreach ($this->get_writable_repositories($list_screen) as $repository) {
136 if ($repository->find($list_screen->get_id())) {
137 $repository->delete($list_screen);
138 break;
139 }
140 }
141 }
142
143 private function get_writable_repository(ListScreen $list_screen): ?ListScreenRepositoryWritable
144 {
145 return $this->get_writable_repositories($list_screen)[0] ?? null;
146 }
147
148 /**
149 * @return ListScreenRepositoryWritable[]
150 */
151 private function get_writable_repositories(ListScreen $list_screen): array
152 {
153 $repositories = [];
154
155 foreach ($this->repositories as $repository) {
156 $match = true;
157
158 if ($repository->has_rules()) {
159 $match = $repository->get_rules()->match([
160 Rule::ID => $list_screen->get_id(),
161 Rule::TYPE => (string)$list_screen->get_table_id(),
162 ]);
163 }
164
165 if ($match && $repository->is_writable()) {
166 $repositories[] = $repository->get_list_screen_repository();
167 }
168 }
169
170 return $repositories;
171 }
172
173 }