Skip to content

Commit

Permalink
added support for persistent parameters with property typehints in PH…
Browse files Browse the repository at this point in the history
…P 7.4 [Closes #230]
  • Loading branch information
dg committed Sep 13, 2019
1 parent af851cb commit d2e8e63
Show file tree
Hide file tree
Showing 4 changed files with 337 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,12 @@ public function loadState(array $params): void
$reflection = $this->getReflection();
foreach ($reflection->getPersistentParams() as $name => $meta) {
if (isset($params[$name])) { // nulls are ignored
$type = gettype($meta['def']);
if (!$reflection->convertType($params[$name], $type)) {
if (!$reflection->convertType($params[$name], $meta['type'])) {
throw new Nette\Application\BadRequestException(sprintf(
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
$name,
$this instanceof Presenter ? 'presenter ' . $this->getName() : "component '{$this->getUniqueId()}'",
$type === 'NULL' ? 'scalar' : $type,
$meta['type'] === 'NULL' ? 'scalar' : $meta['type'],
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function getPersistentParams(string $class = null): array
if (!$rp->isStatic() && self::parseAnnotation($rp, 'persistent')) {
$params[$name] = [
'def' => $default,
'type' => Nette\Utils\Reflection::getPropertyType($rp) ?: gettype($default),
'since' => $isPresenter ? Nette\Utils\Reflection::getPropertyDeclaringClass($rp)->getName() : null,
];
}
Expand Down Expand Up @@ -111,13 +112,12 @@ public function saveState(Component $component, array &$params): void
$params[$name] = $component->$name; // object property value
}

$type = gettype($meta['def']);
if (!self::convertType($params[$name], $type)) {
if (!self::convertType($params[$name], $meta['type'])) {
throw new InvalidLinkException(sprintf(
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
$name,
$component instanceof Presenter ? 'presenter ' . $component->getName() : "component '{$component->getUniqueId()}'",
$type === 'NULL' ? 'scalar' : $type,
$meta['type'] === 'NULL' ? 'scalar' : $meta['type'],
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
));
}
Expand Down
Loading

0 comments on commit d2e8e63

Please sign in to comment.