-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathComponentBinder.php
64 lines (53 loc) · 1.56 KB
/
ComponentBinder.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
<?php
namespace Gt\DomTemplate;
use Gt\Dom\Document;
use Gt\Dom\Element;
class ComponentBinder extends DocumentBinder {
private Element $componentElement;
public function setComponentBinderDependencies(Element $componentElement):void {
$this->componentElement = $componentElement;
}
public function bindList(
iterable $listData,
null|string|Element $context = null,
?string $templateName = null
):int {
if(is_string($context)) {
$context = $this->stringToContext($context);
}
if($context) {
$this->checkElementContainedWithinComponent($context);
}
else {
$context = $this->componentElement;
}
return parent::bindList($listData, $context, $templateName);
}
protected function bind(
?string $key,
mixed $value,
?Element $context = null
):void {
if($context) {
$this->checkElementContainedWithinComponent($context);
}
else {
$context = $this->componentElement;
}
if(is_callable($value) && !is_string($value)) {
$value = call_user_func($value);
}
$this->elementBinder->bind($key, $value, $context);
$this->placeholderBinder->bind($key, $value, $context);
}
private function checkElementContainedWithinComponent(Element $context):void {
if($this->componentElement !== $context && !$this->componentElement->contains($context)) {
throw new ComponentDoesNotContainContextException(
"<{$this->componentElement->tagName}> does not contain requested <$context->tagName>."
);
}
}
protected function stringToContext(string $context):Element {
return $this->componentElement->querySelector($context);
}
}