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

Fix setter value type with prado tpl/Page #837

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
27 changes: 27 additions & 0 deletions framework/Web/UI/TTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Prado\Prado;
use Prado\TComponent;
use Prado\TPropertyValue;
use Prado\Web\Javascripts\TJavaScriptLiteral;
use Prado\Exceptions\TConfigurationException;
use Prado\Exceptions\TException;
Expand Down Expand Up @@ -441,6 +442,32 @@ protected function configureProperty($component, $name, $value)
}
}
$setter = 'set' . $name;
if (method_exists($component, $setter)) {
Copy link
Member

@belisoful belisoful Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be updated to be behavior aware with #825

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this going to be $component->hasMethod($setter)

if ($reflector = new \ReflectionClass($component)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setter/property may not be in the component but in a behavior modifying the component. looping through each behavior would be needed too but that requires #826.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looping all the behaviors from here sounds time expensive (but can be fixed with some caching) but also a bit incorrect from an OOP point of view.
Maybe adding a way to get the reflected method from inside TComponent itself could be an idea. This would also move the reflection cache inside the class itself, so that other callers can benefit from it.

try {
$params = $reflector->getMethod($setter)->getParameters();
if (
!empty($params) &&
isset($params[0]) &&
!empty($params[0]->getType()) &&
$params[0]->getType() instanceof \ReflectionNamedType
) {
switch ($params[0]->getType()->getName()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

between getting the "new reflectionclass()" and this type-name, caching the class/property/type into a static would greatly speed things up.

case 'bool':
$value = TPropertyValue::ensureBoolean($value);
break;
case 'int':
$value = TPropertyValue::ensureInteger($value);
break;
case 'float':
$value = TPropertyValue::ensureFloat($value);
break;
}
ctrlaltca marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (\ReflectionException $e) {
}
}
}
$component->$setter($value);
}
}
Expand Down