Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bindable object with nested array #471

Merged
merged 30 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
00d1189
build: upgrade dom requirement and loosen version range
g105b Jul 24, 2022
3fcbcb9
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Aug 14, 2022
2a79afb
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Aug 18, 2022
5d58fc4
docs: update examples
g105b Aug 18, 2022
e3957a8
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Aug 26, 2022
73d0b85
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Sep 21, 2022
3cbb825
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 5, 2022
fadbba7
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 8, 2022
5403158
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 31, 2022
224999b
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 10, 2023
3def753
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 17, 2023
3dbda21
feature: trim whitespace when there are only template children
g105b Jan 17, 2023
a28c12c
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 17, 2023
da35c48
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 23, 2023
b2f8fa5
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 26, 2023
2f51576
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 30, 2023
9655705
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 31, 2023
7745df0
maintenance: phpstorm analysis improvements
g105b Jan 31, 2023
5ab8d14
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Feb 15, 2023
14029b2
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Feb 15, 2023
fa5c45b
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Mar 2, 2023
45007a4
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jul 19, 2023
b47f5b2
tweak: remove data-element attribute
g105b Jul 19, 2023
5f0bbdf
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jul 20, 2023
28f4cee
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jul 28, 2023
9c98cd8
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 12, 2023
8d6c62e
test: isolate bug #443
g105b Oct 18, 2023
8bc4026
fix: nested lists are optionally bound
g105b Oct 18, 2023
8a79275
test: add test for checking there is a list element when binding a list
g105b Oct 18, 2023
79e1be8
test: remove unused function
g105b Oct 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 134 additions & 118 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/BindableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function isBindable(object $object):bool {
if(!$refReturn instanceof ReflectionNamedType) {
continue;
}
$refReturnName = $refReturn?->getName();
$refReturnName = $refReturn->getName();

foreach($refAttributes as $refAttr) {
$bindKey = $this->getBindKey($refAttr, $refMethod);
Expand Down
25 changes: 19 additions & 6 deletions src/ListBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function bindListData(
Document|Element $context,
?string $listItemName = null,
?callable $callback = null,
bool $recursiveCall = false,
):int {
if($context instanceof Document) {
$context = $context->documentElement;
Expand All @@ -34,10 +35,20 @@ public function bindListData(
return 0;
}

$listItem = $this->listElementCollection->get(
$context,
$listItemName
);
try {
$listItem = $this->listElementCollection->get(
$context,
$listItemName
);
}
catch(ListElementNotFoundInContextException $e) {
if($recursiveCall) {
return 0;
}
else {
throw $e;
}
}

$elementBinder = new ElementBinder();
$nestedCount = 0;
Expand All @@ -52,7 +63,8 @@ public function bindListData(
$nestedCount += $this->bindListData(
$listValue,
$t,
$listItemName
$listItemName,
recursiveCall: true
);
continue;
}
Expand Down Expand Up @@ -90,7 +102,8 @@ public function bindListData(
$nestedCount += $this->bindListData(
$value,
$t,
$listItemName
$listItemName,
recursiveCall: true,
);
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/ListElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,4 @@ public function getListItemName():?string {

return $listName;
}

public function getInsertCount():int {
return $this->insertCount;
}
}
48 changes: 48 additions & 0 deletions test/phpunit/ListBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\Bind;
use Gt\DomTemplate\ListBinder;
use Gt\DomTemplate\ListElementNotFoundInContextException;
use Gt\DomTemplate\TableElementNotFoundInContextException;
use Gt\DomTemplate\ListElementCollection;
use Gt\DomTemplate\ListElement;
use Gt\DomTemplate\Test\TestHelper\HTMLPageContent;
use Gt\DomTemplate\Test\TestHelper\Model\Student;
use Gt\DomTemplate\Test\TestHelper\TestData;
use PHPUnit\Framework\TestCase;
use Stringable;
Expand Down Expand Up @@ -703,4 +705,50 @@ public function testBindListData_complexStructure():void {
}
}
}

public function testBindListData_objectWithArrayProperties():void {
$list = [
new Student("Abbey", "Appleby", ["one", "two", "three"]),
new Student("Bruna", "Biltsworth", ["four", "five", "six"]),
new Student("Charlie", "Chudder", ["seven", "eight", "nine"]),
];
$document = new HTMLDocument(HTMLPageContent::HTML_STUDENT_LIST);
$listElementCollection = new ListElementCollection($document);
$sut = new ListBinder($listElementCollection);
$sut->bindListData($list, $document);

self::assertCount(count($list), $document->querySelectorAll("body>ul>li"));
foreach($document->querySelectorAll("dl") as $i => $dlElement) {
$student = $list[$i];
self::assertSame("$student->firstName $student->lastName", $dlElement->querySelector("dd.name")->textContent);
$moduleLiElementList = $dlElement->querySelectorAll("dd.modules li");
$moduleList = $student->getModuleList();
self::assertCount(count($moduleList), $moduleLiElementList);

foreach($moduleLiElementList as $j => $moduleLiElement) {
self::assertSame($moduleList[$j], $moduleLiElement->textContent);
}
}
}

public function testBindListData_objectWithArrayProperties_noNestedList():void {
$list = [
new Student("Abbey", "Appleby", ["one", "two", "three"]),
new Student("Bruna", "Biltsworth", ["four", "five", "six"]),
new Student("Charlie", "Chudder", ["seven", "eight", "nine"]),
];
$document = new HTMLDocument(HTMLPageContent::HTML_STUDENT_LIST_NO_MODULE_LIST);
$listElementCollection = new ListElementCollection($document);
$sut = new ListBinder($listElementCollection);
$numBound = $sut->bindListData($list, $document);
self::assertCount(count($list), $document->querySelectorAll("body>ul>li"));
self::assertSame(count($list), $numBound);
}

public function testBindListData_noListInDocument():void {
$document = new HTMLDocument(HTMLPageContent::HTML_SINGLE_ELEMENT);
$sut = new ListBinder(new ListElementCollection($document));
self::expectException(ListElementNotFoundInContextException::class);
$sut->bindListData(["one", "two", "three"], $document);
}
}
14 changes: 13 additions & 1 deletion test/phpunit/TestHelper/HTMLPageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ class HTMLPageContent {
</ul>
HTML;


const HTML_TWO_LISTS = <<<HTML
<!doctype html>
<div id="favourites">
Expand Down Expand Up @@ -450,6 +449,19 @@ class HTMLPageContent {
</dl>
</li>
</ul>
HTML;

const HTML_STUDENT_LIST_NO_MODULE_LIST = <<<HTML
<!doctype html>
<h1>List of students:</h1>
<ul>
<li data-list>
<dl>
<dt>Student name</dt>
<dd class="name">{{firstName}} {{lastName}}</dd>
</dl>
</li>
</ul>
HTML;

const HTML_LANGUAGE = <<<HTML
Expand Down
18 changes: 18 additions & 0 deletions test/phpunit/TestHelper/Model/Student.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Gt\DomTemplate\Test\TestHelper\Model;

use Gt\DomTemplate\BindGetter;

class Student {
public function __construct(
public readonly string $firstName,
public readonly string $lastName,
private readonly array $moduleList,
) {
}

#[BindGetter]
public function getModuleList():array {
return $this->moduleList;
}
}