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() (from r109156)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112817 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-19 01:20:56 +00:00
parent a7d0b685ec
commit 496e9bcef6
4 changed files with 61 additions and 5 deletions

View File

@ -491,10 +491,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

@ -355,6 +355,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);
}
}
/**