diff --git a/composer.json b/composer.json index 0eb0fed..3bb07f8 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "php": "^8.2", - "doctrine/collections": "^1.6.8 || ^2.0", + "doctrine/collections": "^2.1", "doctrine/orm": "^3.0", "doctrine/persistence": "^3.0 || ^4.0" }, diff --git a/src/CriteriaEvaluator.php b/src/CriteriaEvaluator.php index ea7f92d..5fcaba4 100644 --- a/src/CriteriaEvaluator.php +++ b/src/CriteriaEvaluator.php @@ -6,6 +6,7 @@ use BadMethodCallException; use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Order; use Doctrine\Common\Collections\Expr\{ Comparison, CompositeExpression, @@ -70,7 +71,7 @@ public function evaluate(array $entities, Criteria $criteria): array $expr = $criteria->getWhereExpression(); $entities = $this->match($entities, $expr); - if ($orderings = $criteria->getOrderings()) { + if ($orderings = $criteria->orderings()) { $entities = $this->sortResults($entities, $orderings); } @@ -242,8 +243,7 @@ private function getValueOfProperty(object $entity, string $property) /** * @param Entity[] $results - * @param array $orderBy (actually - * Criteria::ASC|Criteria:::DESC but the PHPStan annotations won't work) + * @param array $orderBy * @return Entity[] */ private function sortResults(array $results, array $orderBy): array @@ -276,22 +276,10 @@ private function sortResults(array $results, array $orderBy): array // property in the sorting criteria continue; } - if ($direction === Criteria::ASC) { - if ($v1 > $v2) { - return 1; - } else { - return -1; - } - } elseif ($direction === Criteria::DESC) { - if ($v1 < $v2) { - return 1; - } else { - return -1; - } + if ($direction === Order::Ascending) { + return $v1 > $v2 ? 1 : -1; } else { - // @codeCoverageIgnoreStart - throw new DomainException(sprintf('Unhandled direction %s', $direction)); - // @codeCoverageIgnoreEnd + return $v1 < $v2 ? 1 : -1; } } // If all loops have exited without returning a comparision diff --git a/src/InMemoryRepository.php b/src/InMemoryRepository.php index c5b5478..a1b005f 100644 --- a/src/InMemoryRepository.php +++ b/src/InMemoryRepository.php @@ -185,11 +185,8 @@ public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = nu // Criteria::orderBy silently converts any invalid inputs to 'DESC' // This pre-validates them foreach ($orderBy as $field => $direction) { - if (is_string($direction)) { - $direction = strtoupper(trim($direction)); - if ($direction !== Criteria::ASC && $direction !== Criteria::DESC) { - throw InvalidOrientation::fromClassNameAndField($this->getClassName(), $field); - } + if (is_string($direction) && Order::tryFrom(strtoupper(trim($direction))) === null) { + throw InvalidOrientation::fromClassNameAndField($this->getClassName(), $field); } } $crit->orderBy($orderBy);