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

Commit e20147f

Browse files
committed
Merge pull request #195 from remicollet/issue-php74
Fix for PHP 7.4
2 parents 372773e + 81ed6ca commit e20147f

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/Model/ViewModel.php

+7-2
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;
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

+52
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)