From 92214eaf3753458d5c571ab19f674cc694620e95 Mon Sep 17 00:00:00 2001 From: romanb Date: Wed, 29 Jul 2009 12:00:08 +0000 Subject: [PATCH] [2.0] Part II for ticket #2352. Fixed #2352. --- .../Common/Collections/ArrayCollection.php | 2 +- .../Common/Collections/Collection.php | 129 ++++++++++++++++++ .../ORM/Internal/Hydration/ObjectHydrator.php | 4 +- lib/Doctrine/ORM/PersistentCollection.php | 2 +- lib/Doctrine/ORM/UnitOfWork.php | 12 +- .../AbstractManyToManyAssociationTestCase.php | 4 +- 6 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 lib/Doctrine/Common/Collections/Collection.php diff --git a/lib/Doctrine/Common/Collections/ArrayCollection.php b/lib/Doctrine/Common/Collections/ArrayCollection.php index d68cd8eba..4c0ca0ad1 100644 --- a/lib/Doctrine/Common/Collections/ArrayCollection.php +++ b/lib/Doctrine/Common/Collections/ArrayCollection.php @@ -30,7 +30,7 @@ use \Closure, \ArrayIterator; * @author Roman S. Borschel * @since 2.0 */ -class ArrayCollection implements ICollection +class ArrayCollection implements Collection { /** * An array containing the entries of this collection. diff --git a/lib/Doctrine/Common/Collections/Collection.php b/lib/Doctrine/Common/Collections/Collection.php new file mode 100644 index 000000000..0e71bc55b --- /dev/null +++ b/lib/Doctrine/Common/Collections/Collection.php @@ -0,0 +1,129 @@ + + * @since 2.0 + */ +interface Collection extends \Countable, \IteratorAggregate, \ArrayAccess +{ + /** + * Adds an element to the collection. + * + * @param mixed $element The element to add. + * @return boolean Always TRUE. + */ + function add($element); + + /** + * Clears the collection. + */ + function clear(); + + /** + * Checks whether an element is contained in the collection. + * This is an O(n) operation. + * + * @param mixed $element The element to check for. + * @return boolean TRUE if the collection contains the element, FALSE otherwise. + */ + function contains($element); + + /** + * Checks whether the collection is empty. + * + * @return boolean TRUE if the collection is empty, FALSE otherwise. + */ + function isEmpty(); + + /** + * Removes the element with the specified key/index from the collection. + * + * @param string|integer $key The key/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. + * + * @param mixed $element The element to remove. + * @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. + * + * @param string|integer $key The key/index to check for. + * @return boolean TRUE if the collection contains an element with the specified key/index, + * FALSE otherwise. + */ + function containsKey($key); + + /** + * Gets an element with a specified key / at a specified 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. + * + * @return array The keys/indices of the collection, in the order of the corresponding + * elements in the collection. + */ + function getKeys(); + + /** + * Gets all values of the collection. + * + * @return array The values of all elements in the collection, in the order they + * appear in the collection. + */ + function getValues(); + + /** + * Sets an element in the collection at the specified key/index. + * + * @param string|integer $key The key/index of the element to set. + * @param mixed $value The element to set. + */ + function set($key, $value); + + /** + * Counts the elements in the collection. + * + * @return integer The number of elements in the collection. + */ + //function count(); + + /** + * Gets a plain PHP array representation of the collection. + * + * @return array + */ + function toArray(); + + /** + * Gets the first element of the collection. + * + * @return mixed + */ + function first(); + + /** + * Gets the last element of the collection. + * + * @return mixed + */ + function last(); +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php index d63f4395d..eefdd8770 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php @@ -25,7 +25,7 @@ use Doctrine\DBAL\Connection, Doctrine\ORM\PersistentCollection, Doctrine\ORM\Query, Doctrine\Common\Collections\ArrayCollection, - Doctrine\Common\Collections\ICollection; + Doctrine\Common\Collections\Collection; /** * The ObjectHydrator constructs an object graph out of an SQL result set. @@ -147,7 +147,7 @@ class ObjectHydrator extends AbstractHydrator if ( ! is_object($coll)) { end($coll); $this->_resultPointers[$dqlAlias] =& $coll[key($coll)]; - } else if ($coll instanceof ICollection) { + } else if ($coll instanceof Collection) { if (count($coll) > 0) { $this->_resultPointers[$dqlAlias] = $coll->last(); } diff --git a/lib/Doctrine/ORM/PersistentCollection.php b/lib/Doctrine/ORM/PersistentCollection.php index 101631ce7..0f4040f4f 100644 --- a/lib/Doctrine/ORM/PersistentCollection.php +++ b/lib/Doctrine/ORM/PersistentCollection.php @@ -44,7 +44,7 @@ use Doctrine\Common\DoctrineException, * @author Roman Borschel * @author Giorgio Sironi */ -final class PersistentCollection implements \Doctrine\Common\Collections\ICollection +final class PersistentCollection implements \Doctrine\Common\Collections\Collection { /** * A snapshot of the collection at the moment it was fetched from the database. diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index fa2a647ab..28caf3255 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -22,7 +22,7 @@ namespace Doctrine\ORM; use Doctrine\Common\Collections\ArrayCollection, - Doctrine\Common\Collections\ICollection, + Doctrine\Common\Collections\Collection, Doctrine\Common\DoctrineException, Doctrine\Common\PropertyChangedListener, Doctrine\ORM\Event\LifecycleEventArgs, @@ -1455,7 +1455,7 @@ class UnitOfWork implements PropertyChangedListener continue; } $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); - if ($relatedEntities instanceof ICollection) { + if ($relatedEntities instanceof Collection) { foreach ($relatedEntities as $relatedEntity) { $this->_doRefresh($relatedEntity, $visited); } @@ -1479,7 +1479,7 @@ class UnitOfWork implements PropertyChangedListener continue; } $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); - if ($relatedEntities instanceof ICollection) { + if ($relatedEntities instanceof Collection) { foreach ($relatedEntities as $relatedEntity) { $this->_doDetach($relatedEntity, $visited); } @@ -1504,7 +1504,7 @@ class UnitOfWork implements PropertyChangedListener continue; } $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); - if ($relatedEntities instanceof ICollection) { + if ($relatedEntities instanceof Collection) { foreach ($relatedEntities as $relatedEntity) { $this->_doMerge($relatedEntity, $visited, $managedCopy, $assocMapping); } @@ -1529,7 +1529,7 @@ class UnitOfWork implements PropertyChangedListener continue; } $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName]->getValue($entity); - if (($relatedEntities instanceof ICollection || is_array($relatedEntities))) { + if (($relatedEntities instanceof Collection || is_array($relatedEntities))) { foreach ($relatedEntities as $relatedEntity) { $this->_doPersist($relatedEntity, $visited); } @@ -1554,7 +1554,7 @@ class UnitOfWork implements PropertyChangedListener } $relatedEntities = $class->reflFields[$assocMapping->sourceFieldName] ->getValue($entity); - if ($relatedEntities instanceof ICollection || is_array($relatedEntities)) { + if ($relatedEntities instanceof Collection || is_array($relatedEntities)) { foreach ($relatedEntities as $relatedEntity) { $this->_doRemove($relatedEntity, $visited); } diff --git a/tests/Doctrine/Tests/ORM/Functional/AbstractManyToManyAssociationTestCase.php b/tests/Doctrine/Tests/ORM/Functional/AbstractManyToManyAssociationTestCase.php index 21f97b696..201565c67 100644 --- a/tests/Doctrine/Tests/ORM/Functional/AbstractManyToManyAssociationTestCase.php +++ b/tests/Doctrine/Tests/ORM/Functional/AbstractManyToManyAssociationTestCase.php @@ -2,7 +2,7 @@ namespace Doctrine\Tests\ORM\Functional; -use Doctrine\Common\Collections\ICollection; +use Doctrine\Common\Collections\Collection; require_once __DIR__ . '/../../TestInit.php'; @@ -36,7 +36,7 @@ class AbstractManyToManyAssociationTestCase extends \Doctrine\Tests\OrmFunctiona ->fetchAll()); } - public function assertCollectionEquals(ICollection $first, ICollection $second) + public function assertCollectionEquals(Collection $first, Collection $second) { return $first->forAll(function($k, $e) use($second) { return $second->contains($e); }); }