diff --git a/lib/Cake/Test/Case/Utility/Set2Test.php b/lib/Cake/Test/Case/Utility/Set2Test.php index cdf6bcae342..aa70bebbafa 100644 --- a/lib/Cake/Test/Case/Utility/Set2Test.php +++ b/lib/Cake/Test/Case/Utility/Set2Test.php @@ -833,6 +833,45 @@ public function testExtractAttributePattern() { $this->assertEquals($expected, $result); } +/** + * Test that uneven keys are handled correctly. + * + * @return void + */ + public function testExtractUnevenKeys() { + $data = array( + 'Level1' => array( + 'Level2' => array('test1', 'test2'), + 'Level2bis' => array('test3', 'test4') + ) + ); + $this->assertEquals( + array('test1', 'test2'), + Set2::extract($data, 'Level1.Level2') + ); + $this->assertEquals( + array('test3', 'test4'), + Set2::extract($data, 'Level1.Level2bis') + ); + + $data = array( + 'Level1' => array( + 'Level2bis' => array( + array('test3', 'test4'), + array('test5', 'test6') + ) + ) + ); + $expected = array( + array('test3', 'test4'), + array('test5', 'test6') + ); + $this->assertEquals($expected, Set2::extract($data, 'Level1.Level2bis')); + + $data['Level1']['Level2'] = array('test1', 'test2'); + $this->assertEquals($expected, Set2::extract($data, 'Level1.Level2bis')); + } + /** * testSort method * diff --git a/lib/Cake/Utility/Set2.php b/lib/Cake/Utility/Set2.php index 7341d28ad8e..33bfb46a9b3 100644 --- a/lib/Cake/Utility/Set2.php +++ b/lib/Cake/Utility/Set2.php @@ -376,6 +376,14 @@ public static function combine(array $data, $keyPath, $valuePath = null, $groupP /** * Returns a formated series of values extracted from `$data`, using * `$format` as the format and `$paths` as the values to extract. + * + * Usage: + * + * {{{ + * $result = Set::format($users, array('{n}.User.id', '{n}.User.name'), '%s : %s'); + * }}} + * + * The `$format` string can use any format options that `vsprintf()` and `sprintf()` do. * * @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