Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Normalize zend-view view models by returning result of getVariables()
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Nov 12, 2015
1 parent 1a7c425 commit ecd9e92
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Template/ArrayParametersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ trait ArrayParametersTrait
/**
* Cast params to an array, if possible.
*
* Casts the provided $params argument to an array, using the following rules:
*
* - null values result in an empty array
* - array values are returned verbatim
* - zend-view view models return the result of getVariables()
* - Traversables are cast using iterator_to_array
* - objects that are not zend-view view models nor traversables are cast
* using PHP's type casting
* - scalar values result in an exception
*
* @param mixed $params
* @return array
* @throws Exception\InvalidArgumentException for non-array, non-object parameters.
Expand All @@ -31,6 +41,12 @@ private function normalizeParams($params)
return $params;
}

// Special case for zendframework/zend-view view models.
// Not using typehinting, so as not to require zend-view as a dependency.
if (is_object($params) && method_exists($params, 'getVariables')) {
return $params->getVariables();
}

if ($params instanceof Traversable) {
return iterator_to_array($params);
}
Expand Down

0 comments on commit ecd9e92

Please sign in to comment.