-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDocumentBinder.php
228 lines (194 loc) · 5.43 KB
/
DocumentBinder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace Gt\DomTemplate;
use Gt\Dom\Attr;
use Gt\Dom\Document;
use Gt\Dom\Element;
class DocumentBinder extends Binder {
protected ElementBinder $elementBinder;
protected PlaceholderBinder $placeholderBinder;
protected TableBinder $tableBinder;
protected ListBinder $listBinder;
protected ListElementCollection $templateCollection;
protected BindableCache $bindableCache;
public function __construct(
protected readonly Document $document,
) {}
public function setDependencies(
ElementBinder $elementBinder,
PlaceholderBinder $placeholderBinder,
TableBinder $tableBinder,
ListBinder $listBinder,
ListElementCollection $listElementCollection,
BindableCache $bindableCache,
):void {
$this->elementBinder = $elementBinder;
$this->placeholderBinder = $placeholderBinder;
$this->tableBinder = $tableBinder;
$this->listBinder = $listBinder;
$this->templateCollection = $listElementCollection;
$this->bindableCache = $bindableCache;
}
/**
* Applies the string value of $value anywhere within $context that
* has a data-bind attribute with no specified key.
*/
public function bindValue(
mixed $value,
null|string|Element $context = null
):void {
if(is_string($context)) {
$context = $this->stringToContext($context);
}
$this->bind(null, $value, $context);
}
/**
* Applies the string value of $value to any elements within $context
* that have the data-bind attribute matching the provided key.
*/
public function bindKeyValue(
string $key,
mixed $value,
null|Element|string $context = null,
):void {
if(is_string($context)) {
$context = $this->stringToContext($context);
}
$this->bind($key, $value, $context);
}
/**
* Binds multiple key-value-pairs to any matching elements within
* the $context element.
*/
public function bindData(
mixed $kvp,
null|string|Element $context = null
):void {
if(is_string($context)) {
$context = $this->stringToContext($context);
}
if($this->isIndexedArray($kvp)) {
throw new IncompatibleBindDataException("bindData is only compatible with key-value-pair data, but it was passed an indexed array.");
}
if(is_object($kvp) && method_exists($kvp, "asArray")) {
$kvp = $kvp->asArray();
}
// The $kvp object may be both an object with its own key-value-pairs and
// an iterable object. We can perform the two bind operations here.
$object = null;
if(is_object($kvp)) {
if($this->bindableCache->isBindable($kvp)) {
$object = $kvp;
$kvp = $this->bindableCache->convertToKvp($kvp);
}
}
foreach($kvp ?? [] as $key => $value) {
$this->bindKeyValue($key, $value, $context);
}
if(is_iterable($object)) {
$this->listBinder->bindListData($object, $context ?? $this->document);
}
}
public function bindTable(
mixed $tableData,
null|string|Element $context = null,
?string $bindKey = null
):void {
if(is_string($context)) {
$context = $this->stringToContext($context);
}
$this->tableBinder->bindTableData(
$tableData,
$context ?? $this->document,
$bindKey
);
}
/**
* @param iterable<int, mixed> $listData
*/
public function bindList(
iterable $listData,
null|string|Element $context = null,
?string $templateName = null
):int {
if(is_string($context)) {
$context = $this->stringToContext($context);
}
if(!$context) {
$context = $this->document;
}
return $this->listBinder->bindListData($listData, $context, $templateName);
}
/** @param iterable<int, mixed> $listData */
public function bindListCallback(
iterable $listData,
callable $callback,
null|string|Element $context = null,
?string $templateName = null
):int {
if(!$context) {
$context = $this->document;
}
return $this->listBinder->bindListData(
$listData,
$context,
$templateName,
$callback
);
}
public function cleanupDocument():void {
/**
* @var Attr[] $xpathResult
* @phpstan-ignore varTag.nativeType
*/
$xpathResult = $this->document->evaluate(
"//*/@*[starts-with(name(), 'data-bind')] | //*/@*[starts-with(name(), 'data-list')] | //*/@*[starts-with(name(), 'data-template')] | //*/@*[starts-with(name(), 'data-table-key')] | //*/@*[starts-with(name(), 'data-element')]"
);
$elementsToRemove = [];
foreach($xpathResult as $item) {
$ownerElement = $item->ownerElement;
if($ownerElement->hasAttribute("data-element")) {
if(!$ownerElement->hasAttribute("data-bound")) {
array_push($elementsToRemove, $ownerElement);
}
continue;
}
$ownerElement->removeAttribute($item->name);
}
foreach($this->document->querySelectorAll("[data-element]") as $dataElement) {
$dataElement->removeAttribute("data-element");
}
foreach($this->document->querySelectorAll("[data-bound]") as $dataBound) {
$dataBound->removeAttribute("data-bound");
}
foreach($elementsToRemove as $element) {
$element->remove();
}
}
protected function bind(
?string $key,
mixed $value,
?Element $context = null
):void {
if(!$context) {
$context = $this->document->documentElement;
}
if(is_callable($value) && !is_string($value)) {
$value = call_user_func($value);
}
$this->elementBinder->bind($key, $value, $context);
}
private function isIndexedArray(mixed $data):bool {
if(!is_array($data)) {
return false;
}
foreach(array_keys($data) as $key) {
if(!is_int($key)) {
return false;
}
}
return true;
}
protected function stringToContext(string $context):Element {
return $this->document->querySelector($context);
}
}