From 5d3fdfc9d6e7c51298f74e2bb29ea64f0a2c26ac Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Fri, 7 Oct 2011 16:11:50 +0200 Subject: [PATCH] Add support for NULL values in Set::format(). Add test case. Fixes #2076. --- cake/libs/set.php | 2 +- cake/tests/cases/libs/set.test.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 71a8a604f..452ec9ff2 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -327,7 +327,7 @@ class Set { for ($j = 0; $j < $count; $j++) { $args = array(); for ($i = 0; $i < $count2; $i++) { - if (isset($data[$i][$j])) { + if (array_key_exists($j, $data[$i])) { $args[] = $data[$i][$j]; } } diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 3ca78ba42..3fe0e7f16 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -2255,6 +2255,26 @@ class SetTest extends CakeTestCase { $this->assertEqual($result, $expected); } +/** + * testFormattingNullValues method + * + * @return void + */ + public function testFormattingNullValues() { + $data = array( + array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')), + array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null)), + array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null))); + + $result = Set::format($data, '%s', array('{n}.Person.something')); + $expected = array('42', '', ''); + $this->assertEqual($expected, $result); + + $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something')); + $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, '); + $this->assertEqual($expected, $result); + } + /** * testCountDim method *