mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
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
This commit is contained in:
parent
5e2b574e2f
commit
61df674db5
2 changed files with 129 additions and 0 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in a new issue