Skip to content

Commit 3187c71

Browse files
committed
Component::saveState() fixed compatibility with PHP 7.2
1 parent 4eec7c0 commit 3187c71

File tree

3 files changed

+49
-32
lines changed

3 files changed

+49
-32
lines changed

src/Application/UI/Component.php

+2-32
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,10 @@ public function loadState(array $params): void
144144

145145
/**
146146
* Saves state informations for next request.
147-
* @param ComponentReflection $reflection (internal, used by Presenter)
148147
*/
149-
public function saveState(array &$params, ComponentReflection $reflection = null): void
148+
public function saveState(array &$params): void
150149
{
151-
$reflection = $reflection === null ? $this->getReflection() : $reflection;
152-
foreach ($reflection->getPersistentParams() as $name => $meta) {
153-
if (isset($params[$name])) {
154-
// injected value
155-
156-
} elseif (array_key_exists($name, $params)) { // nulls are skipped
157-
continue;
158-
159-
} elseif ((!isset($meta['since']) || $this instanceof $meta['since']) && isset($this->$name)) {
160-
$params[$name] = $this->$name; // object property value
161-
162-
} else {
163-
continue; // ignored parameter
164-
}
165-
166-
$type = gettype($meta['def']);
167-
if (!ComponentReflection::convertType($params[$name], $type)) {
168-
throw new InvalidLinkException(sprintf(
169-
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
170-
$name,
171-
$this instanceof Presenter ? 'presenter ' . $this->getName() : "component '{$this->getUniqueId()}'",
172-
$type === 'NULL' ? 'scalar' : $type,
173-
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
174-
));
175-
}
176-
177-
if ($params[$name] === $meta['def'] || ($meta['def'] === null && $params[$name] === '')) {
178-
$params[$name] = null; // value transmit is unnecessary
179-
}
180-
}
150+
$this->getReflection()->saveState($this, $params);
181151
}
182152

183153

src/Application/UI/ComponentReflection.php

+37
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,43 @@ public function getPersistentComponents(string $class = null): array
8686
}
8787

8888

89+
/**
90+
* Saves state informations for next request.
91+
*/
92+
public function saveState(Component $component, array &$params): void
93+
{
94+
foreach ($this->getPersistentParams() as $name => $meta) {
95+
if (isset($params[$name])) {
96+
// injected value
97+
98+
} elseif (array_key_exists($name, $params)) { // nulls are skipped
99+
continue;
100+
101+
} elseif ((!isset($meta['since']) || $component instanceof $meta['since']) && isset($component->$name)) {
102+
$params[$name] = $component->$name; // object property value
103+
104+
} else {
105+
continue; // ignored parameter
106+
}
107+
108+
$type = gettype($meta['def']);
109+
if (!self::convertType($params[$name], $type)) {
110+
throw new InvalidLinkException(sprintf(
111+
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
112+
$name,
113+
$component instanceof Presenter ? 'presenter ' . $component->getName() : "component '{$component->getUniqueId()}'",
114+
$type === 'NULL' ? 'scalar' : $type,
115+
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
116+
));
117+
}
118+
119+
if ($params[$name] === $meta['def'] || ($meta['def'] === null && $params[$name] === '')) {
120+
$params[$name] = null; // value transmit is unnecessary
121+
}
122+
}
123+
}
124+
125+
89126
/**
90127
* Is a method callable? It means class is instantiable and method has
91128
* public visibility, is non-static and non-abstract.

src/Application/UI/Presenter.php

+10
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,16 @@ protected function getGlobalState($forClass = null): array
11341134
}
11351135

11361136

1137+
/**
1138+
* Saves state informations for next request.
1139+
* @param ComponentReflection $reflection
1140+
*/
1141+
public function saveState(array &$params, ComponentReflection $reflection = null): void
1142+
{
1143+
($reflection ?: $this->getReflection())->saveState($this, $params);
1144+
}
1145+
1146+
11371147
/**
11381148
* Permanently saves state information for all subcomponents to $this->globalState.
11391149
*/

0 commit comments

Comments
 (0)