pax_global_header00006660000000000000000000000064125133114060014506gustar00rootroot0000000000000052 comment=6c1e4eef75f310ea1b3e30945e9f06e652128b8a collections-1.3.0/000077500000000000000000000000001251331140600140255ustar00rootroot00000000000000collections-1.3.0/.gitignore000066400000000000000000000000101251331140600160040ustar00rootroot00000000000000vendor/ collections-1.3.0/.travis.yml000066400000000000000000000003421251331140600161350ustar00rootroot00000000000000language: php php: - 5.3 - 5.4 - 5.5 - 5.6 - 7.0 - hhvm - hhvm-nightly matrix: fast_finish: true allow_failures: - php: 7.0 before_script: - composer --prefer-source install script: - phpunit collections-1.3.0/LICENSE000066400000000000000000000020511251331140600150300ustar00rootroot00000000000000Copyright (c) 2006-2013 Doctrine Project Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. collections-1.3.0/README.md000066400000000000000000000015621251331140600153100ustar00rootroot00000000000000# Doctrine Collections [![Build Status](https://travis-ci.org/doctrine/collections.svg?branch=master)](https://travis-ci.org/doctrine/collections) Collections Abstraction library ## Changelog ### v1.3.0 * [Explicit casting of first and max results in criteria API](https://github.com/doctrine/collections/pull/26) * [Keep keys when using `ArrayCollection#matching()` with sorting](https://github.com/doctrine/collections/pull/49) * [Made `AbstractLazyCollection#$initialized` protected for extensibility](https://github.com/doctrine/collections/pull/52) ### v1.2.0 * Add a new ``AbstractLazyCollection`` ### v1.1.0 * Deprecated ``Comparison::IS``, because it's only there for SQL semantics. These are fixed in the ORM instead. * Add ``Comparison::CONTAINS`` to perform partial string matches: $criteria->andWhere($criteria->expr()->contains('property', 'Foo')); collections-1.3.0/composer.json000066400000000000000000000016221251331140600165500ustar00rootroot00000000000000{ "name": "doctrine/collections", "type": "library", "description": "Collections Abstraction library", "keywords": ["collections", "array", "iterator"], "homepage": "http://www.doctrine-project.org", "license": "MIT", "authors": [ {"name": "Guilherme Blanco", "email": "guilhermeblanco@gmail.com"}, {"name": "Roman Borschel", "email": "roman@code-factory.org"}, {"name": "Benjamin Eberlei", "email": "kontakt@beberlei.de"}, {"name": "Jonathan Wage", "email": "jonwage@gmail.com"}, {"name": "Johannes Schmitt", "email": "schmittjoh@gmail.com"} ], "require": { "php": ">=5.3.2" }, "require-dev": { "phpunit/phpunit": "~4.0" }, "autoload": { "psr-0": { "Doctrine\\Common\\Collections\\": "lib/" } }, "extra": { "branch-alias": { "dev-master": "1.2.x-dev" } } } collections-1.3.0/lib/000077500000000000000000000000001251331140600145735ustar00rootroot00000000000000collections-1.3.0/lib/Doctrine/000077500000000000000000000000001251331140600163425ustar00rootroot00000000000000collections-1.3.0/lib/Doctrine/Common/000077500000000000000000000000001251331140600175725ustar00rootroot00000000000000collections-1.3.0/lib/Doctrine/Common/Collections/000077500000000000000000000000001251331140600220505ustar00rootroot00000000000000collections-1.3.0/lib/Doctrine/Common/Collections/AbstractLazyCollection.php000066400000000000000000000152761251331140600272130ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections; use Closure; /** * Lazy collection that is backed by a concrete collection * * @author Michaƫl Gallego * @since 1.2 */ abstract class AbstractLazyCollection implements Collection { /** * The backed collection to use * * @var Collection */ protected $collection; /** * @var boolean */ protected $initialized = false; /** * {@inheritDoc} */ public function count() { $this->initialize(); return $this->collection->count(); } /** * {@inheritDoc} */ public function add($element) { $this->initialize(); return $this->collection->add($element); } /** * {@inheritDoc} */ public function clear() { $this->initialize(); $this->collection->clear(); } /** * {@inheritDoc} */ public function contains($element) { $this->initialize(); return $this->collection->contains($element); } /** * {@inheritDoc} */ public function isEmpty() { $this->initialize(); return $this->collection->isEmpty(); } /** * {@inheritDoc} */ public function remove($key) { $this->initialize(); return $this->collection->remove($key); } /** * {@inheritDoc} */ public function removeElement($element) { $this->initialize(); return $this->collection->removeElement($element); } /** * {@inheritDoc} */ public function containsKey($key) { $this->initialize(); return $this->collection->containsKey($key); } /** * {@inheritDoc} */ public function get($key) { $this->initialize(); return $this->collection->get($key); } /** * {@inheritDoc} */ public function getKeys() { $this->initialize(); return $this->collection->getKeys(); } /** * {@inheritDoc} */ public function getValues() { $this->initialize(); return $this->collection->getValues(); } /** * {@inheritDoc} */ public function set($key, $value) { $this->initialize(); $this->collection->set($key, $value); } /** * {@inheritDoc} */ public function toArray() { $this->initialize(); return $this->collection->toArray(); } /** * {@inheritDoc} */ public function first() { $this->initialize(); return $this->collection->first(); } /** * {@inheritDoc} */ public function last() { $this->initialize(); return $this->collection->last(); } /** * {@inheritDoc} */ public function key() { $this->initialize(); return $this->collection->key(); } /** * {@inheritDoc} */ public function current() { $this->initialize(); return $this->collection->current(); } /** * {@inheritDoc} */ public function next() { $this->initialize(); return $this->collection->next(); } /** * {@inheritDoc} */ public function exists(Closure $p) { $this->initialize(); return $this->collection->exists($p); } /** * {@inheritDoc} */ public function filter(Closure $p) { $this->initialize(); return $this->collection->filter($p); } /** * {@inheritDoc} */ public function forAll(Closure $p) { $this->initialize(); return $this->collection->forAll($p); } /** * {@inheritDoc} */ public function map(Closure $func) { $this->initialize(); return $this->collection->map($func); } /** * {@inheritDoc} */ public function partition(Closure $p) { $this->initialize(); return $this->collection->partition($p); } /** * {@inheritDoc} */ public function indexOf($element) { $this->initialize(); return $this->collection->indexOf($element); } /** * {@inheritDoc} */ public function slice($offset, $length = null) { $this->initialize(); return $this->collection->slice($offset, $length); } /** * {@inheritDoc} */ public function getIterator() { $this->initialize(); return $this->collection->getIterator(); } /** * {@inheritDoc} */ public function offsetExists($offset) { $this->initialize(); return $this->collection->offsetExists($offset); } /** * {@inheritDoc} */ public function offsetGet($offset) { $this->initialize(); return $this->collection->offsetGet($offset); } /** * {@inheritDoc} */ public function offsetSet($offset, $value) { $this->initialize(); $this->collection->offsetSet($offset, $value); } /** * {@inheritDoc} */ public function offsetUnset($offset) { $this->initialize(); $this->collection->offsetUnset($offset); } /** * Is the lazy collection already initialized? * * @return bool */ public function isInitialized() { return $this->initialized; } /** * Initialize the collection * * @return void */ protected function initialize() { if ( ! $this->initialized) { $this->doInitialize(); $this->initialized = true; } } /** * Do the initialization logic * * @return void */ abstract protected function doInitialize(); } collections-1.3.0/lib/Doctrine/Common/Collections/ArrayCollection.php000066400000000000000000000176731251331140600256710ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections; use ArrayIterator; use Closure; use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; /** * An ArrayCollection is a Collection implementation that wraps a regular PHP array. * * @since 2.0 * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel */ class ArrayCollection implements Collection, Selectable { /** * An array containing the entries of this collection. * * @var array */ private $elements; /** * Initializes a new ArrayCollection. * * @param array $elements */ public function __construct(array $elements = array()) { $this->elements = $elements; } /** * {@inheritDoc} */ public function toArray() { return $this->elements; } /** * {@inheritDoc} */ public function first() { return reset($this->elements); } /** * {@inheritDoc} */ public function last() { return end($this->elements); } /** * {@inheritDoc} */ public function key() { return key($this->elements); } /** * {@inheritDoc} */ public function next() { return next($this->elements); } /** * {@inheritDoc} */ public function current() { return current($this->elements); } /** * {@inheritDoc} */ public function remove($key) { if ( ! isset($this->elements[$key]) && ! array_key_exists($key, $this->elements)) { return null; } $removed = $this->elements[$key]; unset($this->elements[$key]); return $removed; } /** * {@inheritDoc} */ public function removeElement($element) { $key = array_search($element, $this->elements, true); if ($key === false) { return false; } unset($this->elements[$key]); return true; } /** * Required by interface ArrayAccess. * * {@inheritDoc} */ public function offsetExists($offset) { return $this->containsKey($offset); } /** * Required by interface ArrayAccess. * * {@inheritDoc} */ public function offsetGet($offset) { return $this->get($offset); } /** * Required by interface ArrayAccess. * * {@inheritDoc} */ public function offsetSet($offset, $value) { if ( ! isset($offset)) { return $this->add($value); } $this->set($offset, $value); } /** * Required by interface ArrayAccess. * * {@inheritDoc} */ public function offsetUnset($offset) { return $this->remove($offset); } /** * {@inheritDoc} */ public function containsKey($key) { return isset($this->elements[$key]) || array_key_exists($key, $this->elements); } /** * {@inheritDoc} */ public function contains($element) { return in_array($element, $this->elements, true); } /** * {@inheritDoc} */ public function exists(Closure $p) { foreach ($this->elements as $key => $element) { if ($p($key, $element)) { return true; } } return false; } /** * {@inheritDoc} */ public function indexOf($element) { return array_search($element, $this->elements, true); } /** * {@inheritDoc} */ public function get($key) { return isset($this->elements[$key]) ? $this->elements[$key] : null; } /** * {@inheritDoc} */ public function getKeys() { return array_keys($this->elements); } /** * {@inheritDoc} */ public function getValues() { return array_values($this->elements); } /** * {@inheritDoc} */ public function count() { return count($this->elements); } /** * {@inheritDoc} */ public function set($key, $value) { $this->elements[$key] = $value; } /** * {@inheritDoc} */ public function add($value) { $this->elements[] = $value; return true; } /** * {@inheritDoc} */ public function isEmpty() { return empty($this->elements); } /** * Required by interface IteratorAggregate. * * {@inheritDoc} */ public function getIterator() { return new ArrayIterator($this->elements); } /** * {@inheritDoc} */ public function map(Closure $func) { return new static(array_map($func, $this->elements)); } /** * {@inheritDoc} */ public function filter(Closure $p) { return new static(array_filter($this->elements, $p)); } /** * {@inheritDoc} */ public function forAll(Closure $p) { foreach ($this->elements as $key => $element) { if ( ! $p($key, $element)) { return false; } } return true; } /** * {@inheritDoc} */ public function partition(Closure $p) { $matches = $noMatches = array(); foreach ($this->elements as $key => $element) { if ($p($key, $element)) { $matches[$key] = $element; } else { $noMatches[$key] = $element; } } return array(new static($matches), new static($noMatches)); } /** * Returns a string representation of this object. * * @return string */ public function __toString() { return __CLASS__ . '@' . spl_object_hash($this); } /** * {@inheritDoc} */ public function clear() { $this->elements = array(); } /** * {@inheritDoc} */ public function slice($offset, $length = null) { return array_slice($this->elements, $offset, $length, true); } /** * {@inheritDoc} */ public function matching(Criteria $criteria) { $expr = $criteria->getWhereExpression(); $filtered = $this->elements; if ($expr) { $visitor = new ClosureExpressionVisitor(); $filter = $visitor->dispatch($expr); $filtered = array_filter($filtered, $filter); } if ($orderings = $criteria->getOrderings()) { foreach (array_reverse($orderings) as $field => $ordering) { $next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1); } uasort($filtered, $next); } $offset = $criteria->getFirstResult(); $length = $criteria->getMaxResults(); if ($offset || $length) { $filtered = array_slice($filtered, (int)$offset, $length); } return new static($filtered); } } collections-1.3.0/lib/Doctrine/Common/Collections/Collection.php000066400000000000000000000210561251331140600246600ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections; use ArrayAccess; use Closure; use Countable; use IteratorAggregate; /** * 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 also be used * like a list. * * A Collection has an internal iterator just like a PHP array. In addition, * a Collection can be iterated with external iterators, which is preferable. * To use an external iterator simply use the foreach language construct to * 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. * * @since 2.0 * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel */ interface Collection extends Countable, IteratorAggregate, ArrayAccess { /** * Adds an element at the end of the collection. * * @param mixed $element The element to add. * * @return boolean Always TRUE. */ public function add($element); /** * Clears the collection, removing all elements. * * @return void */ public function clear(); /** * Checks whether an element is contained in the collection. * This is an O(n) operation, where n is the size of the collection. * * @param mixed $element The element to search for. * * @return boolean TRUE if the collection contains the element, FALSE otherwise. */ public function contains($element); /** * Checks whether the collection is empty (contains no elements). * * @return boolean TRUE if the collection is empty, FALSE otherwise. */ public function isEmpty(); /** * Removes the element at the specified index from the collection. * * @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. */ public function remove($key); /** * Removes the specified element from the collection, if it is found. * * @param mixed $element The element to remove. * * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. */ public 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. */ public function containsKey($key); /** * Gets the element at the specified key/index. * * @param string|integer $key The key/index of the element to retrieve. * * @return mixed */ public 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. */ public 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. */ public 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. * * @return void */ public function set($key, $value); /** * Gets a native PHP array representation of the collection. * * @return array */ public function toArray(); /** * Sets the internal iterator to the first element in the collection and returns this element. * * @return mixed */ public function first(); /** * Sets the internal iterator to the last element in the collection and returns this element. * * @return mixed */ public function last(); /** * Gets the key/index of the element at the current iterator position. * * @return int|string */ public function key(); /** * Gets the element of the collection at the current iterator position. * * @return mixed */ public function current(); /** * Moves the internal iterator position to the next element and returns this element. * * @return mixed */ public 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. */ public 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. */ public function filter(Closure $p); /** * Tests whether the given predicate p holds for all elements of this collection. * * @param Closure $p The predicate. * * @return boolean TRUE, if the predicate yields TRUE for all elements, FALSE otherwise. */ public 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 */ public 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. */ public 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 int|string|bool The key/index of the element or FALSE if the element was not found. */ public function indexOf($element); /** * Extracts a slice of $length elements starting at position $offset from the Collection. * * If $length is null it returns all elements from $offset to the end of the Collection. * Keys have to be preserved by this method. Calling this method will only return the * selected slice and NOT change the elements contained in the collection slice is called on. * * @param int $offset The offset to start from. * @param int|null $length The maximum number of elements to return, or null for no limit. * * @return array */ public function slice($offset, $length = null); } collections-1.3.0/lib/Doctrine/Common/Collections/Criteria.php000066400000000000000000000141511251331140600243250ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections; use Doctrine\Common\Collections\Expr\Expression; use Doctrine\Common\Collections\Expr\CompositeExpression; /** * Criteria for filtering Selectable collections. * * @author Benjamin Eberlei * @since 2.3 */ class Criteria { /** * @var string */ const ASC = 'ASC'; /** * @var string */ const DESC = 'DESC'; /** * @var \Doctrine\Common\Collections\ExpressionBuilder|null */ private static $expressionBuilder; /** * @var \Doctrine\Common\Collections\Expr\Expression|null */ private $expression; /** * @var string[] */ private $orderings = array(); /** * @var int|null */ private $firstResult; /** * @var int|null */ private $maxResults; /** * Creates an instance of the class. * * @return Criteria */ public static function create() { return new static(); } /** * Returns the expression builder. * * @return \Doctrine\Common\Collections\ExpressionBuilder */ public static function expr() { if (self::$expressionBuilder === null) { self::$expressionBuilder = new ExpressionBuilder(); } return self::$expressionBuilder; } /** * Construct a new Criteria. * * @param Expression $expression * @param string[]|null $orderings * @param int|null $firstResult * @param int|null $maxResults */ public function __construct(Expression $expression = null, array $orderings = null, $firstResult = null, $maxResults = null) { $this->expression = $expression; $this->setFirstResult($firstResult); $this->setMaxResults($maxResults); if (null !== $orderings) { $this->orderBy($orderings); } } /** * Sets the where expression to evaluate when this Criteria is searched for. * * @param Expression $expression * * @return Criteria */ public function where(Expression $expression) { $this->expression = $expression; return $this; } /** * Appends the where expression to evaluate when this Criteria is searched for * using an AND with previous expression. * * @param Expression $expression * * @return Criteria */ public function andWhere(Expression $expression) { if ($this->expression === null) { return $this->where($expression); } $this->expression = new CompositeExpression(CompositeExpression::TYPE_AND, array( $this->expression, $expression )); return $this; } /** * Appends the where expression to evaluate when this Criteria is searched for * using an OR with previous expression. * * @param Expression $expression * * @return Criteria */ public function orWhere(Expression $expression) { if ($this->expression === null) { return $this->where($expression); } $this->expression = new CompositeExpression(CompositeExpression::TYPE_OR, array( $this->expression, $expression )); return $this; } /** * Gets the expression attached to this Criteria. * * @return Expression|null */ public function getWhereExpression() { return $this->expression; } /** * Gets the current orderings of this Criteria. * * @return string[] */ public function getOrderings() { return $this->orderings; } /** * Sets the ordering of the result of this Criteria. * * Keys are field and values are the order, being either ASC or DESC. * * @see Criteria::ASC * @see Criteria::DESC * * @param string[] $orderings * * @return Criteria */ public function orderBy(array $orderings) { $this->orderings = array_map( function ($ordering) { return strtoupper($ordering) === Criteria::ASC ? Criteria::ASC : Criteria::DESC; }, $orderings ); return $this; } /** * Gets the current first result option of this Criteria. * * @return int|null */ public function getFirstResult() { return $this->firstResult; } /** * Set the number of first result that this Criteria should return. * * @param int|null $firstResult The value to set. * * @return Criteria */ public function setFirstResult($firstResult) { $this->firstResult = null === $firstResult ? null : (int) $firstResult; return $this; } /** * Gets maxResults. * * @return int|null */ public function getMaxResults() { return $this->maxResults; } /** * Sets maxResults. * * @param int|null $maxResults The value to set. * * @return Criteria */ public function setMaxResults($maxResults) { $this->maxResults = null === $maxResults ? null : (int) $maxResults; return $this; } } collections-1.3.0/lib/Doctrine/Common/Collections/Expr/000077500000000000000000000000001251331140600227665ustar00rootroot00000000000000collections-1.3.0/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php000066400000000000000000000160761251331140600305650ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections\Expr; /** * Walks an expression graph and turns it into a PHP closure. * * This closure can be used with {@Collection#filter()} and is used internally * by {@ArrayCollection#select()}. * * @author Benjamin Eberlei * @since 2.3 */ class ClosureExpressionVisitor extends ExpressionVisitor { /** * Accesses the field of a given object. This field has to be public * directly or indirectly (through an accessor get*, is*, or a magic * method, __get, __call). * * @param object $object * @param string $field * * @return mixed */ public static function getObjectFieldValue($object, $field) { if (is_array($object)) { return $object[$field]; } $accessors = array('get', 'is'); foreach ($accessors as $accessor) { $accessor .= $field; if ( ! method_exists($object, $accessor)) { continue; } return $object->$accessor(); } // __call should be triggered for get. $accessor = $accessors[0] . $field; if (method_exists($object, '__call')) { return $object->$accessor(); } if ($object instanceof \ArrayAccess) { return $object[$field]; } return $object->$field; } /** * Helper for sorting arrays of objects based on multiple fields + orientations. * * @param string $name * @param int $orientation * @param \Closure $next * * @return \Closure */ public static function sortByField($name, $orientation = 1, \Closure $next = null) { if ( ! $next) { $next = function() { return 0; }; } return function ($a, $b) use ($name, $next, $orientation) { $aValue = ClosureExpressionVisitor::getObjectFieldValue($a, $name); $bValue = ClosureExpressionVisitor::getObjectFieldValue($b, $name); if ($aValue === $bValue) { return $next($a, $b); } return (($aValue > $bValue) ? 1 : -1) * $orientation; }; } /** * {@inheritDoc} */ public function walkComparison(Comparison $comparison) { $field = $comparison->getField(); $value = $comparison->getValue()->getValue(); // shortcut for walkValue() switch ($comparison->getOperator()) { case Comparison::EQ: return function ($object) use ($field, $value) { return ClosureExpressionVisitor::getObjectFieldValue($object, $field) === $value; }; case Comparison::NEQ: return function ($object) use ($field, $value) { return ClosureExpressionVisitor::getObjectFieldValue($object, $field) !== $value; }; case Comparison::LT: return function ($object) use ($field, $value) { return ClosureExpressionVisitor::getObjectFieldValue($object, $field) < $value; }; case Comparison::LTE: return function ($object) use ($field, $value) { return ClosureExpressionVisitor::getObjectFieldValue($object, $field) <= $value; }; case Comparison::GT: return function ($object) use ($field, $value) { return ClosureExpressionVisitor::getObjectFieldValue($object, $field) > $value; }; case Comparison::GTE: return function ($object) use ($field, $value) { return ClosureExpressionVisitor::getObjectFieldValue($object, $field) >= $value; }; case Comparison::IN: return function ($object) use ($field, $value) { return in_array(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value); }; case Comparison::NIN: return function ($object) use ($field, $value) { return ! in_array(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value); }; case Comparison::CONTAINS: return function ($object) use ($field, $value) { return false !== strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value); }; default: throw new \RuntimeException("Unknown comparison operator: " . $comparison->getOperator()); } } /** * {@inheritDoc} */ public function walkValue(Value $value) { return $value->getValue(); } /** * {@inheritDoc} */ public function walkCompositeExpression(CompositeExpression $expr) { $expressionList = array(); foreach ($expr->getExpressionList() as $child) { $expressionList[] = $this->dispatch($child); } switch($expr->getType()) { case CompositeExpression::TYPE_AND: return $this->andExpressions($expressionList); case CompositeExpression::TYPE_OR: return $this->orExpressions($expressionList); default: throw new \RuntimeException("Unknown composite " . $expr->getType()); } } /** * @param array $expressions * * @return callable */ private function andExpressions($expressions) { return function ($object) use ($expressions) { foreach ($expressions as $expression) { if ( ! $expression($object)) { return false; } } return true; }; } /** * @param array $expressions * * @return callable */ private function orExpressions($expressions) { return function ($object) use ($expressions) { foreach ($expressions as $expression) { if ($expression($object)) { return true; } } return false; }; } } collections-1.3.0/lib/Doctrine/Common/Collections/Expr/Comparison.php000066400000000000000000000047451251331140600256230ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections\Expr; /** * Comparison of a field with a value by the given operator. * * @author Benjamin Eberlei * @since 2.3 */ class Comparison implements Expression { const EQ = '='; const NEQ = '<>'; const LT = '<'; const LTE = '<='; const GT = '>'; const GTE = '>='; const IS = '='; // no difference with EQ const IN = 'IN'; const NIN = 'NIN'; const CONTAINS = 'CONTAINS'; /** * @var string */ private $field; /** * @var string */ private $op; /** * @var Value */ private $value; /** * @param string $field * @param string $operator * @param mixed $value */ public function __construct($field, $operator, $value) { if ( ! ($value instanceof Value)) { $value = new Value($value); } $this->field = $field; $this->op = $operator; $this->value = $value; } /** * @return string */ public function getField() { return $this->field; } /** * @return Value */ public function getValue() { return $this->value; } /** * @return string */ public function getOperator() { return $this->op; } /** * {@inheritDoc} */ public function visit(ExpressionVisitor $visitor) { return $visitor->walkComparison($this); } } collections-1.3.0/lib/Doctrine/Common/Collections/Expr/CompositeExpression.php000066400000000000000000000047451251331140600275330ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections\Expr; /** * Expression of Expressions combined by AND or OR operation. * * @author Benjamin Eberlei * @since 2.3 */ class CompositeExpression implements Expression { const TYPE_AND = 'AND'; const TYPE_OR = 'OR'; /** * @var string */ private $type; /** * @var Expression[] */ private $expressions = array(); /** * @param string $type * @param array $expressions * * @throws \RuntimeException */ public function __construct($type, array $expressions) { $this->type = $type; foreach ($expressions as $expr) { if ($expr instanceof Value) { throw new \RuntimeException("Values are not supported expressions as children of and/or expressions."); } if ( ! ($expr instanceof Expression)) { throw new \RuntimeException("No expression given to CompositeExpression."); } $this->expressions[] = $expr; } } /** * Returns the list of expressions nested in this composite. * * @return Expression[] */ public function getExpressionList() { return $this->expressions; } /** * @return string */ public function getType() { return $this->type; } /** * {@inheritDoc} */ public function visit(ExpressionVisitor $visitor) { return $visitor->walkCompositeExpression($this); } } collections-1.3.0/lib/Doctrine/Common/Collections/Expr/Expression.php000066400000000000000000000024401251331140600256360ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections\Expr; /** * Expression for the {@link Selectable} interface. * * @author Benjamin Eberlei */ interface Expression { /** * @param ExpressionVisitor $visitor * * @return mixed */ public function visit(ExpressionVisitor $visitor); } collections-1.3.0/lib/Doctrine/Common/Collections/Expr/ExpressionVisitor.php000066400000000000000000000051321251331140600272170ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections\Expr; /** * An Expression visitor walks a graph of expressions and turns them into a * query for the underlying implementation. * * @author Benjamin Eberlei */ abstract class ExpressionVisitor { /** * Converts a comparison expression into the target query language output. * * @param Comparison $comparison * * @return mixed */ abstract public function walkComparison(Comparison $comparison); /** * Converts a value expression into the target query language part. * * @param Value $value * * @return mixed */ abstract public function walkValue(Value $value); /** * Converts a composite expression into the target query language output. * * @param CompositeExpression $expr * * @return mixed */ abstract public function walkCompositeExpression(CompositeExpression $expr); /** * Dispatches walking an expression to the appropriate handler. * * @param Expression $expr * * @return mixed * * @throws \RuntimeException */ public function dispatch(Expression $expr) { switch (true) { case ($expr instanceof Comparison): return $this->walkComparison($expr); case ($expr instanceof Value): return $this->walkValue($expr); case ($expr instanceof CompositeExpression): return $this->walkCompositeExpression($expr); default: throw new \RuntimeException("Unknown Expression " . get_class($expr)); } } } collections-1.3.0/lib/Doctrine/Common/Collections/Expr/Value.php000066400000000000000000000027461251331140600245640ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections\Expr; class Value implements Expression { /** * @var mixed */ private $value; /** * @param mixed $value */ public function __construct($value) { $this->value = $value; } /** * @return mixed */ public function getValue() { return $this->value; } /** * {@inheritDoc} */ public function visit(ExpressionVisitor $visitor) { return $visitor->walkValue($this); } } collections-1.3.0/lib/Doctrine/Common/Collections/ExpressionBuilder.php000066400000000000000000000103251251331140600262300ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections; use Doctrine\Common\Collections\Expr\Comparison; use Doctrine\Common\Collections\Expr\CompositeExpression; use Doctrine\Common\Collections\Expr\Value; /** * Builder for Expressions in the {@link Selectable} interface. * * Important Notice for interoperable code: You have to use scalar * values only for comparisons, otherwise the behavior of the comparision * may be different between implementations (Array vs ORM vs ODM). * * @author Benjamin Eberlei * @since 2.3 */ class ExpressionBuilder { /** * @param mixed $x * * @return CompositeExpression */ public function andX($x = null) { return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args()); } /** * @param mixed $x * * @return CompositeExpression */ public function orX($x = null) { return new CompositeExpression(CompositeExpression::TYPE_OR, func_get_args()); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function eq($field, $value) { return new Comparison($field, Comparison::EQ, new Value($value)); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function gt($field, $value) { return new Comparison($field, Comparison::GT, new Value($value)); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function lt($field, $value) { return new Comparison($field, Comparison::LT, new Value($value)); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function gte($field, $value) { return new Comparison($field, Comparison::GTE, new Value($value)); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function lte($field, $value) { return new Comparison($field, Comparison::LTE, new Value($value)); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function neq($field, $value) { return new Comparison($field, Comparison::NEQ, new Value($value)); } /** * @param string $field * * @return Comparison */ public function isNull($field) { return new Comparison($field, Comparison::EQ, new Value(null)); } /** * @param string $field * @param mixed $values * * @return Comparison */ public function in($field, array $values) { return new Comparison($field, Comparison::IN, new Value($values)); } /** * @param string $field * @param mixed $values * * @return Comparison */ public function notIn($field, array $values) { return new Comparison($field, Comparison::NIN, new Value($values)); } /** * @param string $field * @param mixed $value * * @return Comparison */ public function contains($field, $value) { return new Comparison($field, Comparison::CONTAINS, new Value($value)); } } collections-1.3.0/lib/Doctrine/Common/Collections/Selectable.php000066400000000000000000000036731251331140600246350ustar00rootroot00000000000000. */ namespace Doctrine\Common\Collections; /** * Interface for collections that allow efficient filtering with an expression API. * * Goal of this interface is a backend independent method to fetch elements * from a collections. {@link Expression} is crafted in a way that you can * implement queries from both in-memory and database-backed collections. * * For database backed collections this allows very efficient access by * utilizing the query APIs, for example SQL in the ORM. Applications using * this API can implement efficient database access without having to ask the * EntityManager or Repositories. * * @author Benjamin Eberlei * @since 2.3 */ interface Selectable { /** * Selects all elements from a selectable that match the expression and * returns a new collection containing these elements. * * @param Criteria $criteria * * @return Collection */ public function matching(Criteria $criteria); } collections-1.3.0/phpunit.xml.dist000066400000000000000000000014671251331140600172100ustar00rootroot00000000000000 ./tests/Doctrine/ ./lib/Doctrine/ performance collections-1.3.0/tests/000077500000000000000000000000001251331140600151675ustar00rootroot00000000000000collections-1.3.0/tests/Doctrine/000077500000000000000000000000001251331140600167365ustar00rootroot00000000000000collections-1.3.0/tests/Doctrine/Tests/000077500000000000000000000000001251331140600200405ustar00rootroot00000000000000collections-1.3.0/tests/Doctrine/Tests/Common/000077500000000000000000000000001251331140600212705ustar00rootroot00000000000000collections-1.3.0/tests/Doctrine/Tests/Common/Collections/000077500000000000000000000000001251331140600235465ustar00rootroot00000000000000collections-1.3.0/tests/Doctrine/Tests/Common/Collections/AbstractLazyCollectionTest.php000066400000000000000000000007731251331140600315450ustar00rootroot00000000000000assertFalse($collection->isInitialized()); $this->assertCount(3, $collection); $collection->add('bar'); $this->assertTrue($collection->isInitialized()); $this->assertCount(4, $collection); } } collections-1.3.0/tests/Doctrine/Tests/Common/Collections/ArrayCollectionTest.php000066400000000000000000000231521251331140600302140ustar00rootroot00000000000000. */ namespace Doctrine\Tests\Common\Collections; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; /** * Tests for {@see \Doctrine\Common\Collections\ArrayCollection} * * @covers \Doctrine\Common\Collections\ArrayCollection */ class ArrayCollectionTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider provideDifferentElements */ public function testToArray($elements) { $collection = new ArrayCollection($elements); $this->assertSame($elements, $collection->toArray()); } /** * @dataProvider provideDifferentElements */ public function testFirst($elements) { $collection = new ArrayCollection($elements); $this->assertSame(reset($elements), $collection->first()); } /** * @dataProvider provideDifferentElements */ public function testLast($elements) { $collection = new ArrayCollection($elements); $this->assertSame(end($elements), $collection->last()); } /** * @dataProvider provideDifferentElements */ public function testKey($elements) { $collection = new ArrayCollection($elements); $this->assertSame(key($elements), $collection->key()); next($elements); $collection->next(); $this->assertSame(key($elements), $collection->key()); } /** * @dataProvider provideDifferentElements */ public function testNext($elements) { $collection = new ArrayCollection($elements); while (true) { $collectionNext = $collection->next(); $arrayNext = next($elements); if(!$collectionNext || !$arrayNext) { break; } $this->assertSame($arrayNext, $collectionNext, "Returned value of ArrayCollection::next() and next() not match"); $this->assertSame(key($elements), $collection->key(), "Keys not match"); $this->assertSame(current($elements), $collection->current(), "Current values not match"); } } /** * @dataProvider provideDifferentElements */ public function testCurrent($elements) { $collection = new ArrayCollection($elements); $this->assertSame(current($elements), $collection->current()); next($elements); $collection->next(); $this->assertSame(current($elements), $collection->current()); } /** * @dataProvider provideDifferentElements */ public function testGetKeys($elements) { $collection = new ArrayCollection($elements); $this->assertSame(array_keys($elements), $collection->getKeys()); } /** * @dataProvider provideDifferentElements */ public function testGetValues($elements) { $collection = new ArrayCollection($elements); $this->assertSame(array_values($elements), $collection->getValues()); } /** * @dataProvider provideDifferentElements */ public function testCount($elements) { $collection = new ArrayCollection($elements); $this->assertSame(count($elements), $collection->count()); } /** * @dataProvider provideDifferentElements */ public function testIterator($elements) { $collection = new ArrayCollection($elements); $iterations = 0; foreach($collection->getIterator() as $key => $item) { $this->assertSame($elements[$key], $item, "Item {$key} not match"); $iterations++; } $this->assertEquals(count($elements), $iterations, "Number of iterations not match"); } /** * @return array */ public function provideDifferentElements() { return array( 'indexed' => array(array(1, 2, 3, 4, 5)), 'associative' => array(array('A' => 'a', 'B' => 'b', 'C' => 'c')), 'mixed' => array(array('A' => 'a', 1, 'B' => 'b', 2, 3)), ); } public function testRemove() { $elements = array(1, 'A' => 'a', 2, 'B' => 'b', 3); $collection = new ArrayCollection($elements); $this->assertEquals(1, $collection->remove(0)); unset($elements[0]); $this->assertEquals(null, $collection->remove('non-existent')); unset($elements['non-existent']); $this->assertEquals(2, $collection->remove(1)); unset($elements[1]); $this->assertEquals('a', $collection->remove('A')); unset($elements['A']); $this->assertEquals($elements, $collection->toArray()); } public function testRemoveElement() { $elements = array(1, 'A' => 'a', 2, 'B' => 'b', 3, 'A2' => 'a', 'B2' => 'b'); $collection = new ArrayCollection($elements); $this->assertTrue($collection->removeElement(1)); unset($elements[0]); $this->assertFalse($collection->removeElement('non-existent')); $this->assertTrue($collection->removeElement('a')); unset($elements['A']); $this->assertTrue($collection->removeElement('a')); unset($elements['A2']); $this->assertEquals($elements, $collection->toArray()); } public function testContainsKey() { $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'B2' => 'b'); $collection = new ArrayCollection($elements); $this->assertTrue($collection->containsKey(0), "Contains index 0"); $this->assertTrue($collection->containsKey('A'), "Contains key \"A\""); $this->assertTrue($collection->containsKey('null'), "Contains key \"null\", with value null"); $this->assertFalse($collection->containsKey('non-existent'), "Doesn't contain key"); } public function testEmpty() { $collection = new ArrayCollection(); $this->assertTrue($collection->isEmpty(), "Empty collection"); $collection->add(1); $this->assertFalse($collection->isEmpty(), "Not empty collection"); } public function testContains() { $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); $collection = new ArrayCollection($elements); $this->assertTrue($collection->contains(0), "Contains Zero"); $this->assertTrue($collection->contains('a'), "Contains \"a\""); $this->assertTrue($collection->contains(null), "Contains Null"); $this->assertFalse($collection->contains('non-existent'), "Doesn't contain an element"); } public function testExists() { $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); $collection = new ArrayCollection($elements); $this->assertTrue($collection->exists(function($key, $element) { return $key == 'A' && $element == 'a'; }), "Element exists"); $this->assertFalse($collection->exists(function($key, $element) { return $key == 'non-existent' && $element == 'non-existent'; }), "Element not exists"); } public function testIndexOf() { $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); $collection = new ArrayCollection($elements); $this->assertSame(array_search(2, $elements, true), $collection->indexOf(2), 'Index of 2'); $this->assertSame(array_search(null, $elements, true), $collection->indexOf(null), 'Index of null'); $this->assertSame(array_search('non-existent', $elements, true), $collection->indexOf('non-existent'), 'Index of non existent'); } public function testGet() { $elements = array(1, 'A' => 'a', 2, 'null' => null, 3, 'A2' => 'a', 'zero' => 0); $collection = new ArrayCollection($elements); $this->assertSame(2, $collection->get(1), 'Get element by index'); $this->assertSame('a', $collection->get('A'), 'Get element by name'); $this->assertSame(null, $collection->get('non-existent'), 'Get non existent element'); } public function testMatchingWithSortingPreservesyKeys() { $object1 = new \stdClass(); $object2 = new \stdClass(); $object1->sortField = 2; $object2->sortField = 1; $collection = new ArrayCollection(array( 'object1' => $object1, 'object2' => $object2, )); $this->assertSame( array( 'object2' => $object2, 'object1' => $object1, ), $collection ->matching(new Criteria(null, array('sortField' => Criteria::ASC))) ->toArray() ); } } collections-1.3.0/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php000066400000000000000000000172771251331140600322110ustar00rootroot00000000000000. */ namespace Doctrine\Tests\Common\Collections; use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; use Doctrine\Common\Collections\ExpressionBuilder; /** * @group DDC-1637 */ class ClosureExpressionVisitorTest extends \PHPUnit_Framework_TestCase { /** * @var ClosureExpressionVisitor */ private $visitor; /** * @var ExpressionBuilder */ private $builder; protected function setUp() { $this->visitor = new ClosureExpressionVisitor(); $this->builder = new ExpressionBuilder(); } public function testGetObjectFieldValueIsAccessor() { $object = new TestObject(1, 2, true); $this->assertTrue($this->visitor->getObjectFieldValue($object, 'baz')); } public function testGetObjectFieldValueMagicCallMethod() { $object = new TestObject(1, 2, true, 3); $this->assertEquals(3, $this->visitor->getObjectFieldValue($object, 'qux')); } public function testWalkEqualsComparison() { $closure = $this->visitor->walkComparison($this->builder->eq("foo", 1)); $this->assertTrue($closure(new TestObject(1))); $this->assertFalse($closure(new TestObject(2))); } public function testWalkNotEqualsComparison() { $closure = $this->visitor->walkComparison($this->builder->neq("foo", 1)); $this->assertFalse($closure(new TestObject(1))); $this->assertTrue($closure(new TestObject(2))); } public function testWalkLessThanComparison() { $closure = $this->visitor->walkComparison($this->builder->lt("foo", 1)); $this->assertFalse($closure(new TestObject(1))); $this->assertTrue($closure(new TestObject(0))); } public function testWalkLessThanEqualsComparison() { $closure = $this->visitor->walkComparison($this->builder->lte("foo", 1)); $this->assertFalse($closure(new TestObject(2))); $this->assertTrue($closure(new TestObject(1))); $this->assertTrue($closure(new TestObject(0))); } public function testWalkGreaterThanEqualsComparison() { $closure = $this->visitor->walkComparison($this->builder->gte("foo", 1)); $this->assertTrue($closure(new TestObject(2))); $this->assertTrue($closure(new TestObject(1))); $this->assertFalse($closure(new TestObject(0))); } public function testWalkGreaterThanComparison() { $closure = $this->visitor->walkComparison($this->builder->gt("foo", 1)); $this->assertTrue($closure(new TestObject(2))); $this->assertFalse($closure(new TestObject(1))); $this->assertFalse($closure(new TestObject(0))); } public function testWalkInComparison() { $closure = $this->visitor->walkComparison($this->builder->in("foo", array(1, 2, 3))); $this->assertTrue($closure(new TestObject(2))); $this->assertTrue($closure(new TestObject(1))); $this->assertFalse($closure(new TestObject(0))); } public function testWalkNotInComparison() { $closure = $this->visitor->walkComparison($this->builder->notIn("foo", array(1, 2, 3))); $this->assertFalse($closure(new TestObject(1))); $this->assertFalse($closure(new TestObject(2))); $this->assertTrue($closure(new TestObject(0))); $this->assertTrue($closure(new TestObject(4))); } public function testWalkContainsComparison() { $closure = $this->visitor->walkComparison($this->builder->contains('foo', 'hello')); $this->assertTrue($closure(new TestObject('hello world'))); $this->assertFalse($closure(new TestObject('world'))); } public function testWalkAndCompositeExpression() { $closure = $this->visitor->walkCompositeExpression( $this->builder->andX( $this->builder->eq("foo", 1), $this->builder->eq("bar", 1) ) ); $this->assertTrue($closure(new TestObject(1, 1))); $this->assertFalse($closure(new TestObject(1, 0))); $this->assertFalse($closure(new TestObject(0, 1))); $this->assertFalse($closure(new TestObject(0, 0))); } public function testWalkOrCompositeExpression() { $closure = $this->visitor->walkCompositeExpression( $this->builder->orX( $this->builder->eq("foo", 1), $this->builder->eq("bar", 1) ) ); $this->assertTrue($closure(new TestObject(1, 1))); $this->assertTrue($closure(new TestObject(1, 0))); $this->assertTrue($closure(new TestObject(0, 1))); $this->assertFalse($closure(new TestObject(0, 0))); } public function testSortByFieldAscending() { $objects = array(new TestObject("b"), new TestObject("a"), new TestObject("c")); $sort = ClosureExpressionVisitor::sortByField("foo"); usort($objects, $sort); $this->assertEquals("a", $objects[0]->getFoo()); $this->assertEquals("b", $objects[1]->getFoo()); $this->assertEquals("c", $objects[2]->getFoo()); } public function testSortByFieldDescending() { $objects = array(new TestObject("b"), new TestObject("a"), new TestObject("c")); $sort = ClosureExpressionVisitor::sortByField("foo", -1); usort($objects, $sort); $this->assertEquals("c", $objects[0]->getFoo()); $this->assertEquals("b", $objects[1]->getFoo()); $this->assertEquals("a", $objects[2]->getFoo()); } public function testSortDelegate() { $objects = array(new TestObject("a", "c"), new TestObject("a", "b"), new TestObject("a", "a")); $sort = ClosureExpressionVisitor::sortByField("bar", 1); $sort = ClosureExpressionVisitor::sortByField("foo", 1, $sort); usort($objects, $sort); $this->assertEquals("a", $objects[0]->getBar()); $this->assertEquals("b", $objects[1]->getBar()); $this->assertEquals("c", $objects[2]->getBar()); } public function testArrayComparison() { $closure = $this->visitor->walkComparison($this->builder->eq("foo", 42)); $this->assertTrue($closure(array('foo' => 42))); } } class TestObject { private $foo; private $bar; private $baz; private $qux; public function __construct($foo = null, $bar = null, $baz = null, $qux = null) { $this->foo = $foo; $this->bar = $bar; $this->baz = $baz; $this->qux = $qux; } public function __call($name, $arguments) { if ('getqux' === $name) { return $this->qux; } } public function getFoo() { return $this->foo; } public function getBar() { return $this->bar; } public function isBaz() { return $this->baz; } } collections-1.3.0/tests/Doctrine/Tests/Common/Collections/CollectionTest.php000066400000000000000000000172541251331140600272230ustar00rootroot00000000000000collection = new ArrayCollection(); } public function testIssetAndUnset() { $this->assertFalse(isset($this->collection[0])); $this->collection->add('testing'); $this->assertTrue(isset($this->collection[0])); unset($this->collection[0]); $this->assertFalse(isset($this->collection[0])); } public function testToString() { $this->collection->add('testing'); $this->assertTrue(is_string((string) $this->collection)); } public function testRemovingNonExistentEntryReturnsNull() { $this->assertEquals(null, $this->collection->remove('testing_does_not_exist')); } public function testExists() { $this->collection->add("one"); $this->collection->add("two"); $exists = $this->collection->exists(function($k, $e) { return $e == "one"; }); $this->assertTrue($exists); $exists = $this->collection->exists(function($k, $e) { return $e == "other"; }); $this->assertFalse($exists); } public function testMap() { $this->collection->add(1); $this->collection->add(2); $res = $this->collection->map(function($e) { return $e * 2; }); $this->assertEquals(array(2, 4), $res->toArray()); } public function testFilter() { $this->collection->add(1); $this->collection->add("foo"); $this->collection->add(3); $res = $this->collection->filter(function($e) { return is_numeric($e); }); $this->assertEquals(array(0 => 1, 2 => 3), $res->toArray()); } public function testFirstAndLast() { $this->collection->add('one'); $this->collection->add('two'); $this->assertEquals($this->collection->first(), 'one'); $this->assertEquals($this->collection->last(), 'two'); } public function testArrayAccess() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->assertEquals($this->collection[0], 'one'); $this->assertEquals($this->collection[1], 'two'); unset($this->collection[0]); $this->assertEquals($this->collection->count(), 1); } public function testContainsKey() { $this->collection[5] = 'five'; $this->assertTrue($this->collection->containsKey(5)); } public function testContains() { $this->collection[0] = 'test'; $this->assertTrue($this->collection->contains('test')); } public function testSearch() { $this->collection[0] = 'test'; $this->assertEquals(0, $this->collection->indexOf('test')); } public function testGet() { $this->collection[0] = 'test'; $this->assertEquals('test', $this->collection->get(0)); } public function testGetKeys() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->assertEquals(array(0, 1), $this->collection->getKeys()); } public function testGetValues() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->assertEquals(array('one', 'two'), $this->collection->getValues()); } public function testCount() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->assertEquals($this->collection->count(), 2); $this->assertEquals(count($this->collection), 2); } public function testForAll() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->assertEquals($this->collection->forAll(function($k, $e) { return is_string($e); }), true); $this->assertEquals($this->collection->forAll(function($k, $e) { return is_array($e); }), false); } public function testPartition() { $this->collection[] = true; $this->collection[] = false; $partition = $this->collection->partition(function($k, $e) { return $e == true; }); $this->assertEquals($partition[0][0], true); $this->assertEquals($partition[1][0], false); } public function testClear() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->collection->clear(); $this->assertEquals($this->collection->isEmpty(), true); } public function testRemove() { $this->collection[] = 'one'; $this->collection[] = 'two'; $el = $this->collection->remove(0); $this->assertEquals('one', $el); $this->assertEquals($this->collection->contains('one'), false); $this->assertNull($this->collection->remove(0)); } public function testRemoveElement() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->assertTrue($this->collection->removeElement('two')); $this->assertFalse($this->collection->contains('two')); $this->assertFalse($this->collection->removeElement('two')); } public function testSlice() { $this->collection[] = 'one'; $this->collection[] = 'two'; $this->collection[] = 'three'; $slice = $this->collection->slice(0, 1); $this->assertInternalType('array', $slice); $this->assertEquals(array('one'), $slice); $slice = $this->collection->slice(1); $this->assertEquals(array(1 => 'two', 2 => 'three'), $slice); $slice = $this->collection->slice(1, 1); $this->assertEquals(array(1 => 'two'), $slice); } public function fillMatchingFixture() { $std1 = new \stdClass(); $std1->foo = "bar"; $this->collection[] = $std1; $std2 = new \stdClass(); $std2->foo = "baz"; $this->collection[] = $std2; } /** * @group DDC-1637 */ public function testMatching() { $this->fillMatchingFixture(); $col = $this->collection->matching(new Criteria(Criteria::expr()->eq("foo", "bar"))); $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col); $this->assertNotSame($col, $this->collection); $this->assertEquals(1, count($col)); } /** * @group DDC-1637 */ public function testMatchingOrdering() { $this->fillMatchingFixture(); $col = $this->collection->matching(new Criteria(null, array('foo' => 'DESC'))); $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col); $this->assertNotSame($col, $this->collection); $this->assertEquals(2, count($col)); $this->assertEquals('baz', $col->first()->foo); $this->assertEquals('bar', $col->last()->foo); } /** * @group DDC-1637 */ public function testMatchingSlice() { $this->fillMatchingFixture(); $col = $this->collection->matching(new Criteria(null, null, 1, 1)); $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $col); $this->assertNotSame($col, $this->collection); $this->assertEquals(1, count($col)); $this->assertEquals('baz', $col[0]->foo); } public function testCanRemoveNullValuesByKey() { $this->collection->add(null); $this->collection->remove(0); $this->assertTrue($this->collection->isEmpty()); } public function testCanVerifyExistingKeysWithNullValues() { $this->collection->set('key', null); $this->assertTrue($this->collection->containsKey('key')); } } collections-1.3.0/tests/Doctrine/Tests/Common/Collections/CriteriaTest.php000066400000000000000000000050671251331140600266710ustar00rootroot00000000000000assertInstanceOf('Doctrine\Common\Collections\Criteria', $criteria); } public function testConstructor() { $expr = new Comparison("field", "=", "value"); $criteria = new Criteria($expr, array("foo" => "ASC"), 10, 20); $this->assertSame($expr, $criteria->getWhereExpression()); $this->assertEquals(array("foo" => "ASC"), $criteria->getOrderings()); $this->assertEquals(10, $criteria->getFirstResult()); $this->assertEquals(20, $criteria->getMaxResults()); } public function testWhere() { $expr = new Comparison("field", "=", "value"); $criteria = new Criteria(); $criteria->where($expr); $this->assertSame($expr, $criteria->getWhereExpression()); } public function testAndWhere() { $expr = new Comparison("field", "=", "value"); $criteria = new Criteria(); $criteria->where($expr); $expr = $criteria->getWhereExpression(); $criteria->andWhere($expr); $where = $criteria->getWhereExpression(); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $where); $this->assertEquals(CompositeExpression::TYPE_AND, $where->getType()); $this->assertSame(array($expr, $expr), $where->getExpressionList()); } public function testOrWhere() { $expr = new Comparison("field", "=", "value"); $criteria = new Criteria(); $criteria->where($expr); $expr = $criteria->getWhereExpression(); $criteria->orWhere($expr); $where = $criteria->getWhereExpression(); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $where); $this->assertEquals(CompositeExpression::TYPE_OR, $where->getType()); $this->assertSame(array($expr, $expr), $where->getExpressionList()); } public function testOrderings() { $criteria = Criteria::create() ->orderBy(array("foo" => "ASC")); $this->assertEquals(array("foo" => "ASC"), $criteria->getOrderings()); } public function testExpr() { $this->assertInstanceOf('Doctrine\Common\Collections\ExpressionBuilder', Criteria::expr()); } } collections-1.3.0/tests/Doctrine/Tests/Common/Collections/ExpressionBuilderTest.php000066400000000000000000000071321251331140600305700ustar00rootroot00000000000000builder = new ExpressionBuilder(); } public function testAndX() { $expr = $this->builder->andX($this->builder->eq("a", "b")); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $expr); $this->assertEquals(CompositeExpression::TYPE_AND, $expr->getType()); } public function testOrX() { $expr = $this->builder->orX($this->builder->eq("a", "b")); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\CompositeExpression', $expr); $this->assertEquals(CompositeExpression::TYPE_OR, $expr->getType()); } public function testInvalidAndXArgument() { $this->setExpectedException("RuntimeException"); $this->builder->andX("foo"); } public function testEq() { $expr = $this->builder->eq("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::EQ, $expr->getOperator()); } public function testNeq() { $expr = $this->builder->neq("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::NEQ, $expr->getOperator()); } public function testLt() { $expr = $this->builder->lt("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::LT, $expr->getOperator()); } public function testGt() { $expr = $this->builder->gt("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::GT, $expr->getOperator()); } public function testGte() { $expr = $this->builder->gte("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::GTE, $expr->getOperator()); } public function testLte() { $expr = $this->builder->lte("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::LTE, $expr->getOperator()); } public function testIn() { $expr = $this->builder->in("a", array("b")); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::IN, $expr->getOperator()); } public function testNotIn() { $expr = $this->builder->notIn("a", array("b")); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::NIN, $expr->getOperator()); } public function testIsNull() { $expr = $this->builder->isNull("a"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::EQ, $expr->getOperator()); } public function testContains() { $expr = $this->builder->contains("a", "b"); $this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr); $this->assertEquals(Comparison::CONTAINS, $expr->getOperator()); } } collections-1.3.0/tests/Doctrine/Tests/LazyArrayCollection.php000066400000000000000000000007441251331140600245100ustar00rootroot00000000000000collection = new ArrayCollection(array('a', 'b', 'c')); } } collections-1.3.0/tests/Doctrine/Tests/TestInit.php000066400000000000000000000007751251331140600223250ustar00rootroot00000000000000