Skip to content

Commit

Permalink
Binding null values (#298)
Browse files Browse the repository at this point in the history
* fix: binding null value does nothing on Element and Placeholder binds

* test: null binds by key

* test: null binds by data

* test: null binds by table

* test: null binds by list

* stan: fix type annotations

* build: update dependencies
  • Loading branch information
g105b authored Nov 15, 2021
1 parent 6099617 commit 6b06a89
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 37 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"phpgt/dom": "^3.0.0"
},
"require-dev": {
"phpunit/phpunit": "9.*",
"phpstan/phpstan": "1.*"
"phpunit/phpunit": "~9.5",
"phpstan/phpstan": "~1.1"
},

"autoload": {
Expand Down
56 changes: 28 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/DocumentBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Gt\Dom\Attr;
use Gt\Dom\Document;
use Gt\Dom\Element;
use Gt\Dom\XPathResult;

class DocumentBinder {
private ElementBinder $elementBinder;
Expand Down Expand Up @@ -129,11 +130,11 @@ public function bindListCallback(
}

public function cleanDatasets():void {
/** @var XPathResult<Attr> $xpathResult */
$xpathResult = $this->document->evaluate(
"//*/@*[starts-with(name(), 'data-bind')] | //*/@*[starts-with(name(), 'data-template')]"
);
foreach($xpathResult as $item) {
/** @var Attr $item */
$item->ownerElement->removeAttribute($item->name);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ElementBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function bind(
mixed $value,
Element $context
):void {
/** @var Element $element */
foreach($this->htmlAttributeCollection->find($context) as $element) {
/** @var Element $element */
$this->htmlAttributeBinder->expandAttributes($element);
$this->htmlAttributeBinder->bind($key, $value, $element);
}
Expand Down
4 changes: 4 additions & 0 deletions src/HTMLAttributeBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public function bind(
mixed $value,
Document|Element $element
):void {
if(is_null($value)) {
return;
}

if($element instanceof Document) {
$element = $element->documentElement;
}
Expand Down
4 changes: 4 additions & 0 deletions src/ListBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function bindListData(
);
}

if(is_null($listItem)) {
continue;
}

if($this->isKVP($listItem)) {
$elementBinder->bind(null, $listKey, $t);

Expand Down
6 changes: 5 additions & 1 deletion src/PlaceholderBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function bind(
mixed $value,
Node|Document $context
):void {
if(is_null($value)) {
return;
}

if($context instanceof Document) {
$context = $context->documentElement;
}
Expand All @@ -31,10 +35,10 @@ public function bind(
/** @var Text|Attr $text */
$text = $attributeOrText;
if($text instanceof Attr) {
/** @var Text $text */
$text = $text->firstChild;
}

/** @var Text $text */
$placeholder = $text->splitText(
strpos($text->data, "{{")
);
Expand Down
5 changes: 2 additions & 3 deletions src/TableBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ public function bindTableData(
}

$headerRow = array_shift($tableData);
/** @var HTMLTableElement $table */
foreach($tableArray as $table) {
/** @var HTMLTableElement $table */

$allowedHeaders = $headerRow;

$tHead = $table->tHead;
Expand Down Expand Up @@ -95,7 +94,7 @@ public function bindTableData(
}

$cellElement = $tr->ownerDocument->createElement($cellTypeToCreate);
$cellElement->textContent = $columnValue;
$cellElement->textContent = $columnValue ?? "";
$tr->appendChild($cellElement);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TemplateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public function get(

private function extractTemplates(Document $document):void {
$dataTemplateArray = [];
/** @var Element $element */
foreach($document->querySelectorAll("[data-template]") as $element) {
/** @var Element $element */
$nodePath = (string)(new NodePathCalculator($element));
$templateElement = new TemplateElement($element);
$key = $templateElement->getTemplateName() ?? $nodePath;
Expand Down
Loading

0 comments on commit 6b06a89

Please sign in to comment.