Skip to content

Commit

Permalink
489 bindable cache method OR property name (#490)
Browse files Browse the repository at this point in the history
* 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

* tweak: check method or property of object
closes #489

* tweak: ignore caught error
  • Loading branch information
g105b authored Feb 29, 2024
1 parent 743c005 commit 716acb1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/BindableCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\DomTemplate;

use Closure;
use Error;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -70,7 +71,8 @@ public function isBindable(object $object):bool {

foreach($refAttributes as $refAttr) {
$bindKey = $this->getBindKey($refAttr, $refMethod);
$attributeCache[$bindKey] = fn(object $object):null|iterable|string => $this->nullableStringOrIterable($object->$methodName());
$attributeCache[$bindKey] = fn(object $object):null|iterable|string => $this->nullableStringOrIterable($object, $methodName);

if(class_exists($refReturnName)) {
$cacheObjectKeys[$bindKey] = $refReturnName;
}
Expand All @@ -85,7 +87,7 @@ public function isBindable(object $object):bool {
$bindKey = $this->getBindKey($refAttr);
// TODO: Test for object type in object property.
$attributeCache[$bindKey]
= fn(object $object, $key):null|iterable|string => $this->nullableStringOrIterable($object->$propName);
= fn(object $object, $key):null|iterable|string => $this->nullableStringOrIterable($object, $propName);
}
}
elseif($refProp->isPublic()) {
Expand All @@ -99,7 +101,7 @@ public function isBindable(object $object):bool {
$attributeCache[$bindKey] =
fn(object $object, $key):null|iterable|string =>
isset($object->$key)
? $this->nullableStringOrIterable($object->$key)
? $this->nullableStringOrIterable($object, $key)
: null;

if($refTypeName && class_exists($refTypeName)) {
Expand Down Expand Up @@ -192,7 +194,11 @@ public function convertToKvp(object|array $object):array {
$bindFunc = "get" . ucfirst($propName);
if($objectToExtract) {
if(property_exists($objectToExtract, $propName)) {
$objectToExtract = $objectToExtract->$propName;
try {
$objectToExtract = $objectToExtract->$propName;
}
// @phpstan-ignore-next-line
catch(Error) {}
}
elseif(method_exists($objectToExtract, $bindFunc)) {
$objectToExtract = $objectToExtract->$bindFunc();
Expand Down Expand Up @@ -249,7 +255,17 @@ private function getBindKey(
}

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

if(is_scalar($value)) {
return $value;
}
Expand Down

0 comments on commit 716acb1

Please sign in to comment.