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

Fix for PHP 7.4 #195

Merged
merged 3 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Model/ViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,13 @@ public function clearOptions()
public function getVariable($name, $default = null)
{
$name = (string) $name;
if (array_key_exists($name, $this->variables)) {
return $this->variables[$name];

if (is_array($this->variables)) {
if (array_key_exists($name, $this->variables)) {
return $this->variables[$name];
}
} elseif ($this->variables->offsetExists($name)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to check if this is an ArrayAccess object first ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I can see from the code (construct, setter...) it must be an array or Zend\View\Variables (extends ArrayObject), so I think no additional check is needed here.

return $this->variables->offsetGet($name);
}

return $default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

$vars = $this->vars();
$vars = (array) $this->vars();

if (empty($vars)) {
echo "No object model passed";
} elseif (isset($vars['message'])) {
echo $vars['message'];
} else {
$objKey = current($this->vars())->helper->getObjectKey();
$objKey = current($vars)->helper->getObjectKey();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice,

This BC break on ArrayAccess object in 7.4 is terrible
Already bitten on another project, not easy to catch

echo 'This is an iteration with objectKey: ' . $objKey;
}
52 changes: 52 additions & 0 deletions test/Model/ViewModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,56 @@ public function testCloneWithArray()
$this->assertEquals('foo', $model1->getVariable('a'));
$this->assertEquals('bar', $model2->getVariable('a'));
}

public function variableValue()
{
return [
// variables default expected

// if it is set always get the value
[['foo' => 'bar'], 'baz', 'bar'],
[['foo' => 'bar'], null, 'bar'],
[new ArrayObject(['foo' => 'bar']), 'baz', 'bar'],
[new ArrayObject(['foo' => 'bar']), null, 'bar'],

// if it is null always get null value
[['foo' => null], null, null],
[['foo' => null], 'baz', null],
[new ArrayObject(['foo' => null]), null, null],
[new ArrayObject(['foo' => null]), 'baz', null],

// when it is not set always get default value
[[], 'baz', 'baz'],
[new ArrayObject(), 'baz', 'baz'],
];
}

/**
* @dataProvider variableValue
*
* @param array|ArrayObject $variables
* @param string|null $default
* @param string|null $expected
*/
public function testGetVariableSetByConstruct($variables, $default, $expected)
{
$model = new ViewModel($variables);

self::assertSame($expected, $model->getVariable('foo', $default));
}

/**
* @dataProvider variableValue
*
* @param array|ArrayObject $variables
* @param string|null $default
* @param string|null $expected
*/
public function testGetVariableSetBySetter($variables, $default, $expected)
{
$model = new ViewModel();
$model->setVariables($variables);

self::assertSame($expected, $model->getVariable('foo', $default));
}
}