diff --git a/docs/en/expressions.rst b/docs/en/expressions.rst index ef431bd3..08dfddf5 100644 --- a/docs/en/expressions.rst +++ b/docs/en/expressions.rst @@ -76,7 +76,7 @@ orderBy Sets the ordering of the result of this Criteria. .. code-block:: php - $criteria->orderBy(['name' => Criteria::ASC]); + $criteria->orderBy(['name' => Order::ASC]); setFirstResult -------------- diff --git a/src/ArrayCollection.php b/src/ArrayCollection.php index c5041310..d3597b57 100644 --- a/src/ArrayCollection.php +++ b/src/ArrayCollection.php @@ -471,7 +471,7 @@ public function matching(Criteria $criteria) if ($orderings) { $next = null; foreach (array_reverse($orderings) as $field => $ordering) { - $next = ClosureExpressionVisitor::sortByField($field, $ordering === Criteria::DESC ? -1 : 1, $next); + $next = ClosureExpressionVisitor::sortByField($field, $ordering === Order::DESC->value ? -1 : 1, $next); } uasort($filtered, $next); diff --git a/src/Criteria.php b/src/Criteria.php index eb49cbc6..1e863a3c 100644 --- a/src/Criteria.php +++ b/src/Criteria.php @@ -24,7 +24,7 @@ class Criteria private static ExpressionBuilder|null $expressionBuilder = null; - /** @var array */ + /** @var array */ private array $orderings = []; private int|null $firstResult = null; @@ -155,7 +155,10 @@ public function getWhereExpression() */ public function getOrderings() { - return $this->orderings; + return array_map( + static fn (Order $ordering): string => $ordering->value, + $this->orderings, + ); } /** @@ -163,8 +166,8 @@ public function getOrderings() * * Keys are field and values are the order, being either ASC or DESC. * - * @see Criteria::ASC - * @see Criteria::DESC + * @see Order::ASC + * @see Order::DESC * * @param array $orderings * @@ -173,7 +176,13 @@ public function getOrderings() public function orderBy(array $orderings) { $this->orderings = array_map( - static fn (string $ordering): string => strtoupper($ordering) === self::ASC ? self::ASC : self::DESC, + static function (string|Order $ordering): Order { + if ($ordering instanceof Order) { + return $ordering; + } + + return strtoupper($ordering) === Order::ASC->value ? Order::ASC : Order::DESC; + }, $orderings, ); diff --git a/src/Order.php b/src/Order.php new file mode 100644 index 00000000..576550d6 --- /dev/null +++ b/src/Order.php @@ -0,0 +1,11 @@ + $object1, ], $collection - ->matching(new Criteria(null, ['sortField' => Criteria::ASC])) + ->matching(new Criteria(null, ['sortField' => Order::ASC])) ->toArray(), ); } @@ -443,7 +444,7 @@ public function testMultiColumnSortAppliesAllSorts(): void self::assertSame( $expected, $collection - ->matching(new Criteria(null, ['foo' => Criteria::DESC, 'bar' => Criteria::DESC])) + ->matching(new Criteria(null, ['foo' => Order::DESC, 'bar' => Order::DESC])) ->toArray(), ); } diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index a3115cc6..d84eeb73 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -9,6 +9,7 @@ use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\Expr\Expression; use Doctrine\Common\Collections\Expr\Value; +use Doctrine\Common\Collections\Order; use RuntimeException; use stdClass; @@ -72,7 +73,7 @@ public function testMatchingOrdering(): void { $this->fillMatchingFixture(); - $col = $this->collection->matching(new Criteria(null, ['foo' => 'DESC'])); + $col = $this->collection->matching(new Criteria(null, ['foo' => Order::DESC])); self::assertInstanceOf(Collection::class, $col); self::assertNotSame($col, $this->collection); diff --git a/tests/CriteriaTest.php b/tests/CriteriaTest.php index be855cc6..58839366 100644 --- a/tests/CriteriaTest.php +++ b/tests/CriteriaTest.php @@ -8,6 +8,7 @@ use Doctrine\Common\Collections\Expr\Comparison; use Doctrine\Common\Collections\Expr\CompositeExpression; use Doctrine\Common\Collections\ExpressionBuilder; +use Doctrine\Common\Collections\Order; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use PHPUnit\Framework\TestCase; @@ -25,10 +26,10 @@ public function testCreate(): void public function testConstructor(): void { $expr = new Comparison('field', '=', 'value'); - $criteria = new Criteria($expr, ['foo' => 'ASC'], 10, 20); + $criteria = new Criteria($expr, ['foo' => Order::ASC], 10, 20); self::assertSame($expr, $criteria->getWhereExpression()); - self::assertSame(['foo' => 'ASC'], $criteria->getOrderings()); + self::assertSame(['foo' => Order::ASC->value], $criteria->getOrderings()); self::assertSame(10, $criteria->getFirstResult()); self::assertSame(20, $criteria->getMaxResults()); } @@ -38,7 +39,7 @@ public function testDeprecatedNullOffset(): void $expr = new Comparison('field', '=', 'value'); $this->expectDeprecationWithIdentifier('https://github.com/doctrine/collections/pull/311'); - $criteria = new Criteria($expr, ['foo' => 'ASC'], null, 20); + $criteria = new Criteria($expr, ['foo' => Order::ASC], null, 20); self::assertSame($expr, $criteria->getWhereExpression()); self::assertSame(['foo' => 'ASC'], $criteria->getOrderings()); @@ -122,9 +123,9 @@ public function testOrWhereWithoutWhere(): void public function testOrderings(): void { $criteria = Criteria::create() - ->orderBy(['foo' => 'ASC']); + ->orderBy(['foo' => Order::ASC]); - self::assertEquals(['foo' => 'ASC'], $criteria->getOrderings()); + self::assertEquals(['foo' => Order::ASC->value], $criteria->getOrderings()); } public function testExpr(): void