Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 43 additions & 0 deletions src/BindFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace GT\DomTemplate;

use Closure;
use ReflectionMethod;

abstract class BindFilter {
/** @var array<class-string, object> */
private array $dependencies = [];

public function setDependency(object $dependency):void {
unset($this->dependencies[$dependency::class]);
$this->dependencies[$dependency::class] = $dependency;
}

/**
* @template T of object
* @param class-string<T> $class
* @return T|null
*/
public function getDependency(string $class):?object {
foreach(array_reverse($this->dependencies) as $dependency) {
if($dependency instanceof $class) {
return $dependency;
}
}

return null;
}

public function getFilter(string $name):?Closure {
if(method_exists(self::class, $name) || !method_exists($this, $name)) {
return null;
}

$method = new ReflectionMethod($this, $name);
if(!$method->isPublic() || $method->isStatic()) {
return null;
}

return $method->getClosure($this);
}
}
12 changes: 12 additions & 0 deletions src/BindFilterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace GT\DomTemplate;

class BindFilterException extends DomTemplateException {
public function __construct(
public readonly string $filterName,
public readonly string $expression,
string $message,
) {
parent::__construct($message);
}
}
11 changes: 11 additions & 0 deletions src/BindFilterNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace GT\DomTemplate;

class BindFilterNotFoundException extends BindFilterException {
public function __construct(string $filterName, string $expression) {
parent::__construct($filterName, $expression,
"Bind filter \"$filterName\" could not be found for expression \"$expression\". "
. "Define a public instance method named $filterName on a registered BindFilter class.",
);
}
}
11 changes: 11 additions & 0 deletions src/BindFilterNotRegisteredException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace GT\DomTemplate;

class BindFilterNotRegisteredException extends BindFilterException {
public function __construct(string $filterName, string $expression) {
parent::__construct($filterName, $expression,
"No BindFilter instance is registered for expression \"$expression\". "
. "Call Binder::registerBindFilter() before binding filtered values.",
);
}
}
66 changes: 66 additions & 0 deletions src/BindFilterPipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace GT\DomTemplate;

use Closure;

class BindFilterPipeline {
/** @var array<class-string<BindFilter>, BindFilter> */
private array $filters = [];

public function register(BindFilter $filter):void {
unset($this->filters[$filter::class]);
$this->filters[$filter::class] = $filter;
}

/** @return array{0:string, 1:mixed} */
public function apply(string $expression, mixed $value):array {
$parts = explode(" ", $expression, 2);
if(isset($parts[1]) && str_starts_with(ltrim($parts[1]), "|")) {
throw new InvalidBindFilterExpressionException("", $expression);
}
$binding = explode("=", $parts[0], 2);
$chain = explode("|", $binding[0]);
$bindKey = array_shift($chain);
if($value instanceof BindableValue) {
$value = $chain ? $value->rawValue : $value->getBindingValue();
}
$callbacks = [];
foreach($chain as $filterName) {
if(!preg_match('/^[a-z][a-z0-9_]*$/iD', $filterName)) {
throw new InvalidBindFilterExpressionException($filterName, $expression);
}
if(!$this->filters) {
throw new BindFilterNotRegisteredException($filterName, $expression);
}
$callback = $this->getFilter($filterName);
if(!$callback) {
throw new BindFilterNotFoundException($filterName, $expression);
}
$callbacks[] = $callback;
}

foreach($callbacks as $callback) {
$value = $callback($value);
}

$unfilteredExpression = $bindKey;
if(isset($binding[1])) {
$unfilteredExpression .= "=" . $binding[1];
}
if(isset($parts[1])) {
$unfilteredExpression .= " " . $parts[1];
}

return [$unfilteredExpression, $value];
}

private function getFilter(string $name):?Closure {
foreach(array_reverse($this->filters) as $filter) {
if($callback = $filter->getFilter($name)) {
return $callback;
}
}

return null;
}
}
82 changes: 40 additions & 42 deletions src/BindableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use ReflectionProperty as RefProperty;
use ReflectionProperty;
use stdClass;
use Stringable;

class BindableCache {
/**
Expand Down Expand Up @@ -119,8 +118,8 @@ private function addMethodBindings(
$methodName = $reflectionMethod->getName();
foreach($this->getBindAttributes($reflectionMethod) as $reflectionAttribute) {
$bindKey = $this->getBindKey($reflectionAttribute, $reflectionMethod);
$attributeCache[$bindKey] = fn(object $object):null|iterable|string
=> $this->nullableStringOrIterable($object, $methodName);
$attributeCache[$bindKey] = fn(object $object):mixed
=> $this->readBindableValue($object, $methodName);
$this->cacheNestedClassName(
$objectKeys,
$bindKey,
Expand All @@ -144,8 +143,8 @@ private function addPropertyBindings(
if(!empty($attributeList)) {
foreach($attributeList as $reflectionAttribute) {
$bindKey = $this->getBindKey($reflectionAttribute);
$attributeCache[$bindKey] = fn(object $object):null|iterable|string
=> $this->nullableStringOrIterable($object, $propertyName);
$attributeCache[$bindKey] = fn(object $object):mixed
=> $this->readBindableValue($object, $propertyName);
$this->cacheNestedClassName(
$objectKeys,
$bindKey,
Expand All @@ -160,9 +159,9 @@ private function addPropertyBindings(
return;
}

$attributeCache[$propertyName] = fn(object $object):null|iterable|string
$attributeCache[$propertyName] = fn(object $object):mixed
=> isset($object->$propertyName)
? $this->nullableStringOrIterable($object, $propertyName)
? $this->readBindableValue($object, $propertyName)
: null;
$this->cacheNestedClassName(
$objectKeys,
Expand Down Expand Up @@ -249,8 +248,8 @@ private function appendNestedBindableKeys(
}

/**
* @param object|array<string, string> $object
* @return array<string, string>
* @param object|array<string, mixed> $object
* @return array<string, mixed>
*/
public function convertToKvp(object|array $object):array {
if(is_array($object)) {
Expand All @@ -261,25 +260,42 @@ public function convertToKvp(object|array $object):array {
return $this->stringifyMap(get_object_vars($object));
}

return array_map(
fn(mixed $value):mixed => $value instanceof BindableValue ? $value->getBindingValue() : $value,
$this->convertToBindableValues($object),
);
}

/**
* Retain original values alongside their ordinary binding representation.
* @param object|array<string, mixed> $object
* @return array<string, mixed>
*/
public function convertToBindableValues(object|array $object):array {
if(is_array($object)) {
return $object;
}

if($object instanceof stdClass) {
return array_map($this->preserveValue(...), get_object_vars($object));
}

if(!$this->isBindable($object)) {
return [];
}

return $this->convertBindableObjectToKvp($object);
}

/** @return array<string, null|string|array<int|string, mixed>> */
private function convertBindableObjectToKvp(object $object):array {
$kvp = [];
$values = [];
foreach($this->bindableClassMap[$object::class] as $key => $valueGetter) {
$targetObject = $this->resolveTargetObject($object, $key);
$value = $targetObject
? $valueGetter($targetObject)
: null;
$kvp[$key] = $this->normalizeValue($value);
$value = $targetObject ? $valueGetter($targetObject) : null;
$values[$key] = $this->preserveValue($value);
}

return $kvp;
return $values;
}

private function preserveValue(mixed $value):mixed {
return is_iterable($value) ? $value : new BindableValue($value);
}

private function resolveTargetObject(object $object, string $key):?object {
Expand Down Expand Up @@ -392,31 +408,13 @@ private function isAttribute(
return strcasecmp($reflectionAttribute->getName(), $className) === 0;
}

/** @return null|string|array<int|string, mixed> */
private function nullableStringOrIterable(
object $object,
string $keyOrMethod,
):null|iterable|string {
private function readBindableValue(object $object, string $keyOrMethod):mixed {
if(method_exists($object, $keyOrMethod)) {
$value = $object->$keyOrMethod();
}
elseif(property_exists($object, $keyOrMethod)) {
$value = $object->$keyOrMethod;
}
else {
return null;
return $object->$keyOrMethod();
}

if(is_scalar($value)) {
return $value;
}
elseif(is_iterable($value)) {
return $value;
}
elseif(is_object($value)) {
if($value instanceof Stringable || method_exists($value, "__toString")) {
return (string)$value;
}
if(property_exists($object, $keyOrMethod)) {
return $object->$keyOrMethod;
}

return null;
Expand Down
25 changes: 25 additions & 0 deletions src/BindableValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace GT\DomTemplate;

use Stringable;

/** @internal Retain the original value without changing ordinary object bindings. */
class BindableValue {
private bool $resolved = false;
private mixed $bindingValue = null;

public function __construct(public readonly mixed $rawValue) {}

public function getBindingValue():mixed {
if(!$this->resolved) {
$this->bindingValue = match(true) {
is_iterable($this->rawValue), is_null($this->rawValue) => $this->rawValue,
is_scalar($this->rawValue), $this->rawValue instanceof Stringable => (string)$this->rawValue,
default => null,
};
$this->resolved = true;
}

return $this->bindingValue;
}
}
2 changes: 2 additions & 0 deletions src/Binder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use GT\Dom\Element;

abstract class Binder {
abstract public function registerBindFilter(BindFilter $filter):void;

abstract public function bindValue(
mixed $value,
null|string|Element $context = null
Expand Down
6 changes: 5 additions & 1 deletion src/DocumentBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function setDependencies(
$this->bindableCache = $bindableCache;
}

public function registerBindFilter(BindFilter $filter):void {
$this->elementBinder->registerBindFilter($filter);
}

/**
* Applies the string value of $value anywhere within $context that
* has a data-bind attribute with no specified key.
Expand Down Expand Up @@ -101,7 +105,7 @@ public function bindData(
if(is_object($kvp)) {
if($this->bindableCache->isBindable($kvp)) {
$object = $kvp;
$kvp = $this->bindableCache->convertToKvp($kvp);
$kvp = $this->bindableCache->convertToBindableValues($kvp);
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/ElementBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class ElementBinder {
private HTMLAttributeCollection $htmlAttributeCollection;
private PlaceholderBinder $placeholderBinder;

public function registerBindFilter(BindFilter $filter):void {
$this->htmlAttributeBinder->registerBindFilter($filter);
}

public function setDebugSource(?string $debugSource):void {
$this->htmlAttributeBinder->setDebugSource($debugSource);
}
Expand All @@ -31,16 +35,17 @@ public function bind(
mixed $value,
Element $context
):void {
if(!is_null($value) && !is_scalar($value) && !is_iterable($value)) {
$value = new BindValue($value);
}

/** @var Element $element */
foreach($this->htmlAttributeCollection->find($context) as $element) {
$this->htmlAttributeBinder->expandAttributes($element);
$this->htmlAttributeBinder->bind($key, $value, $element);
}

if(!is_null($value) && !is_scalar($value) && !is_iterable($value)
&& !$value instanceof BindableValue) {
$value = new BindValue($value);
}

$this->placeholderBinder->bind($key, $value, $context);
}
}
Loading
Loading