-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: upgrade dom requirement and loosen version range * docs: update examples * feature: trim whitespace when there are only template children closes #363 * maintenance: phpstorm analysis improvements * tweak: remove data-element attribute * feature: implement ComponentBinder and abstract Binder class for #470 * fix: bindable cache allows nested nullable objects closes #474 * refactor: all domtemplate classes set their dependencies outside of the constructor for #470 * tweak: stricter reflection type checking * wip: better exception on missing list element * tweak: handle nullable iterables closes #473 * tweak: handle binding of outer list item * tidy: improve types and coverage * ci: php 8.2 * ci: phpstan level 6
- Loading branch information
Showing
21 changed files
with
823 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace Gt\DomTemplate; | ||
|
||
use Gt\Dom\Element; | ||
|
||
abstract class Binder { | ||
abstract public function bindValue( | ||
mixed $value, | ||
?Element $context = null | ||
):void; | ||
|
||
/** | ||
* Applies the string value of $value to any elements within $context | ||
* that have the data-bind attribute matching the provided key. | ||
*/ | ||
abstract public function bindKeyValue( | ||
string $key, | ||
mixed $value, | ||
?Element $context = null | ||
):void; | ||
|
||
/** | ||
* Binds multiple key-value-pairs to any matching elements within | ||
* the $context element. | ||
*/ | ||
abstract public function bindData( | ||
mixed $kvp, | ||
?Element $context = null | ||
):void; | ||
|
||
abstract public function bindTable( | ||
mixed $tableData, | ||
?Element $context = null, | ||
?string $bindKey = null | ||
):void; | ||
|
||
/** | ||
* @param iterable<int, mixed> $listData | ||
*/ | ||
abstract public function bindList( | ||
iterable $listData, | ||
?Element $context = null, | ||
?string $templateName = null | ||
):int; | ||
|
||
/** @param iterable<int, mixed> $listData */ | ||
abstract public function bindListCallback( | ||
iterable $listData, | ||
callable $callback, | ||
?Element $context = null, | ||
?string $templateName = null | ||
):int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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, | ||
?Element $context = null, | ||
?string $templateName = null | ||
):int { | ||
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>." | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?php | ||
namespace Gt\DomTemplate; | ||
|
||
class ComponentDoesNotContainContextException extends DomTemplateException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.