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

489 bindable cache method OR property name #490

Merged
merged 35 commits into from
Feb 29, 2024
Merged
Changes from all commits
Commits
Show all changes
35 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
acfca16
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Oct 25, 2023
afc9a52
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Nov 10, 2023
5ab216b
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Dec 12, 2023
b026d9a
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Jan 13, 2024
5099b0a
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Feb 4, 2024
fe62f74
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Feb 14, 2024
2ab9024
Merge branch 'master' of github.com:/PhpGt/DomTemplate
g105b Feb 29, 2024
951d9a3
tweak: check method or property of object
g105b Feb 29, 2024
f4cb357
tweak: ignore caught error
g105b Feb 29, 2024
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
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
Loading