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

Commit b3ee595

Browse files
committed
Merge branch 'hotfix/195'
Close #195
2 parents 372773e + e5b3af9 commit b3ee595

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ All notable changes to this project will be documented in this file, in reverse
2626
curly braces in array and string offset access to square brackets
2727
in order to prevent issues under the upcoming PHP 7.4 release.
2828

29+
- [#195](https://github.com/zendframework/zend-view/pull/195) fixes PHP 7.4 compatibility.
30+
2931
## 2.11.2 - 2019-02-19
3032

3133
### Added

src/Model/ViewModel.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,13 @@ public function clearOptions()
239239
public function getVariable($name, $default = null)
240240
{
241241
$name = (string) $name;
242-
if (array_key_exists($name, $this->variables)) {
243-
return $this->variables[$name];
242+
243+
if (is_array($this->variables)) {
244+
if (array_key_exists($name, $this->variables)) {
245+
return $this->variables[$name];
246+
}
247+
} elseif ($this->variables->offsetExists($name)) {
248+
return $this->variables->offsetGet($name);
244249
}
245250

246251
return $default;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22

3-
$vars = $this->vars();
3+
$vars = (array) $this->vars();
44

55
if (empty($vars)) {
66
echo "No object model passed";
77
} elseif (isset($vars['message'])) {
88
echo $vars['message'];
99
} else {
10-
$objKey = current($this->vars())->helper->getObjectKey();
10+
$objKey = current($vars)->helper->getObjectKey();
1111
echo 'This is an iteration with objectKey: ' . $objKey;
1212
}

test/Model/ViewModelTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,4 +361,56 @@ public function testCloneWithArray()
361361
$this->assertEquals('foo', $model1->getVariable('a'));
362362
$this->assertEquals('bar', $model2->getVariable('a'));
363363
}
364+
365+
public function variableValue()
366+
{
367+
return [
368+
// variables default expected
369+
370+
// if it is set always get the value
371+
[['foo' => 'bar'], 'baz', 'bar'],
372+
[['foo' => 'bar'], null, 'bar'],
373+
[new ArrayObject(['foo' => 'bar']), 'baz', 'bar'],
374+
[new ArrayObject(['foo' => 'bar']), null, 'bar'],
375+
376+
// if it is null always get null value
377+
[['foo' => null], null, null],
378+
[['foo' => null], 'baz', null],
379+
[new ArrayObject(['foo' => null]), null, null],
380+
[new ArrayObject(['foo' => null]), 'baz', null],
381+
382+
// when it is not set always get default value
383+
[[], 'baz', 'baz'],
384+
[new ArrayObject(), 'baz', 'baz'],
385+
];
386+
}
387+
388+
/**
389+
* @dataProvider variableValue
390+
*
391+
* @param array|ArrayObject $variables
392+
* @param string|null $default
393+
* @param string|null $expected
394+
*/
395+
public function testGetVariableSetByConstruct($variables, $default, $expected)
396+
{
397+
$model = new ViewModel($variables);
398+
399+
self::assertSame($expected, $model->getVariable('foo', $default));
400+
}
401+
402+
/**
403+
* @dataProvider variableValue
404+
*
405+
* @param array|ArrayObject $variables
406+
* @param string|null $default
407+
* @param string|null $expected
408+
*/
409+
public function testGetVariableSetBySetter($variables, $default, $expected)
410+
{
411+
$model = new ViewModel();
412+
$model->setVariables($variables);
413+
414+
self::assertSame($expected, $model->getVariable('foo', $default));
415+
}
364416
}

0 commit comments

Comments
 (0)