diff --git a/lib/Cake/Test/Case/Utility/Set2Test.php b/lib/Cake/Test/Case/Utility/Set2Test.php index 1c092f938..cdf6bcae3 100644 --- a/lib/Cake/Test/Case/Utility/Set2Test.php +++ b/lib/Cake/Test/Case/Utility/Set2Test.php @@ -1208,13 +1208,12 @@ class Set2Test extends CakeTestCase { * @return void */ public function testCombineWithFormatting() { - $this->markTestIncomplete('Not done, format() is not implemented'); $a = self::userData(); $result = Set2::combine( $a, '{n}.User.id', - array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), + array('%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id' ); $expected = array( @@ -1230,9 +1229,11 @@ class Set2Test extends CakeTestCase { $result = Set2::combine( $a, - array('{0}: {1}', - '{n}.User.Data.user', - '{n}.User.Data.name'), + array( + '%s: %s', + '{n}.User.Data.user', + '{n}.User.Data.name' + ), '{n}.User.id' ); $expected = array( @@ -1242,18 +1243,6 @@ class Set2Test extends CakeTestCase { ); $this->assertEquals($expected, $result); - $result = Set2::combine( - $a, - array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), - '{n}.User.id' - ); - $expected = array( - 'Mariano Iglesias: mariano.iglesias' => 2, - 'Larry E. Masters: phpnut' => 14, - 'The Gwoo: gwoo' => 25 - ); - $this->assertEquals($expected, $result); - $result = Set2::combine( $a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), @@ -1278,4 +1267,59 @@ class Set2Test extends CakeTestCase { ); $this->assertEquals($expected, $result); } + +/** + * testFormat method + * + * @return void + */ + public function testFormat() { + $data = self::userData(); + + $result = Set2::format( + $data, + array('{n}.User.Data.user', '{n}.User.id'), + '%s, %s' + ); + $expected = array( + 'mariano.iglesias, 2', + 'phpnut, 14', + 'gwoo, 25' + ); + $this->assertEquals($expected, $result); + + $result = Set2::format( + $data, + array('{n}.User.Data.user', '{n}.User.id'), + '%2$s, %1$s' + ); + $expected = array( + '2, mariano.iglesias', + '14, phpnut', + '25, gwoo' + ); + $this->assertEquals($expected, $result); + } + +/** + * testFormattingNullValues method + * + * @return void + */ + public function testFormatNullValues() { + $this->markTestIncomplete('Not done yet'); + + $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 = Set2::format($data, '%s', array('{n}.Person.something')); + $expected = array('42', '', ''); + $this->assertEquals($expected, $result); + + $result = Set2::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something')); + $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, '); + $this->assertEquals($expected, $result); + } } diff --git a/lib/Cake/Utility/Set2.php b/lib/Cake/Utility/Set2.php index 8466d836c..7341d28ad 100644 --- a/lib/Cake/Utility/Set2.php +++ b/lib/Cake/Utility/Set2.php @@ -149,13 +149,14 @@ class Set2 { protected static function _matchToken($key, $token) { if ($token === '{n}') { return is_numeric($key); - } elseif ($token === '{s}') { - return is_string($key); - } elseif (is_numeric($token)) { - return ($key == $token); - } else { - return ($key === $token); } + if ($token === '{s}') { + return is_string($key); + } + if (is_numeric($token)) { + return ($key == $token); + } + return ($key === $token); } /** @@ -268,7 +269,8 @@ class Set2 { if (!is_array($_list)) { return array(); } - } elseif ($op === 'remove') { + } + if ($op === 'remove') { if ($i === count($path) - 1) { unset($_list[$key]); } else { @@ -329,7 +331,7 @@ class Set2 { if (is_array($keyPath)) { $format = array_shift($keyPath); - $keys = self::format($data, $format, $keyPath); + $keys = self::format($data, $keyPath, $format); } else { $keys = self::extract($data, $keyPath); } @@ -339,19 +341,17 @@ class Set2 { if (!empty($valuePath) && is_array($valuePath)) { $format = array_shift($valuePath); - $vals = self::format($data, $format, $valuePath); + $vals = self::format($data, $valuePath, $format); } elseif (!empty($valuePath)) { $vals = self::extract($data, $valuePath); } $count = count($keys); for ($i = 0; $i < $count; $i++) { - if (!isset($vals[$i])) { - $vals[$i] = null; - } + $vals[$i] = isset($vals[$i]) ? $vals[$i] : null; } - if ($groupPath != null) { + if ($groupPath !== null) { $group = self::extract($data, $groupPath); if (!empty($group)) { $c = count($keys); @@ -371,7 +371,46 @@ class Set2 { return array(); } return array_combine($keys, $vals); + } +/** + * Returns a formated series of values extracted from `$data`, using + * `$format` as the format and `$paths` as the values to extract. + * + * @param array $data Source array from which to extract the data + * @param string $paths An array containing one or more Set2::extract()-style key paths + * @param string $format Format string into which values will be inserted, see sprintf() + * @return array An array of strings extracted from `$path` and formatted with `$format` + * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::format + * @see sprintf() + * @see Set2::extract() + */ + public static function format(array $data, array $paths, $format) { + $extracted = array(); + $count = count($paths); + + if (!$count) { + return; + } + + for ($i = 0; $i < $count; $i++) { + $extracted[] = Set::extract($data, $paths[$i]); + } + $out = array(); + $data = $extracted; + $count = count($data[0]); + + $count2 = count($data); + for ($j = 0; $j < $count; $j++) { + $args = array(); + for ($i = 0; $i < $count2; $i++) { + if (array_key_exists($j, $data[$i])) { + $args[] = $data[$i][$j]; + } + } + $out[] = vsprintf($format, $args); + } + return $out; } /**