From 61df674db5829769864b67c024c3065381124184 Mon Sep 17 00:00:00 2001 From: "mariano.iglesias" Date: Sat, 30 Jun 2007 22:34:47 +0000 Subject: [PATCH] Adding Set::combine to allow mapping of keys and values of an array using paths git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5369 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/set.php | 35 +++++++++++ cake/tests/cases/libs/set.test.php | 94 ++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/cake/libs/set.php b/cake/libs/set.php index 623f833bd..a866200bc 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -603,6 +603,41 @@ class Set extends Object { } return $list; } +/** + * Creates an associative array using a $path1 as the path to build its keys, and optionally + * $path2 as path to get the values. If $path2 is not specified, all values will be initialized + * to null (useful for Set::merge). + * + * @param array $data Array from where to extract keys and values + * @param mixed $path1 As an array, or as a dot-separated string. + * @param mixed $path2 As an array, or as a dot-separated string. + * @return array Combined array + * @access public + */ + function combine($data, $path1 = null, $path2 = null) { + if (is_a($this, 'set') && is_string($data) && empty($path2)) { + $path2 = $path1; + $path1 = $data; + $data = $this->get(); + } + + if (is_object($data)) { + $data = get_object_vars($data); + } + + $data1 = Set::extract($data, $path1); + + if (!empty($path2)) { + $data2 = Set::extract($data, $path2); + } else { + $count = count($data1); + for($i=0; $i < $count; $i++) { + $data2[$i] = null; + } + } + + return array_combine($data1, $data2); + } function reverse($object) { if (is_object($object)) { diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index df03611cd..375513639 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -275,6 +275,100 @@ class SetTest extends UnitTestCase { $this->assertTrue($set->insert('Session Test.Test Case', "test")); $this->assertTrue($set->check('Session Test.Test Case')); } + + function testCombine() { + $a = array( + array('User' => array( + 'id' => 2, + 'Data' => array( + 'user' => 'mariano.iglesias', + 'name' => 'Mariano Iglesias' + ) + )), + array('User' => array( + 'id' => 14, + 'Data' => array( + 'user' => 'phpnut', + 'name' => 'Larry E. Masters' + ) + )), + array('User' => array( + 'id' => 25, + 'Data' => array( + 'user' => 'gwoo', + 'name' => 'The Gwoo' + ) + )) + ); + + $result = Set::combine($a, '{n}.User.id'); + $expected = array( + 2 => null, + 14 => null, + 25 => null + ); + $this->assertIdentical($result, $expected); + + $result = Set::combine($a, '{n}.User.id', '{n}.User.Data'); + $expected = array( + 2 => array( + 'user' => 'mariano.iglesias', + 'name' => 'Mariano Iglesias' + ), + 14 => array( + 'user' => 'phpnut', + 'name' => 'Larry E. Masters' + ), + 25 => array( + 'user' => 'gwoo', + 'name' => 'The Gwoo' + ) + ); + $this->assertIdentical($result, $expected); + + $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name'); + $expected = array( + 2 => 'Mariano Iglesias', + 14 => 'Larry E. Masters', + 25 => 'The Gwoo' + ); + $this->assertIdentical($result, $expected); + + $Set =& new Set($a); + + $result = $Set->combine('{n}.User.id'); + $expected = array( + 2 => null, + 14 => null, + 25 => null + ); + $this->assertIdentical($result, $expected); + + $result = $Set->combine('{n}.User.id', '{n}.User.Data'); + $expected = array( + 2 => array( + 'user' => 'mariano.iglesias', + 'name' => 'Mariano Iglesias' + ), + 14 => array( + 'user' => 'phpnut', + 'name' => 'Larry E. Masters' + ), + 25 => array( + 'user' => 'gwoo', + 'name' => 'The Gwoo' + ) + ); + $this->assertIdentical($result, $expected); + + $result = $Set->combine('{n}.User.id', '{n}.User.Data.name'); + $expected = array( + 2 => 'Mariano Iglesias', + 14 => 'Larry E. Masters', + 25 => 'The Gwoo' + ); + $this->assertIdentical($result, $expected); + } } ?> \ No newline at end of file