1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Page\Adapter; |
4
|
|
|
|
5
|
|
|
use Stitcher\Exception\InvalidCollectionAdapter; |
6
|
|
|
use Stitcher\Page\Adapter; |
7
|
|
|
use Stitcher\Configureable; |
8
|
|
|
use Stitcher\Variable\VariableParser; |
9
|
|
|
|
10
|
|
|
class CollectionAdapter implements Adapter, Configureable |
11
|
|
|
{ |
12
|
|
|
/** @var null */ |
13
|
|
|
private static $filterId = null; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
private $entries = []; |
17
|
|
|
|
18
|
|
|
/** @var mixed */ |
19
|
|
|
private $parameter; |
20
|
|
|
|
21
|
|
|
/** @var mixed */ |
22
|
|
|
private $variable; |
23
|
|
|
|
24
|
|
|
/** @var \Stitcher\Variable\VariableParser */ |
25
|
|
|
private $variableParser; |
26
|
|
|
|
27
|
16 |
|
public function __construct( |
28
|
|
|
array $adapterConfiguration, |
29
|
|
|
VariableParser $variableParser |
30
|
|
|
) { |
31
|
16 |
|
if (! $this->isValidConfiguration($adapterConfiguration)) { |
32
|
|
|
throw InvalidCollectionAdapter::create(); |
33
|
|
|
} |
34
|
|
|
|
35
|
16 |
|
$this->variable = $adapterConfiguration['variable']; |
36
|
16 |
|
$this->parameter = $adapterConfiguration['parameter']; |
37
|
16 |
|
$this->variableParser = $variableParser; |
38
|
16 |
|
} |
39
|
|
|
|
40
|
16 |
|
public static function make( |
41
|
|
|
array $adapterConfiguration, |
42
|
|
|
VariableParser $variableParser |
43
|
|
|
): CollectionAdapter { |
44
|
16 |
|
return new self($adapterConfiguration, $variableParser); |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
public static function setFilterId(?string $filterId) |
48
|
|
|
{ |
49
|
5 |
|
self::$filterId = $filterId; |
50
|
5 |
|
} |
51
|
|
|
|
52
|
15 |
|
public function transform(array $pageConfiguration): array |
53
|
|
|
{ |
54
|
15 |
|
$this->entries = $this->getEntries($pageConfiguration); |
|
|
|
|
55
|
|
|
|
56
|
15 |
|
$collectionPageConfiguration = []; |
57
|
|
|
|
58
|
15 |
|
while ($entry = current($this->entries)) { |
59
|
15 |
|
$entryId = key($this->entries); |
60
|
|
|
|
61
|
15 |
|
if (self::$filterId !== null && self::$filterId !== $entryId) { |
62
|
1 |
|
next($this->entries); |
63
|
|
|
|
64
|
1 |
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
15 |
|
$entryConfiguration = $this->createEntryConfiguration( |
68
|
15 |
|
$pageConfiguration, |
69
|
15 |
|
$entryId, |
70
|
15 |
|
$entry |
71
|
|
|
); |
72
|
|
|
|
73
|
15 |
|
$collectionPageConfiguration[$entryConfiguration['id']] = $entryConfiguration; |
74
|
|
|
|
75
|
15 |
|
next($this->entries); |
76
|
|
|
} |
77
|
|
|
|
78
|
15 |
|
return $collectionPageConfiguration; |
79
|
|
|
} |
80
|
|
|
|
81
|
16 |
|
public function isValidConfiguration($subject): bool |
82
|
|
|
{ |
83
|
16 |
|
return is_array($subject) && isset($subject['variable']) && isset($subject['parameter']); |
84
|
|
|
} |
85
|
|
|
|
86
|
15 |
|
protected function getEntries($pageConfiguration): ?array |
87
|
|
|
{ |
88
|
15 |
|
$variable = $pageConfiguration['variables'][$this->variable] ?? null; |
89
|
|
|
|
90
|
15 |
|
$entries = $this->variableParser->parse($variable) ?? $variable; |
91
|
|
|
|
92
|
15 |
|
return $entries; |
93
|
|
|
} |
94
|
|
|
|
95
|
15 |
|
protected function createEntryConfiguration(array $pageConfiguration, $entryId, $entry): array |
96
|
|
|
{ |
97
|
15 |
|
$entryConfiguration = $pageConfiguration; |
98
|
|
|
|
99
|
15 |
|
$parsedEntryId = str_replace('{' . $this->parameter . '}', $entryId, $pageConfiguration['id']); |
100
|
|
|
|
101
|
15 |
|
$entryConfiguration['id'] = $parsedEntryId; |
102
|
|
|
|
103
|
15 |
|
$entryConfiguration['variables'][$this->variable] = $entry; |
104
|
|
|
|
105
|
15 |
|
$entryConfiguration['variables']['meta'] = array_merge( |
106
|
15 |
|
$entryConfiguration['variables']['meta'] ?? [], |
107
|
15 |
|
$this->createMetaVariable($entryConfiguration) |
108
|
|
|
); |
109
|
|
|
|
110
|
15 |
|
$entryConfiguration['variables']['browse'] = |
111
|
15 |
|
$entryConfiguration['variables']['browse'] |
112
|
15 |
|
?? $this->createBrowseData(); |
113
|
|
|
|
114
|
15 |
|
unset($entryConfiguration['config']['collection']); |
115
|
|
|
|
116
|
15 |
|
return $entryConfiguration; |
117
|
|
|
} |
118
|
|
|
|
119
|
15 |
|
protected function createMetaVariable(array $entryConfiguration): array |
120
|
|
|
{ |
121
|
15 |
|
$meta = []; |
122
|
|
|
|
123
|
15 |
|
if ($title = $this->getTitleMeta($entryConfiguration)) { |
124
|
13 |
|
$meta['title'] = $title; |
125
|
|
|
} |
126
|
|
|
|
127
|
15 |
|
if ($description = $this->getDescriptionMeta($entryConfiguration)) { |
128
|
1 |
|
$meta['description'] = $description; |
129
|
|
|
} |
130
|
|
|
|
131
|
15 |
|
return $meta; |
132
|
|
|
} |
133
|
|
|
|
134
|
15 |
|
protected function getTitleMeta(array $entryConfiguration): ?string |
135
|
|
|
{ |
136
|
|
|
$title = |
137
|
15 |
|
$entryConfiguration['variables'][$this->variable]['meta']['title'] |
138
|
15 |
|
?? $entryConfiguration['variables'][$this->variable]['title'] |
139
|
15 |
|
?? null; |
140
|
|
|
|
141
|
15 |
|
return $title; |
142
|
|
|
} |
143
|
|
|
|
144
|
15 |
|
protected function getDescriptionMeta(array $entryConfiguration): ?string |
145
|
|
|
{ |
146
|
|
|
$description = |
147
|
15 |
|
$entryConfiguration['variables'][$this->variable]['meta']['description'] |
148
|
15 |
|
?? $entryConfiguration['variables'][$this->variable]['description'] |
149
|
15 |
|
?? null; |
150
|
|
|
|
151
|
15 |
|
return $description; |
152
|
|
|
} |
153
|
|
|
|
154
|
15 |
|
protected function createBrowseData(): array |
155
|
|
|
{ |
156
|
15 |
|
$browse = []; |
157
|
|
|
|
158
|
15 |
|
$prev = prev($this->entries); |
159
|
|
|
|
160
|
15 |
|
if (! $prev) { |
161
|
15 |
|
reset($this->entries); |
162
|
|
|
} else { |
163
|
14 |
|
$browse['prev'] = $prev; |
164
|
|
|
|
165
|
14 |
|
next($this->entries); |
166
|
|
|
} |
167
|
|
|
|
168
|
15 |
|
$next = next($this->entries); |
169
|
|
|
|
170
|
15 |
|
if ($next) { |
171
|
15 |
|
$browse['next'] = $next; |
172
|
|
|
} |
173
|
|
|
|
174
|
15 |
|
prev($this->entries); |
175
|
|
|
|
176
|
15 |
|
return $browse; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..