mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
updating Set::map() to return an array of objects if the key is numeric. Changed __identity__ to _name_, updated tests.
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6091 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
d13c836ae9
commit
86dafa3009
2 changed files with 33 additions and 28 deletions
|
@ -153,7 +153,8 @@ class Set extends Object {
|
|||
return $this->value;
|
||||
}
|
||||
/**
|
||||
* Maps the contents of the Set object to an object hierarchy
|
||||
* Maps the contents of the Set object to an object hierarchy.
|
||||
* Maintains numeric keys as arrays of objects
|
||||
*
|
||||
* @param string $class A class name of the type of object to map to
|
||||
* @param string $tmp A temporary class name used as $class if $class is an array
|
||||
|
@ -201,30 +202,32 @@ class Set extends Object {
|
|||
/**
|
||||
* Maps the given value as an object. If $value is an object,
|
||||
* it returns $value. Otherwise it maps $value as an object of
|
||||
* type $class, and identity $identity. If $value is not empty,
|
||||
* it will be used to set properties of returned object
|
||||
* (recursively).
|
||||
* type $class, and if primary assign _name_ $key on first array.
|
||||
* If $value is not empty, it will be used to set properties of
|
||||
* returned object (recursively). If $key is numeric will maintain array
|
||||
* structure
|
||||
*
|
||||
* @param mixed $value Value to map
|
||||
* @param string $class Class name
|
||||
* @param string $identity Identity to assign to class
|
||||
* @param string $isParent whether to assign first array as parent
|
||||
* @return mixed Mapped object
|
||||
* @access private
|
||||
*/
|
||||
function __map($array, $class, $identity = false) {
|
||||
function __map($array, $class, $primary = false) {
|
||||
$out = new $class;
|
||||
if (is_array($array)) {
|
||||
foreach ($array as $name => $second) {
|
||||
if(is_numeric($name) && is_array($second)) {
|
||||
$out->{$name} = Set::__map($second, $class, true);
|
||||
} elseif ($identity === true && is_array($second)) {
|
||||
$identity = false;
|
||||
$out->__identity__ = $name;
|
||||
foreach($second as $key2 => $third) {
|
||||
$out->{$key2} = Set::__map($third, $class);
|
||||
foreach ($array as $key => $value) {
|
||||
if (is_numeric($key) && is_array($value)) {
|
||||
$out = (array)$out;
|
||||
$out[$key] = Set::__map($value, $class, true);
|
||||
} elseif ($primary === true && is_array($value)) {
|
||||
$out->_name_ = $key;
|
||||
$primary = false;
|
||||
foreach($value as $key2 => $value2) {
|
||||
$out->{$key2} = Set::__map($value2, $class);
|
||||
}
|
||||
} else {
|
||||
$out->{$name} = Set::__map($second, $class);
|
||||
$out->{$key} = Set::__map($value, $class);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -749,9 +752,9 @@ class Set extends Object {
|
|||
$out = array();
|
||||
if (is_object($object)) {
|
||||
$keys = get_object_vars($object);
|
||||
if(isset($keys['__identity__'])) {
|
||||
$identity = $keys['__identity__'];
|
||||
unset($keys['__identity__']);
|
||||
if (isset($keys['_name_'])) {
|
||||
$identity = $keys['_name_'];
|
||||
unset($keys['_name_']);
|
||||
}
|
||||
$new = array();
|
||||
foreach ($keys as $key => $value) {
|
||||
|
|
|
@ -588,6 +588,7 @@ class SetTest extends UnitTestCase {
|
|||
),
|
||||
)
|
||||
);
|
||||
|
||||
$mapped = Set::map($expected);
|
||||
$ids = array();
|
||||
|
||||
|
@ -595,7 +596,7 @@ class SetTest extends UnitTestCase {
|
|||
$ids[] = $object->id;
|
||||
}
|
||||
$this->assertEqual($ids, array(1, 2));
|
||||
$this->assertEqual(get_object_vars($mapped->{'0'}->headers), $expected[0]['IndexedPage']['headers']);
|
||||
$this->assertEqual(get_object_vars($mapped[0]->headers), $expected[0]['IndexedPage']['headers']);
|
||||
|
||||
$result = Set::reverse($mapped);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
@ -627,7 +628,7 @@ class SetTest extends UnitTestCase {
|
|||
$mapped = Set::map($data);
|
||||
|
||||
$expected = new stdClass();
|
||||
$expected->__identity__ = 'IndexedPage';
|
||||
$expected->_name_ = 'IndexedPage';
|
||||
$expected->id = 2;
|
||||
$expected->url = 'http://blah.com/';
|
||||
$expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
|
||||
|
@ -635,7 +636,7 @@ class SetTest extends UnitTestCase {
|
|||
$expected->redirect = '';
|
||||
$expected->created = "1195055503";
|
||||
$expected->updated = "1195055503";
|
||||
$this->assertIdentical($mapped->{'1'}, $expected);
|
||||
$this->assertIdentical($mapped[1], $expected);
|
||||
|
||||
$ids = array();
|
||||
|
||||
|
@ -658,7 +659,7 @@ class SetTest extends UnitTestCase {
|
|||
));
|
||||
|
||||
$expected = new stdClass;
|
||||
$expected->__identity__ = 'Post';
|
||||
$expected->_name_ = 'Post';
|
||||
$expected->id = '1';
|
||||
$expected->author_id = '1';
|
||||
$expected->title = 'First Post';
|
||||
|
@ -676,7 +677,7 @@ class SetTest extends UnitTestCase {
|
|||
$expected->Author->test = "working";
|
||||
|
||||
$expected2 = new stdClass;
|
||||
$expected2->__identity__ = 'Post';
|
||||
$expected2->_name_ = 'Post';
|
||||
$expected2->id = '2';
|
||||
$expected2->author_id = '3';
|
||||
$expected2->title = 'Second Post';
|
||||
|
@ -693,12 +694,13 @@ class SetTest extends UnitTestCase {
|
|||
$expected2->Author->updated = "2007-03-17 01:22:31";
|
||||
$expected2->Author->test = "working";
|
||||
|
||||
$test = new stdClass;
|
||||
$test->{'0'} = $expected;
|
||||
$test->{'1'} = $expected2;
|
||||
$test = array();
|
||||
$test[0] = $expected;
|
||||
$test[1] = $expected2;
|
||||
|
||||
$this->assertIdentical($test, $result);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue