Added insertFirst(), to insert an item at the beginning of the set

Added containsIDs() and onlyContainsIDs(), to perform simple validation of DataObjectSets - useful for unit testing

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@40228 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2007-08-16 06:34:30 +00:00
parent 4566ec10a6
commit 0c977bf6f7

View File

@ -353,6 +353,23 @@ class DataObjectSet extends ViewableData implements Iterator {
}
}
/**
* Add an item to the beginning of the DataObjectSet
* @param DataObject $item Item to add
* @param string $key Key to index this DataObject by.
*/
public function insertFirst($item, $key = null) {
if($key != null) {
array_shift($this->items, $item);
} else {
// Not very efficient :-(
$newItems = array();
$newItems[$key] = $item;
foreach($this->items as $k => $v) $newItems[$k] = $v;
$this->items = $newItems;
}
}
/**
* @deprecated Use merge()
*/
@ -906,6 +923,26 @@ class DataObjectSet extends ViewableData implements Iterator {
function addWithoutWrite($field) {
$this->items[] = $field;
}
/**
* Returns true if the DataObjectSet contains all of the IDs givem
* @param $idList An array of object IDs
*/
function containsIDs($idList) {
foreach($idList as $item) $wants[$item] = true;
foreach($this->items as $item) if($item) unset($wants[$item->ID]);
return !$wants;
}
/**
* Returns true if the DataObjectSet contains all of and *only* the IDs given.
* Note that it won't like duplicates very much.
* @param $idList An array of object IDs
*/
function onlyContainsIDs($idList) {
return $this->containsIDs($idList) && sizeof($idList) == sizeof($this->items);
}
}
/**