From 0c977bf6f75895bfa24967837fca96fc80bc6e13 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 16 Aug 2007 06:34:30 +0000 Subject: [PATCH] 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 --- core/model/DataObjectSet.php | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/core/model/DataObjectSet.php b/core/model/DataObjectSet.php index dff255fe5..88fcd5fea 100644 --- a/core/model/DataObjectSet.php +++ b/core/model/DataObjectSet.php @@ -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); + } + } /**