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

Commit 2182d85

Browse files
committed
Merge branch 'hotfix/195' into develop
Forward port #195
2 parents 1e0bc98 + e5b3af9 commit 2182d85

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
@@ -48,6 +48,8 @@ All notable changes to this project will be documented in this file, in reverse
4848
curly braces in array and string offset access to square brackets
4949
in order to prevent issues under the upcoming PHP 7.4 release.
5050

51+
- [#195](https://github.com/zendframework/zend-view/pull/195) fixes PHP 7.4 compatibility.
52+
5153
## 2.11.2 - 2019-02-19
5254

5355
### 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)