diff --git a/src/Application/UI/Component.php b/src/Application/UI/Component.php index 93fb1d297..9bf54d229 100644 --- a/src/Application/UI/Component.php +++ b/src/Application/UI/Component.php @@ -144,40 +144,10 @@ public function loadState(array $params): void /** * Saves state informations for next request. - * @param ComponentReflection $reflection (internal, used by Presenter) */ - public function saveState(array &$params, ComponentReflection $reflection = null): void + public function saveState(array &$params): void { - $reflection = $reflection === null ? $this->getReflection() : $reflection; - foreach ($reflection->getPersistentParams() as $name => $meta) { - if (isset($params[$name])) { - // injected value - - } elseif (array_key_exists($name, $params)) { // nulls are skipped - continue; - - } elseif ((!isset($meta['since']) || $this instanceof $meta['since']) && isset($this->$name)) { - $params[$name] = $this->$name; // object property value - - } else { - continue; // ignored parameter - } - - $type = gettype($meta['def']); - if (!ComponentReflection::convertType($params[$name], $type)) { - throw new InvalidLinkException(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, - is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name]) - )); - } - - if ($params[$name] === $meta['def'] || ($meta['def'] === null && $params[$name] === '')) { - $params[$name] = null; // value transmit is unnecessary - } - } + $this->getReflection()->saveState($this, $params); } diff --git a/src/Application/UI/ComponentReflection.php b/src/Application/UI/ComponentReflection.php index 10968ae59..169b438d3 100644 --- a/src/Application/UI/ComponentReflection.php +++ b/src/Application/UI/ComponentReflection.php @@ -86,6 +86,43 @@ public function getPersistentComponents(string $class = null): array } + /** + * Saves state informations for next request. + */ + public function saveState(Component $component, array &$params): void + { + foreach ($this->getPersistentParams() as $name => $meta) { + if (isset($params[$name])) { + // injected value + + } elseif (array_key_exists($name, $params)) { // nulls are skipped + continue; + + } elseif ((!isset($meta['since']) || $component instanceof $meta['since']) && isset($component->$name)) { + $params[$name] = $component->$name; // object property value + + } else { + continue; // ignored parameter + } + + $type = gettype($meta['def']); + if (!self::convertType($params[$name], $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, + is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name]) + )); + } + + if ($params[$name] === $meta['def'] || ($meta['def'] === null && $params[$name] === '')) { + $params[$name] = null; // value transmit is unnecessary + } + } + } + + /** * Is a method callable? It means class is instantiable and method has * public visibility, is non-static and non-abstract. diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 067696a2d..6d4fb27e8 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -1134,6 +1134,16 @@ protected function getGlobalState($forClass = null): array } + /** + * Saves state informations for next request. + * @param ComponentReflection $reflection + */ + public function saveState(array &$params, ComponentReflection $reflection = null): void + { + ($reflection ?: $this->getReflection())->saveState($this, $params); + } + + /** * Permanently saves state information for all subcomponents to $this->globalState. */