API CHANGE #5873 DataObjectSet::shift() now performs a proper shift instead of unshift (wrong). Please use DataObjectSet::unshift($item) if unshifting was intended!

API CHANGE Added DataObjectSet::pop()
MINOR Unit tests for DataObjectSet::shift(), DataObjectSet::unshift() and DataObjectSet::pop()


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@109156 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2010-08-10 05:42:33 +00:00 committed by Sam Minnee
parent 07987a39b0
commit 0cbde73445
4 changed files with 61 additions and 5 deletions

View File

@ -458,10 +458,32 @@ class DataObjectSet extends ViewableData implements IteratorAggregate, Countable
* Insert a DataObject at the beginning of this set.
* @param DataObject $item Item to insert.
*/
public function shift($item) {
array_unshift($this->items, $item);
public function unshift($item) {
$this->insertFirst($item);
}
/**
* Remove a DataObject from the beginning of this set and return it.
* This is the equivalent of pop() but acts on the head of the set.
* Opposite of unshift().
*
* @return DataObject (or null if there are no items in the set)
*/
public function shift() {
return array_shift($this->items);
}
/**
* Remove a DataObject from the end of this set and return it.
* This is the equivalent of shift() but acts on the tail of the set.
* Opposite of push().
*
* @return DataObject (or null if there are no items in the set)
*/
public function pop() {
return array_pop($this->items);
}
/**
* Remove a DataObject from this set.
* @param DataObject $itemObject Item to remove.

View File

@ -241,7 +241,7 @@ class Group extends DataObject {
$blankGroup->Title = $blank;
$blankGroup->ID = 0;
$ret->getItems()->shift($blankGroup);
$ret->getItems()->unshift($blankGroup);
}
return $ret;
}

View File

@ -975,7 +975,7 @@ class Member extends DataObject {
$blankMember->Surname = $blank;
$blankMember->ID = 0;
$ret->getItems()->shift($blankMember);
$ret->getItems()->unshift($blankMember);
}
return $ret;

View File

@ -338,6 +338,40 @@ class DataObjectSetTest extends SapphireTest {
$this->assertTrue($set->exists(), 'Non-empty set does exist.');
}
/**
* Test {@link DataObjectSet->shift()}
*/
function testShift() {
$set = new DataObjectSet();
$set->push(new ArrayData(array('Name' => 'Joe')));
$set->push(new ArrayData(array('Name' => 'Bob')));
$set->push(new ArrayData(array('Name' => 'Ted')));
$this->assertEquals('Joe', $set->shift()->Name);
}
/**
* Test {@link DataObjectSet->unshift()}
*/
function testUnshift() {
$set = new DataObjectSet();
$set->push(new ArrayData(array('Name' => 'Joe')));
$set->push(new ArrayData(array('Name' => 'Bob')));
$set->push(new ArrayData(array('Name' => 'Ted')));
$set->unshift(new ArrayData(array('Name' => 'Steve')));
$this->assertEquals('Steve', $set->First()->Name);
}
/**
* Test {@link DataObjectSet->pop()}
*/
function testPop() {
$set = new DataObjectSet();
$set->push(new ArrayData(array('Name' => 'Joe')));
$set->push(new ArrayData(array('Name' => 'Bob')));
$set->push(new ArrayData(array('Name' => 'Ted')));
$this->assertEquals('Ted', $set->pop()->Name);
}
}
/**