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

Commit

Permalink
Merge branch 'feature/view-model-support' into develop
Browse files Browse the repository at this point in the history
Forward port #185
  • Loading branch information
mwillbanks committed Nov 20, 2015
2 parents fd8e884 + e05a0b1 commit 49e41ce
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Third release candidate.

### Added

- Nothing.
- [#185](https://github.com/zendframework/zend-expressive/pull/185)
Support casting zend-view models to arrays

### Deprecated

Expand Down
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
77 changes: 77 additions & 0 deletions test/Template/ArrayParametersTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZendTest\Expressive\Template;

use ArrayIterator;
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
use Zend\Expressive\Exception\InvalidArgumentException;

class ArrayParametersTraitTest extends TestCase
{
public function setUp()
{
$this->subject = new TestAsset\ArrayParameters();
}

public function testNullParamsAreReturnedAsEmptyArray()
{
$this->assertEquals([], $this->subject->normalize(null));
}

public function testArrayParamsAreReturnedVerbatim()
{
$params = ['foo' => 'bar'];
$this->assertSame($params, $this->subject->normalize($params));
}

public function testExtractsVariablesFromObjectsImplementingGetVariables()
{
$params = ['foo' => 'bar'];
$model = new TestAsset\ViewModel($params);
$this->assertSame($params, $this->subject->normalize($model));
}

public function testCastsTraversablesToArrays()
{
$params = ['foo' => 'bar'];
$model = new ArrayIterator($params);
$this->assertSame($params, $this->subject->normalize($model));
}

public function testCastsObjectsToArrays()
{
$params = ['foo' => 'bar'];
$model = (object) $params;
$this->assertSame($params, $this->subject->normalize($model));
}

public function nonNullScalarParameters()
{
// @codingStandardsIgnoreStart
// [scalar, expected exception string]
return [
'true' => [true, 'bool'],
'false' => [false, 'bool'],
'zero' => [0, 'int'],
'int' => [1, 'int'],
'zero-float' => [0.0, 'double'],
'float' => [1.1, 'double'],
'string' => ['view param', 'string'],
];
// @codingStandardsIgnoreEnd
}

/**
* @dataProvider nonNullScalarParameters
*/
public function testNonNullScalarsRaiseAnException($scalar, $expectedString)
{
$this->setExpectedException(InvalidArgumentException::class, $expectedString);
$this->subject->normalize($scalar);
}
}
19 changes: 19 additions & 0 deletions test/Template/TestAsset/ArrayParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZendTest\Expressive\Template\TestAsset;

use Zend\Expressive\Template\ArrayParametersTrait;

class ArrayParameters
{
use ArrayParametersTrait;

public function normalize($params)
{
return $this->normalizeParams($params);
}
}
22 changes: 22 additions & 0 deletions test/Template/TestAsset/ViewModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZendTest\Expressive\Template\TestAsset;

class ViewModel
{
private $variables;

public function __construct(array $variables)
{
$this->variables = $variables;
}

public function getVariables()
{
return $this->variables;
}
}

0 comments on commit 49e41ce

Please sign in to comment.