From 62e7146d2daf1f9437076d353370e94d052bda29 Mon Sep 17 00:00:00 2001 From: romanb Date: Mon, 29 Mar 2010 13:50:57 +0000 Subject: [PATCH] [2.0][DDC-423] Fixed. --- .../Common/Collections/ArrayCollection.php | 2 +- .../Common/Collections/Collection.php | 115 +++++++++++++----- lib/Doctrine/ORM/PersistentCollection.php | 9 +- .../Common/Collections/CollectionTest.php | 2 +- 4 files changed, 93 insertions(+), 35 deletions(-) diff --git a/lib/Doctrine/Common/Collections/ArrayCollection.php b/lib/Doctrine/Common/Collections/ArrayCollection.php index 58ebab175..a1af5d948 100644 --- a/lib/Doctrine/Common/Collections/ArrayCollection.php +++ b/lib/Doctrine/Common/Collections/ArrayCollection.php @@ -247,7 +247,7 @@ class ArrayCollection implements Collection * @param mixed $element The element to search for. * @return mixed The key/index of the element or FALSE if the element was not found. */ - public function search($element) + public function indexOf($element) { return array_search($element, $this->_elements, true); } diff --git a/lib/Doctrine/Common/Collections/Collection.php b/lib/Doctrine/Common/Collections/Collection.php index c97daa09c..6b415baaf 100644 --- a/lib/Doctrine/Common/Collections/Collection.php +++ b/lib/Doctrine/Common/Collections/Collection.php @@ -21,20 +21,21 @@ namespace Doctrine\Common\Collections; +use Closure, Countable, IteratorAggregate, ArrayAccess; + /** * The missing (SPL) Collection/Array/OrderedMap interface. * * A Collection resembles the nature of a regular PHP array. That is, - * it is essentially an ordered map that can syntactically also be used + * it is essentially an ordered map that can also be used * like a list. * - * A Collection has an internal iterator just like a PHP array. In addition + * A Collection has an internal iterator just like a PHP array. In addition, * a Collection can be iterated with external iterators, which is preferrable. * To use an external iterator simply use the foreach language construct to - * iterate over the collection (which calls getIterator() internally) or - * explicitly retrieve an iterator though getIterator() which can then be + * iterate over the collection (which calls {@link getIterator()} internally) or + * explicitly retrieve an iterator though {@link getIterator()} which can then be * used to iterate over the collection. - * * You can not rely on the internal iterator of the collection being at a certain * position unless you explicitly positioned it before. Prefer iteration with * external iterators. @@ -47,10 +48,10 @@ namespace Doctrine\Common\Collections; * @author Jonathan Wage * @author Roman Borschel */ -interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess +interface Collection extends Countable, IteratorAggregate, ArrayAccess { /** - * Adds an element to the collection. + * Adds an element at the end of the collection. * * @param mixed $element The element to add. * @return boolean Always TRUE. @@ -61,31 +62,31 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * Clears the collection, removing all elements. */ function clear(); - + /** * Checks whether an element is contained in the collection. - * This is an O(n) operation. + * This is an O(n) operation, where n is the size of the collection. * - * @param mixed $element The element to check for. + * @param mixed $element The element to search for. * @return boolean TRUE if the collection contains the element, FALSE otherwise. */ function contains($element); - + /** - * Checks whether the collection is empty. + * Checks whether the collection is empty (contains no elements). * * @return boolean TRUE if the collection is empty, FALSE otherwise. */ function isEmpty(); - + /** - * Removes the element with the specified key/index from the collection. + * Removes the element at the specified index from the collection. * - * @param string|integer $key The key/index of the element to remove. + * @param string|integer $key The kex/index of the element to remove. * @return mixed The removed element or NULL, if the collection did not contain the element. */ function remove($key); - + /** * Removes an element from the collection. * @@ -93,7 +94,7 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * @return mixed The removed element or NULL, if the collection did not contain the element. */ function removeElement($element); - + /** * Checks whether the collection contains an element with the specified key/index. * @@ -102,15 +103,15 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * FALSE otherwise. */ function containsKey($key); - + /** - * Gets an element with a specified key / at a specified index. + * Gets the element at the specified key/index. * * @param string|integer $key The key/index of the element to retrieve. * @return mixed */ function get($key); - + /** * Gets all keys/indices of the collection. * @@ -118,7 +119,7 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * elements in the collection. */ function getKeys(); - + /** * Gets all values of the collection. * @@ -126,7 +127,7 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * appear in the collection. */ function getValues(); - + /** * Sets an element in the collection at the specified key/index. * @@ -134,14 +135,14 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * @param mixed $value The element to set. */ function set($key, $value); - + /** - * Gets a plain PHP array representation of the collection. + * Gets a native PHP array representation of the collection. * * @return array */ function toArray(); - + /** * Sets the internal iterator to the first element in the collection and * returns this element. @@ -149,7 +150,7 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * @return mixed */ function first(); - + /** * Sets the internal iterator to the last element in the collection and * returns this element. @@ -157,22 +158,78 @@ interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess * @return mixed */ function last(); - + /** * Gets the key/index of the element at the current iterator position. * */ function key(); - + /** * Gets the element of the collection at the current iterator position. * */ function current(); - + /** * Moves the internal iterator position to the next element. * */ function next(); + + /** + * Tests for the existence of an element that satisfies the given predicate. + * + * @param Closure $p The predicate. + * @return boolean TRUE if the predicate is TRUE for at least one element, FALSE otherwise. + */ + function exists(Closure $p); + + /** + * Returns all the elements of this collection that satisfy the predicate p. + * The order of the elements is preserved. + * + * @param Closure $p The predicate used for filtering. + * @return Collection A collection with the results of the filter operation. + */ + function filter(Closure $p); + + /** + * Applies the given predicate p to all elements of this collection, + * returning true, if the predicate yields true for all elements. + * + * @param Closure $p The predicate. + * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. + */ + function forAll(Closure $p); + + /** + * Applies the given function to each element in the collection and returns + * a new collection with the elements returned by the function. + * + * @param Closure $func + * @return Collection + */ + function map(Closure $func); + + /** + * Partitions this collection in two collections according to a predicate. + * Keys are preserved in the resulting collections. + * + * @param Closure $p The predicate on which to partition. + * @return array An array with two elements. The first element contains the collection + * of elements where the predicate returned TRUE, the second element + * contains the collection of elements where the predicate returned FALSE. + */ + function partition(Closure $p); + + /** + * Gets the index/key of a given element. The comparison of two elements is strict, + * that means not only the value but also the type must match. + * For objects this means reference equality. + * + * @param mixed $element The element to search for. + * @return mixed The key/index of the element or FALSE if the element was not found. + */ + function indexOf($element); } \ No newline at end of file diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 886d3b0b4..d19537c27 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -22,7 +22,8 @@ namespace Doctrine\ORM; use Doctrine\ORM\Mapping\AssociationMapping, - \Closure; + Doctrine\Common\Collections\Collection, + Closure; /** * A PersistentCollection represents a collection of elements that have persistent state. @@ -40,7 +41,7 @@ use Doctrine\ORM\Mapping\AssociationMapping, * @author Roman Borschel * @author Giorgio Sironi */ -final class PersistentCollection implements \Doctrine\Common\Collections\Collection +final class PersistentCollection implements Collection { /** * A snapshot of the collection at the moment it was fetched from the database. @@ -443,10 +444,10 @@ final class PersistentCollection implements \Doctrine\Common\Collections\Collect /** * {@inheritdoc} */ - public function search($element) + public function indexOf($element) { $this->_initialize(); - return $this->_coll->search($element); + return $this->_coll->indexOf($element); } /** diff --git a/tests/Doctrine/Tests/Common/Collections/CollectionTest.php b/tests/Doctrine/Tests/Common/Collections/CollectionTest.php index 4e642905f..83bb842f5 100644 --- a/tests/Doctrine/Tests/Common/Collections/CollectionTest.php +++ b/tests/Doctrine/Tests/Common/Collections/CollectionTest.php @@ -98,7 +98,7 @@ class CollectionTest extends \Doctrine\Tests\DoctrineTestCase public function testSearch() { $this->_coll[0] = 'test'; - $this->assertEquals(0, $this->_coll->search('test')); + $this->assertEquals(0, $this->_coll->indexOf('test')); } public function testGet()