1
0
Fork 0
forked from lino/radar-wp

Initial import.

This commit is contained in:
ekes 2015-02-24 16:25:12 +01:00
commit 86383280c9
428 changed files with 68738 additions and 0 deletions

View file

@ -0,0 +1,500 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Collections;
use Closure, ArrayIterator;
use Doctrine\Common\Collections\Expr\Expression;
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
/**
* An ArrayCollection is a Collection implementation that wraps a regular PHP array.
*
* @since 2.0
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
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;
}
/**
* Gets the PHP array representation of this collection.
*
* @return array The PHP array representation of this collection.
*/
public function toArray()
{
return $this->_elements;
}
/**
* Sets the internal iterator to the first element in the collection and
* returns this element.
*
* @return mixed
*/
public function first()
{
return reset($this->_elements);
}
/**
* Sets the internal iterator to the last element in the collection and
* returns this element.
*
* @return mixed
*/
public function last()
{
return end($this->_elements);
}
/**
* Gets the current key/index at the current internal iterator position.
*
* @return mixed
*/
public function key()
{
return key($this->_elements);
}
/**
* Moves the internal iterator position to the next element.
*
* @return mixed
*/
public function next()
{
return next($this->_elements);
}
/**
* Gets the element of the collection at the current internal iterator position.
*
* @return mixed
*/
public function current()
{
return current($this->_elements);
}
/**
* Removes an element with a specific key/index from the collection.
*
* @param mixed $key
* @return mixed The removed element or NULL, if no element exists for the given key.
*/
public function remove($key)
{
if (isset($this->_elements[$key])) {
$removed = $this->_elements[$key];
unset($this->_elements[$key]);
return $removed;
}
return null;
}
/**
* 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)
{
$key = array_search($element, $this->_elements, true);
if ($key !== false) {
unset($this->_elements[$key]);
return true;
}
return false;
}
/**
* ArrayAccess implementation of offsetExists()
*
* @see containsKey()
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset)
{
return $this->containsKey($offset);
}
/**
* ArrayAccess implementation of offsetGet()
*
* @see get()
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* ArrayAccess implementation of offsetSet()
*
* @see add()
* @see set()
*
* @param mixed $offset
* @param mixed $value
* @return bool
*/
public function offsetSet($offset, $value)
{
if ( ! isset($offset)) {
return $this->add($value);
}
return $this->set($offset, $value);
}
/**
* ArrayAccess implementation of offsetUnset()
*
* @see remove()
*
* @param mixed $offset
* @return mixed
*/
public function offsetUnset($offset)
{
return $this->remove($offset);
}
/**
* Checks whether the collection contains a specific key/index.
*
* @param mixed $key The key to check for.
* @return boolean TRUE if the given key/index exists, FALSE otherwise.
*/
public function containsKey($key)
{
return isset($this->_elements[$key]);
}
/**
* Checks whether the given element is contained in the collection.
* Only element values are compared, not keys. 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
* @return boolean TRUE if the given element is contained in the collection,
* FALSE otherwise.
*/
public function contains($element)
{
foreach ($this->_elements as $collectionElement) {
if ($element === $collectionElement) {
return true;
}
}
return false;
}
/**
* 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)
{
foreach ($this->_elements as $key => $element) {
if ($p($key, $element)) {
return true;
}
}
return false;
}
/**
* Searches for a given element and, if found, returns the corresponding key/index
* of that 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.
*/
public function indexOf($element)
{
return array_search($element, $this->_elements, true);
}
/**
* Gets the element with the given key/index.
*
* @param mixed $key The key.
* @return mixed The element or NULL, if no element exists for the given key.
*/
public function get($key)
{
if (isset($this->_elements[$key])) {
return $this->_elements[$key];
}
return null;
}
/**
* Gets all keys/indexes of the collection elements.
*
* @return array
*/
public function getKeys()
{
return array_keys($this->_elements);
}
/**
* Gets all elements.
*
* @return array
*/
public function getValues()
{
return array_values($this->_elements);
}
/**
* Returns the number of elements in the collection.
*
* Implementation of the Countable interface.
*
* @return integer The number of elements in the collection.
*/
public function count()
{
return count($this->_elements);
}
/**
* Adds/sets an element in the collection at the index / with the specified key.
*
* When the collection is a Map this is like put(key,value)/add(key,value).
* When the collection is a List this is like add(position,value).
*
* @param mixed $key
* @param mixed $value
*/
public function set($key, $value)
{
$this->_elements[$key] = $value;
}
/**
* Adds an element to the collection.
*
* @param mixed $value
* @return boolean Always TRUE.
*/
public function add($value)
{
$this->_elements[] = $value;
return true;
}
/**
* Checks whether the collection is empty.
*
* Note: This is preferable over count() == 0.
*
* @return boolean TRUE if the collection is empty, FALSE otherwise.
*/
public function isEmpty()
{
return ! $this->_elements;
}
/**
* Gets an iterator for iterating over the elements in the collection.
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->_elements);
}
/**
* 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)
{
return new static(array_map($func, $this->_elements));
}
/**
* 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)
{
return new static(array_filter($this->_elements, $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.
*/
public function forAll(Closure $p)
{
foreach ($this->_elements as $key => $element) {
if ( ! $p($key, $element)) {
return false;
}
}
return true;
}
/**
* 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)
{
$coll1 = $coll2 = array();
foreach ($this->_elements as $key => $element) {
if ($p($key, $element)) {
$coll1[$key] = $element;
} else {
$coll2[$key] = $element;
}
}
return array(new static($coll1), new static($coll2));
}
/**
* Returns a string representation of this object.
*
* @return string
*/
public function __toString()
{
return __CLASS__ . '@' . spl_object_hash($this);
}
/**
* Clears the collection.
*/
public function clear()
{
$this->_elements = array();
}
/**
* Extract 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
* @param int $length
* @return array
*/
public function slice($offset, $length = null)
{
return array_slice($this->_elements, $offset, $length, true);
}
/**
* Select all elements from a selectable that match the criteria and
* return a new collection containing these elements.
*
* @param Criteria $criteria
* @return Collection
*/
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()) {
$next = null;
foreach (array_reverse($orderings) as $field => $ordering) {
$next = ClosureExpressionVisitor::sortByField($field, $ordering == 'DESC' ? -1 : 1, $next);
}
usort($filtered, $next);
}
$offset = $criteria->getFirstResult();
$length = $criteria->getMaxResults();
if ($offset || $length) {
$filtered = array_slice($filtered, (int)$offset, $length);
}
return new static($filtered);
}
}

View file

@ -0,0 +1,243 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
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 <b>ordered map</b> 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 preferrable.
* 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 <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
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.
*/
function add($element);
/**
* Clears the collection, removing all elements.
*/
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.
*/
function contains($element);
/**
* Checks whether the collection is empty (contains no elements).
*
* @return boolean TRUE if the collection is empty, FALSE otherwise.
*/
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.
*/
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.
*/
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 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.
*
* @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);
/**
* 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.
*
* @return mixed
*/
function first();
/**
* Sets the internal iterator to the last element in the collection and
* returns this element.
*
* @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);
/**
* Extract 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
* @param int $length
* @return array
*/
function slice($offset, $length = null);
}

View file

@ -0,0 +1,240 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Collections;
use Doctrine\Common\Collections\Expr\Expression;
use Doctrine\Common\Collections\Expr\CompositeExpression;
/**
* Criteria for filtering Selectable collections.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @since 2.3
*/
class Criteria
{
/**
* @var string
*/
const ASC = 'ASC';
/**
* @var string
*/
const DESC = 'DESC';
/**
* @var \Doctrine\Common\Collections\ExpressionBuilder
*/
private static $expressionBuilder;
/**
* @var \Doctrine\Common\Collections\Expr\Expression
*/
private $expression;
/**
* @var array|null
*/
private $orderings;
/**
* @var int
*/
private $firstResult;
/**
* @var int
*/
private $maxResults;
/**
* Creates an instance of the class.
*
* @return Criteria
*/
public static function create()
{
return new static();
}
/**
* Return the expression builder.
*
* @return \Doctrine\Common\Collections\ExpressionBuilder
*/
public static function expr()
{
if (self::$expressionBuilder === null) {
self::$expressionBuilder = new ExpressionBuilder();
}
return self::$expressionBuilder;
}
/**
* Construct new criteria
*
* @param Expression $expression
* @param array $orderings
* @param int $firstResult
* @param int $maxResults
*/
public function __construct(Expression $expression = null, array $orderings = null, $firstResult = null, $maxResults = null)
{
$this->expression = $expression;
$this->orderings = $orderings;
$this->firstResult = $firstResult;
$this->maxResults = $maxResults;
}
/**
* Set the where expression to evaluate when this criteria is searched for.
*
* @param Expression
* @return Criteria
*/
public function where(Expression $expression)
{
$this->expression = $expression;
return $this;
}
/**
* Append the where expression to evaluate when this criteria is searched for
* using an AND with previous expression.
*
* @param 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;
}
/**
* Append the where expression to evaluate when this criteria is searched for
* using an OR with previous expression.
*
* @param 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;
}
/**
* Get the expression attached to this criteria.
*
* @return Expression|null
*/
public function getWhereExpression()
{
return $this->expression;
}
/**
* Get current orderings of this Criteria
*
* @return array
*/
public function getOrderings()
{
return $this->orderings;
}
/**
* Set 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 array
* @return Criteria
*/
public function orderBy(array $orderings)
{
$this->orderings = $orderings;
return $this;
}
/**
* Get current first result option of the critera.
*
* @return firstResult.
*/
public function getFirstResult()
{
return $this->firstResult;
}
/**
* Set number of first result that this criteria should return.
*
* @param firstResult the value to set.
* @return Criteria
*/
public function setFirstResult($firstResult)
{
$this->firstResult = $firstResult;
return $this;
}
/**
* Get maxResults.
*
* @return maxResults.
*/
public function getMaxResults()
{
return $this->maxResults;
}
/**
* Set maxResults.
*
* @param maxResults the value to set.
* @return Criteria
*/
public function setMaxResults($maxResults)
{
$this->maxResults = $maxResults;
return $this;
}
}

View file

@ -0,0 +1,195 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
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 <kontakt@beberlei.de>
* @since 2.3
*/
class ClosureExpressionVisitor extends ExpressionVisitor
{
/**
* Access the field of a given object. This field has to be public directly
* or indirectly (through an accessor get* or a magic method, __get, __call).
*
* is*() is not supported.
*
* @return mixed
*/
static public function getObjectFieldValue($object, $field)
{
$accessor = "get" . $field;
if (method_exists($object, $accessor) || 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
*/
static public 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:
case Comparison::IS:
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);
};
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());
}
}
private function andExpressions($expressions)
{
return function ($object) use ($expressions) {
foreach ($expressions as $expression) {
if ( ! $expression($object)) {
return false;
}
}
return true;
};
}
private function orExpressions($expressions)
{
return function ($object) use ($expressions) {
foreach ($expressions as $expression) {
if ($expression($object)) {
return true;
}
}
return false;
};
}
}

View file

@ -0,0 +1,75 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Collections\Expr;
/**
* Comparison of a field with a value by the given operator.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @since 2.3
*/
class Comparison implements Expression
{
const EQ = '=';
const NEQ = '<>';
const LT = '<';
const LTE = '<=';
const GT = '>';
const GTE = '>=';
const IS = 'IS';
const IN = 'IN';
const NIN = 'NIN';
private $field;
private $op;
private $value;
public function __construct($field, $operator, $value)
{
if ( ! ($value instanceof Value)) {
$value = new Value($value);
}
$this->field = $field;
$this->op = $operator;
$this->value = $value;
}
public function getField()
{
return $this->field;
}
public function getValue()
{
return $this->value;
}
public function getOperator()
{
return $this->op;
}
public function visit(ExpressionVisitor $visitor)
{
return $visitor->walkComparison($this);
}
}

View file

@ -0,0 +1,72 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Collections\Expr;
/**
* Expression of Expressions combined by AND or OR operation.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @since 2.3
*/
class CompositeExpression implements Expression
{
const TYPE_AND = 'AND';
const TYPE_OR = 'OR';
private $type;
private $expressions = array();
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;
}
}
/**
* Return the list of expressions nested in this composite.
*
* @return Expression[]
*/
public function getExpressionList()
{
return $this->expressions;
}
public function getType()
{
return $this->type;
}
public function visit(ExpressionVisitor $visitor)
{
return $visitor->walkCompositeExpression($this);
}
}

View file

@ -0,0 +1,31 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Collections\Expr;
/**
* Expression for the {@link Selectable} interface.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
interface Expression
{
public function visit(ExpressionVisitor $visitor);
}

View file

@ -0,0 +1,81 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
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 <kontakt@beberlei.de>
*/
abstract class ExpressionVisitor
{
/**
* Convert a comparison expression into the target query language output
*
* @param Comparison $comparison
*
* @return mixed
*/
abstract public function walkComparison(Comparison $comparison);
/**
* Convert a value expression into the target query language part.
*
* @param Value $value
*
* @return mixed
*/
abstract public function walkValue(Value $value);
/**
* Convert a composite expression into the target query language output
*
* @param CompositeExpression $expr
*
* @return mixed
*/
abstract public function walkCompositeExpression(CompositeExpression $expr);
/**
* Dispatch walking an expression to the appropriate handler.
*
* @param Expression
*
* @return mixed
*/
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));
}
}
}

View file

@ -0,0 +1,41 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\Common\Collections\Expr;
class Value implements Expression
{
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
public function visit(ExpressionVisitor $visitor)
{
return $visitor->walkValue($this);
}
}

View file

@ -0,0 +1,149 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYvalue HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYvalue
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
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.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @since 2.3
*/
class ExpressionBuilder
{
/**
* @return CompositeExpression
*/
public function andX($x = null)
{
return new CompositeExpression(CompositeExpression::TYPE_AND, func_get_args());
}
/**
* @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
* @param mixed $value
*
* @return Comparison
*/
public function isNull($field)
{
return new Comparison($field, Comparison::IS, new Value(null));
}
/**
* @param string $field
* @param mixed $value
*
* @return Comparison
*/
public function in($field, array $values)
{
return new Comparison($field, Comparison::IN, new Value($values));
}
/**
* @param string $field
* @param mixed $value
*
* @return Comparison
*/
public function notIn($field, array $values)
{
return new Comparison($field, Comparison::NIN, new Value($values));
}
}

View file

@ -0,0 +1,48 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
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 <kontakt@beberlei.de>
* @since 2.3
*/
interface Selectable
{
/**
* Select all elements from a selectable that match the expression and
* return a new collection containing these elements.
*
* @param Criteria $criteria
* @return Collection
*/
function matching(Criteria $criteria);
}