From 5831b9061a3f40898b36aafbbc23a6d1893934b3 Mon Sep 17 00:00:00 2001 From: tr Date: Thu, 2 Aug 2012 02:11:39 +0100 Subject: [PATCH 1/4] need to check that the variables aren't already an array. otherwise, for example, forms become an array of elements. --- src/Model/ViewModel.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Model/ViewModel.php b/src/Model/ViewModel.php index 4af8f3e6..7ad0f1d7 100644 --- a/src/Model/ViewModel.php +++ b/src/Model/ViewModel.php @@ -262,9 +262,10 @@ public function setVariables($variables, $overwrite = false) } if ($overwrite) { - if (!is_object($variables) && !$variables instanceof ArrayAccess) { + if (!is_object($variables) && !$variables instanceof ArrayAccess && !is_array($variables)) { $variables = ArrayUtils::iteratorToArray($variables); } + $this->variables = $variables; return $this; } From f7e56a1a9c5de6476c91e455fc6201eace4655c1 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Thu, 2 Aug 2012 16:23:13 +0200 Subject: [PATCH 2/4] Fix ViewModel::setVariables with objects as vars A typo in the check to cast objects to array accessible variables caused the tests to pass, but causes problems with a set of variables in an array to be casted to arrays. --- src/Model/ViewModel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/ViewModel.php b/src/Model/ViewModel.php index 7ad0f1d7..6078feb7 100644 --- a/src/Model/ViewModel.php +++ b/src/Model/ViewModel.php @@ -262,7 +262,7 @@ public function setVariables($variables, $overwrite = false) } if ($overwrite) { - if (!is_object($variables) && !$variables instanceof ArrayAccess && !is_array($variables)) { + if (is_object($variables) && !$variables instanceof ArrayAccess) { $variables = ArrayUtils::iteratorToArray($variables); } From b7ae88c14feaa6fa6ce6b1ccbd318bbc6fe4d758 Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Thu, 2 Aug 2012 16:50:44 +0200 Subject: [PATCH 3/4] Create test for objects casted to empty array Objects in variables array implementing Traversable, but not ArrayAccess are converted into an empty array. The test shows this and the ViewModel is patched to specifically cast only the right objects to an array. --- test/Model/TestAsset/Variable.php | 36 +++++++++++++++++++++++++++++++ test/Model/ViewModelTest.php | 8 +++++++ 2 files changed, 44 insertions(+) create mode 100644 test/Model/TestAsset/Variable.php diff --git a/test/Model/TestAsset/Variable.php b/test/Model/TestAsset/Variable.php new file mode 100644 index 00000000..719b0f48 --- /dev/null +++ b/test/Model/TestAsset/Variable.php @@ -0,0 +1,36 @@ +assertSame(iterator_to_array($options), $model->getOptions()); } + public function testAllowsPassingNonArrayAccessObjectsAsArrayInConstructor() + { + $vars = array('foo' => new Variable); + $model = new ViewModel($vars); + $this->assertSame($vars, $model->getVariables()); + } + public function testCanSetVariablesSingly() { $model = new ViewModel(array('foo' => 'bar')); From a48469698a9e1a78d354d3f186e2efdb959c85eb Mon Sep 17 00:00:00 2001 From: Jurian Sluiman Date: Thu, 2 Aug 2012 17:06:08 +0200 Subject: [PATCH 4/4] Fix CS for Variable test asset --- test/Model/TestAsset/Variable.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/Model/TestAsset/Variable.php b/test/Model/TestAsset/Variable.php index 719b0f48..f1569105 100644 --- a/test/Model/TestAsset/Variable.php +++ b/test/Model/TestAsset/Variable.php @@ -14,23 +14,23 @@ class Variable implements Iterator { - public function current() - { - } + public function current() + { + } - public function key() - { - } + public function key() + { + } - public function next() - { - } + public function next() + { + } - public function rewind() - { - } + public function rewind() + { + } - public function valid() - { - } + public function valid() + { + } } \ No newline at end of file