diff --git a/src/Criteria.php b/src/Criteria.php index eb49cbc6..782f1013 100644 --- a/src/Criteria.php +++ b/src/Criteria.php @@ -8,7 +8,9 @@ use Doctrine\Common\Collections\Expr\Expression; use Doctrine\Deprecations\Deprecation; +use function array_filter; use function array_map; +use function array_merge; use function func_num_args; use function strtoupper; @@ -54,6 +56,20 @@ public static function expr() return self::$expressionBuilder; } + /** + * Merges two Criteria together. + * + * @return self + */ + public static function merge(Criteria $leftCriteria, Criteria $rightCriteria) + { + return self::create() + ->andWhere(self::expr()->andX(...array_filter([$leftCriteria->getWhereExpression(), $rightCriteria->getWhereExpression()]))) + ->orderBy(array_merge($leftCriteria->getOrderings(), $rightCriteria->getOrderings())) + ->setFirstResult($rightCriteria->getFirstResult() ?? $leftCriteria->getFirstResult()) + ->setMaxResults($rightCriteria->getMaxResults() ?? $leftCriteria->getFirstResult()); + } + /** * Construct a new Criteria. *