Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
24 changes: 6 additions & 18 deletions src/CriteriaEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BadMethodCallException;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Order;
use Doctrine\Common\Collections\Expr\{
Comparison,
CompositeExpression,
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -242,8 +243,7 @@ private function getValueOfProperty(object $entity, string $property)

/**
* @param Entity[] $results
* @param array<string, string> $orderBy (actually
* Criteria::ASC|Criteria:::DESC but the PHPStan annotations won't work)
* @param array<string, Order> $orderBy
* @return Entity[]
*/
private function sortResults(array $results, array $orderBy): array
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions src/InMemoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading