Update core 8.3.0
This commit is contained in:
parent
da7a7918f8
commit
cd7a898e66
6144 changed files with 132297 additions and 87747 deletions
|
@ -26,6 +26,11 @@ use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor;
|
|||
/**
|
||||
* An ArrayCollection is a Collection implementation that wraps a regular PHP array.
|
||||
*
|
||||
* Warning: Using (un-)serialize() on a collection is not a supported use-case
|
||||
* and may break when we change the internals in the future. If you need to
|
||||
* serialize a collection use {@link toArray()} and reconstruct the collection
|
||||
* manually.
|
||||
*
|
||||
* @since 2.0
|
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
||||
* @author Jonathan Wage <jonwage@gmail.com>
|
||||
|
@ -50,6 +55,21 @@ class ArrayCollection implements Collection, Selectable
|
|||
$this->elements = $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance from the specified elements.
|
||||
*
|
||||
* This method is provided for derived classes to specify how a new
|
||||
* instance should be created when constructor semantics have changed.
|
||||
*
|
||||
* @param array $elements Elements.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
protected function createFrom(array $elements)
|
||||
{
|
||||
return new static($elements);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
@ -254,9 +274,9 @@ class ArrayCollection implements Collection, Selectable
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function add($value)
|
||||
public function add($element)
|
||||
{
|
||||
$this->elements[] = $value;
|
||||
$this->elements[] = $element;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -284,7 +304,7 @@ class ArrayCollection implements Collection, Selectable
|
|||
*/
|
||||
public function map(Closure $func)
|
||||
{
|
||||
return new static(array_map($func, $this->elements));
|
||||
return $this->createFrom(array_map($func, $this->elements));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -292,7 +312,7 @@ class ArrayCollection implements Collection, Selectable
|
|||
*/
|
||||
public function filter(Closure $p)
|
||||
{
|
||||
return new static(array_filter($this->elements, $p));
|
||||
return $this->createFrom(array_filter($this->elements, $p));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -324,7 +344,7 @@ class ArrayCollection implements Collection, Selectable
|
|||
}
|
||||
}
|
||||
|
||||
return array(new static($matches), new static($noMatches));
|
||||
return array($this->createFrom($matches), $this->createFrom($noMatches));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -368,8 +388,9 @@ class ArrayCollection implements Collection, Selectable
|
|||
}
|
||||
|
||||
if ($orderings = $criteria->getOrderings()) {
|
||||
$next = null;
|
||||
foreach (array_reverse($orderings) as $field => $ordering) {
|
||||
$next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1);
|
||||
$next = ClosureExpressionVisitor::sortByField($field, $ordering == Criteria::DESC ? -1 : 1, $next);
|
||||
}
|
||||
|
||||
uasort($filtered, $next);
|
||||
|
@ -382,6 +403,6 @@ class ArrayCollection implements Collection, Selectable
|
|||
$filtered = array_slice($filtered, (int)$offset, $length);
|
||||
}
|
||||
|
||||
return new static($filtered);
|
||||
return $this->createFrom($filtered);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,6 +69,24 @@ class ClosureExpressionVisitor extends ExpressionVisitor
|
|||
return $object[$field];
|
||||
}
|
||||
|
||||
if (isset($object->$field)) {
|
||||
return $object->$field;
|
||||
}
|
||||
|
||||
// camelcase field name to support different variable naming conventions
|
||||
$ccField = preg_replace_callback('/_(.?)/', function($matches) { return strtoupper($matches[1]); }, $field);
|
||||
|
||||
foreach ($accessors as $accessor) {
|
||||
$accessor .= $ccField;
|
||||
|
||||
|
||||
if ( ! method_exists($object, $accessor)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $object->$accessor();
|
||||
}
|
||||
|
||||
return $object->$field;
|
||||
}
|
||||
|
||||
|
@ -155,6 +173,26 @@ class ClosureExpressionVisitor extends ExpressionVisitor
|
|||
return false !== strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
|
||||
};
|
||||
|
||||
case Comparison::MEMBER_OF:
|
||||
return function ($object) use ($field, $value) {
|
||||
$fieldValues = ClosureExpressionVisitor::getObjectFieldValue($object, $field);
|
||||
if (!is_array($fieldValues)) {
|
||||
$fieldValues = iterator_to_array($fieldValues);
|
||||
}
|
||||
return in_array($value, $fieldValues);
|
||||
};
|
||||
|
||||
case Comparison::STARTS_WITH:
|
||||
return function ($object) use ($field, $value) {
|
||||
return 0 === strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
|
||||
};
|
||||
|
||||
case Comparison::ENDS_WITH:
|
||||
return function ($object) use ($field, $value) {
|
||||
return $value === substr(ClosureExpressionVisitor::getObjectFieldValue($object, $field), -strlen($value));
|
||||
};
|
||||
|
||||
|
||||
default:
|
||||
throw new \RuntimeException("Unknown comparison operator: " . $comparison->getOperator());
|
||||
}
|
||||
|
|
|
@ -27,17 +27,19 @@ namespace Doctrine\Common\Collections\Expr;
|
|||
*/
|
||||
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';
|
||||
|
||||
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';
|
||||
const MEMBER_OF = 'MEMBER_OF';
|
||||
const STARTS_WITH = 'STARTS_WITH';
|
||||
const ENDS_WITH = 'ENDS_WITH';
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
|
|
@ -27,7 +27,7 @@ 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
|
||||
* values only for comparisons, otherwise the behavior of the comparison
|
||||
* may be different between implementations (Array vs ORM vs ODM).
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
|
@ -163,4 +163,38 @@ class ExpressionBuilder
|
|||
{
|
||||
return new Comparison($field, Comparison::CONTAINS, new Value($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Comparison
|
||||
*/
|
||||
public function memberOf ($field, $value)
|
||||
{
|
||||
return new Comparison($field, Comparison::MEMBER_OF, new Value($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Comparison
|
||||
*/
|
||||
public function startsWith($field, $value)
|
||||
{
|
||||
return new Comparison($field, Comparison::STARTS_WITH, new Value($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return Comparison
|
||||
*/
|
||||
public function endsWith($field, $value)
|
||||
{
|
||||
return new Comparison($field, Comparison::ENDS_WITH, new Value($value));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue