From 0cbde73445b9701af18bb0642e32b295fc26b9e9 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 10 Aug 2010 05:42:33 +0000 Subject: [PATCH] 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 --- core/model/DataObjectSet.php | 28 +++++++++++++++++++++++++--- security/Group.php | 2 +- security/Member.php | 2 +- tests/DataObjectSetTest.php | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/core/model/DataObjectSet.php b/core/model/DataObjectSet.php index deace8139..2e70d2762 100644 --- a/core/model/DataObjectSet.php +++ b/core/model/DataObjectSet.php @@ -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. diff --git a/security/Group.php b/security/Group.php index 7697ee2d5..eaa0d228d 100644 --- a/security/Group.php +++ b/security/Group.php @@ -241,7 +241,7 @@ class Group extends DataObject { $blankGroup->Title = $blank; $blankGroup->ID = 0; - $ret->getItems()->shift($blankGroup); + $ret->getItems()->unshift($blankGroup); } return $ret; } diff --git a/security/Member.php b/security/Member.php index d35ccf92b..0d1a4798f 100755 --- a/security/Member.php +++ b/security/Member.php @@ -975,7 +975,7 @@ class Member extends DataObject { $blankMember->Surname = $blank; $blankMember->ID = 0; - $ret->getItems()->shift($blankMember); + $ret->getItems()->unshift($blankMember); } return $ret; diff --git a/tests/DataObjectSetTest.php b/tests/DataObjectSetTest.php index ad9101c09..cdcc2972a 100644 --- a/tests/DataObjectSetTest.php +++ b/tests/DataObjectSetTest.php @@ -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); + } + } /**