Skip to content

Commit 7533211

Browse files
authored
255 bind data object (#256)
* build: upgrade to stable dom release * feature: allow bindData to be used with object that has Bind Attributes closes #255
1 parent 2857bd0 commit 7533211

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/DocumentBinder.php

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Gt\Dom\Document;
55
use Gt\Dom\Element;
66
use Iterator;
7+
use ReflectionObject;
78

89
class DocumentBinder {
910
private ElementBinder $elementBinder;
@@ -66,6 +67,16 @@ public function bindData(
6667
throw new IncompatibleBindDataException("bindData is only compatible with key-value-pair data, but it was passed an indexed array.");
6768
}
6869

70+
if(is_object($kvp) && !is_iterable($kvp)) {
71+
$refObj = new ReflectionObject($kvp);
72+
foreach($refObj->getMethods() as $refMethod) {
73+
foreach($refMethod->getAttributes(Bind::class) as $refAttr) {
74+
$bindName = $refAttr->getArguments()[0];
75+
$kvp->$bindName = $refMethod->getClosure($kvp);
76+
}
77+
}
78+
}
79+
6980
foreach($kvp as $key => $value) {
7081
$this->bindKeyValue($key, $value, $context);
7182
}
@@ -102,6 +113,10 @@ private function bind(
102113
$context = $this->document->documentElement;
103114
}
104115

116+
if(is_callable($value)) {
117+
$value = call_user_func($value);
118+
}
119+
105120
$this->elementBinder->bind($key, $value, $context);
106121
$this->placeholderBinder->bind($key, $value, $context);
107122
}

src/TemplateCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use Gt\Dom\Document;
55
use Gt\Dom\Element;
6-
use Gt\Dom\Text;
76

87
class TemplateCollection {
98
/** @var array<string, TemplateElement> */
@@ -50,6 +49,7 @@ private function extractTemplates(Document $document):void {
5049
);
5150

5251
foreach($dataTemplateArray as $nodePath => $element) {
52+
/** @var Element $element */
5353
$templateElement = new TemplateElement($element);
5454
$name = $templateElement->getTemplateName() ?? $nodePath;
5555
$this->elementKVP[$name] = $templateElement;

test/phpunit/DocumentBinderTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Gt\Dom\HTMLElement\HTMLParagraphElement;
77
use Gt\Dom\HTMLElement\HTMLTableElement;
88
use Gt\Dom\HTMLElement\HTMLTableRowElement;
9+
use Gt\DomTemplate\Bind;
910
use Gt\DomTemplate\DocumentBinder;
1011
use Gt\DomTemplate\IncompatibleBindDataException;
1112
use Gt\DomTemplate\InvalidBindPropertyException;
@@ -375,4 +376,25 @@ public function testBindList():void {
375376
self::assertSame($listItem, $liElementList[$i]->textContent);
376377
}
377378
}
379+
380+
public function testBindData_objectWithAttribute():void {
381+
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE);
382+
$sut = new DocumentBinder($document);
383+
384+
$userObject = new class {
385+
#[Bind("username")]
386+
public function getUser():string {
387+
return "some_username";
388+
}
389+
390+
#[Bind("email")]
391+
public function getEmailAddress():string {
392+
393+
}
394+
};
395+
396+
$sut->bindData($userObject);
397+
self::assertSame("some_username", $document->getElementById("dd1")->textContent);
398+
self::assertSame("[email protected]", $document->getElementById("dd2")->textContent);
399+
}
378400
}

0 commit comments

Comments
 (0)