Merge pull request #245 from halkyon/master

SS_Map_Iterator checks for hasMethod() (brought over from DataObjectSet::map() in 2.4)
This commit is contained in:
Sam Minnée 2012-03-18 16:49:17 -07:00
commit 1b97c827dc
3 changed files with 26 additions and 4 deletions

View File

@ -165,7 +165,9 @@ class SS_Map_Iterator implements Iterator {
if(isset($this->firstItems[$this->firstItemIdx])) {
return $this->firstItems[$this->firstItemIdx][1];
} else {
if($rewoundItem) return $rewoundItem->{$this->titleField};
if($rewoundItem) return ($rewoundItem->hasMethod($this->titleField))
? $rewoundItem->{$this->titleField}()
: $rewoundItem->{$this->titleField};
}
}
@ -174,7 +176,9 @@ class SS_Map_Iterator implements Iterator {
if(isset($this->firstItems[$this->firstItemIdx])) {
return $this->firstItems[$this->firstItemIdx][1];
} else {
return $this->items->current()->{$this->titleField};
return ($this->items->current()->hasMethod($this->titleField))
? $this->items->current()->{$this->titleField}()
: $this->items->current()->{$this->titleField};
}
}

View File

@ -1051,7 +1051,11 @@ class DataObjectTest_Team extends DataObject implements TestOnly {
'Position' => 'Varchar(100)'
)
);
function MyTitle() {
return 'Team ' . $this->Title;
}
function getDynamicField() {
return 'dynamicfield';
}

View File

@ -68,6 +68,20 @@ class SS_MapTest extends SapphireTest {
), $map->keys());
}
function testMethodAsValueField() {
$list = DataList::create('DataObjectTest_Team');
$list->sort('Title');
$map = new SS_Map($list, 'ID', 'MyTitle');
$this->assertEquals(array(
'Team Subteam 1',
'Team Subteam 2',
'Team Subteam 3',
'Team Team 1',
'Team Team 2',
'Team Team 3'
), $map->values());
}
function testValues() {
$list = DataList::create('DataObjectTest_TeamComment');
$list->sort('Name');
@ -129,4 +143,4 @@ class SS_MapTest extends SapphireTest {
}
}
}