-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathListBinder.php
186 lines (162 loc) · 4.11 KB
/
ListBinder.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
<?php
namespace Gt\DomTemplate;
use DateTimeInterface;
use Gt\Dom\Document;
use Gt\Dom\Element;
use Iterator;
use IteratorAggregate;
use Stringable;
class ListBinder {
private ElementBinder $elementBinder;
private ListElementCollection $listElementCollection;
private BindableCache $bindableCache;
/**
* @noinspection PhpPropertyOnlyWrittenInspection
* @phpstan-ignore-next-line
*/
private TableBinder $tableBinder;
public function setDependencies(
ElementBinder $elementBinder,
ListElementCollection $listElementCollection,
BindableCache $bindableCache,
TableBinder $tableBinder,
):void {
$this->elementBinder = $elementBinder;
$this->listElementCollection = $listElementCollection;
$this->bindableCache = $bindableCache;
$this->tableBinder = $tableBinder;
}
/** @param iterable<int|string,mixed> $listData */
public function bindListData(
iterable $listData,
Document|Element $context,
?string $listItemName = null,
?callable $callback = null,
bool $recursiveCall = false,
):int {
if($context instanceof Document) {
$context = $context->documentElement;
}
if($this->isEmpty($listData)) {
$this->clearListItemParentHTML($context, $listItemName);
return 0;
}
try {
$listItem = $this->listElementCollection->get(
$context,
$listItemName
);
}
catch(ListElementNotFoundInContextException $e) {
if($recursiveCall) {
return 0;
}
else {
throw $e;
}
}
$elementBinder = $this->elementBinder;
$nestedCount = 0;
$i = -1;
foreach($listData as $listKey => $listValue) {
$i++;
$t = $listItem->insertListItem();
// If the $listValue's first value is iterable, then treat this as a nested list.
if($this->isNested($listValue)) {
$elementBinder->bind(null, $listKey, $t);
$this->bindListData(
$listValue,
$t,
);
foreach($this->bindableCache->convertToKvp($listValue) as $key => $value) {
$elementBinder->bind($key, $value, $t);
}
continue;
}
if(is_object($listValue) && method_exists($listValue, "asArray")) {
$listValue = $listValue->asArray();
}
elseif(is_object($listValue) && !is_iterable($listValue)) {
if($this->bindableCache->isBindable($listValue)) {
$listValue = $this->bindableCache->convertToKvp($listValue);
}
}
if($callback) {
$listValue = call_user_func(
$callback,
$t,
$listValue,
$listKey,
);
}
if(is_null($listValue)) {
continue;
}
if($this->isKVP($listValue)) {
$elementBinder->bind(null, $listKey, $t);
foreach($listValue as $key => $value) {
$elementBinder->bind($key, $value, $t);
if($this->isNested($value)) {
$elementBinder->bind(null, $key, $t);
$nestedCount += $this->bindListData(
$value,
$t,
$listItemName,
recursiveCall: true,
);
}
}
}
else {
$elementBinder->bind(null, $listValue, $t);
}
}
return $nestedCount + $i + 1;
}
/** @param iterable<int|string,mixed> $listData */
private function isEmpty(iterable $listData):bool {
if(is_array($listData)) {
return is_null(array_key_first($listData));
}
else {
/** @var Iterator|IteratorAggregate $iterator */
$iterator = $listData;
if($iterator instanceof IteratorAggregate) {
$iterator = $iterator->getIterator();
/** @var Iterator $iterator */
}
$iterator->rewind();
return !$iterator->valid();
}
}
private function clearListItemParentHTML(
Element $context,
?string $listName
):void {
$listElement = $this->listElementCollection->get($context, $listName);
$parent = $listElement->getListItemParent();
$parent->innerHTML = trim($parent->innerHTML ?? "");
}
private function isKVP(mixed $item):bool {
if(is_scalar($item)) {
return false;
}
if($item instanceof DateTimeInterface) {
return false;
}
if($item instanceof Stringable) {
return false;
}
return true;
}
private function isNested(mixed $item):bool {
if(is_array($item)) {
$key = array_key_first($item);
return is_int($key) || (isset($item[$key]) && is_iterable($item[$key]));
}
elseif(is_iterable($item)) {
return true;
}
return false;
}
}