Skip to content

Commit

Permalink
Component::saveState() fixed compatibility with PHP 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 14, 2017
1 parent 4eec7c0 commit 3187c71
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
34 changes: 2 additions & 32 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
37 changes: 37 additions & 0 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 3187c71

Please sign in to comment.