Skip to content

Commit 03e9428

Browse files
committed
added support for persistent parameters with property typehints in PHP 7.4 [Closes #230]
1 parent af851cb commit 03e9428

File tree

4 files changed

+337
-19
lines changed

4 files changed

+337
-19
lines changed

src/Application/UI/Component.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,12 @@ public function loadState(array $params): void
135135
$reflection = $this->getReflection();
136136
foreach ($reflection->getPersistentParams() as $name => $meta) {
137137
if (isset($params[$name])) { // nulls are ignored
138-
$type = gettype($meta['def']);
139-
if (!$reflection->convertType($params[$name], $type)) {
138+
if (!$reflection->convertType($params[$name], $meta['type'])) {
140139
throw new Nette\Application\BadRequestException(sprintf(
141140
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
142141
$name,
143142
$this instanceof Presenter ? 'presenter ' . $this->getName() : "component '{$this->getUniqueId()}'",
144-
$type === 'NULL' ? 'scalar' : $type,
143+
$meta['type'] === 'NULL' ? 'scalar' : $meta['type'],
145144
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
146145
));
147146
}

src/Application/UI/ComponentReflection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function getPersistentParams(string $class = null): array
5252
if (!$rp->isStatic() && self::parseAnnotation($rp, 'persistent')) {
5353
$params[$name] = [
5454
'def' => $default,
55+
'type' => Nette\Utils\Reflection::getPropertyType($rp) ?: gettype($default),
5556
'since' => $isPresenter ? Nette\Utils\Reflection::getPropertyDeclaringClass($rp)->getName() : null,
5657
];
5758
}
@@ -111,13 +112,12 @@ public function saveState(Component $component, array &$params): void
111112
$params[$name] = $component->$name; // object property value
112113
}
113114

114-
$type = gettype($meta['def']);
115-
if (!self::convertType($params[$name], $type)) {
115+
if (!self::convertType($params[$name], $meta['type'])) {
116116
throw new InvalidLinkException(sprintf(
117117
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
118118
$name,
119119
$component instanceof Presenter ? 'presenter ' . $component->getName() : "component '{$component->getUniqueId()}'",
120-
$type === 'NULL' ? 'scalar' : $type,
120+
$meta['type'] === 'NULL' ? 'scalar' : $meta['type'],
121121
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
122122
));
123123
}

0 commit comments

Comments
 (0)