File tree Expand file tree Collapse file tree 6 files changed +25
-20
lines changed
Expand file tree Collapse file tree 6 files changed +25
-20
lines changed Original file line number Diff line number Diff line change @@ -228,9 +228,9 @@ User::query(new \Aternos\Model\Query\SelectQuery(
228228 ], \Aternos\Model\Query\WhereGroup:: OR)
229229 ]),
230230 [
231- new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\OrderField ::DESCENDING),
232- new \Aternos\Model\Query\OrderField("hello", \Aternos\Model\Query\OrderField ::ASCENDING),
233- new \Aternos\Model\Query\OrderField("foo", \Aternos\Model\Query\OrderField ::DESCENDING)
231+ new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\Direction ::DESCENDING),
232+ new \Aternos\Model\Query\OrderField("hello", \Aternos\Model\Query\Direction ::ASCENDING),
233+ new \Aternos\Model\Query\OrderField("foo", \Aternos\Model\Query\Direction ::DESCENDING)
234234 ],
235235 [
236236 new \Aternos\Model\Query\SelectField("field"),
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Aternos \Model \Query ;
4+
5+ enum Direction
6+ {
7+ case ASCENDING ;
8+ case DESCENDING ;
9+ }
Original file line number Diff line number Diff line change 33namespace Aternos \Model \Query \Generator ;
44
55use Aternos \Model \Query \{DeleteQuery ,
6+ Direction ,
67 OrderField ,
78 Query ,
89 SelectField ,
@@ -171,9 +172,8 @@ private function generateOrder(Query $query): string
171172 foreach ($ orderFields as $ orderField ) {
172173 /** @var OrderField $orderField */
173174 $ direction = match ($ orderField ->direction ) {
174- OrderField::ASCENDING => "ASC " ,
175- OrderField::DESCENDING => "DESC " ,
176- default => throw new UnexpectedValueException ("Invalid direction: " . $ orderField ->direction ),
175+ Direction::ASCENDING => "ASC " ,
176+ Direction::DESCENDING => "DESC " ,
177177 };
178178
179179 if ($ orderField ->raw ) {
Original file line number Diff line number Diff line change 99 */
1010class OrderField
1111{
12- /**
13- * Direction constants
14- */
15- const int ASCENDING = 0 ;
16- const int DESCENDING = 1 ;
1712
1813 /**
1914 * Field name to order by
@@ -30,17 +25,17 @@ class OrderField
3025 /**
3126 * Order direction
3227 *
33- * @var int
28+ * @var Direction
3429 */
35- public int $ direction = self ::ASCENDING ;
30+ public Direction $ direction = Direction ::ASCENDING ;
3631
3732 /**
3833 * OrderField constructor.
3934 *
4035 * @param string|null $field
41- * @param int $direction
36+ * @param Direction $direction
4237 */
43- public function __construct (?string $ field = null , int $ direction = self ::ASCENDING )
38+ public function __construct (?string $ field = null , Direction $ direction = Direction ::ASCENDING )
4439 {
4540 $ this ->field = $ field ;
4641 $ this ->direction = $ direction ;
@@ -57,10 +52,10 @@ public function setRaw(bool $raw): OrderField
5752 }
5853
5954 /**
60- * @param int $direction
55+ * @param Direction $direction
6156 * @return OrderField
6257 */
63- public function setDirection (int $ direction ): OrderField
58+ public function setDirection (Direction $ direction ): OrderField
6459 {
6560 $ this ->direction = $ direction ;
6661 return $ this ;
Original file line number Diff line number Diff line change @@ -111,8 +111,8 @@ public function orderBy(array $order): static
111111
112112 if (!is_int ($ value )) {
113113 $ value = match (strtoupper ($ value )) {
114- "ASCENDING " , "ASC " => OrderField ::ASCENDING ,
115- "DESCENDING " , "DESC " => OrderField ::DESCENDING ,
114+ "ASCENDING " , "ASC " => Direction ::ASCENDING ,
115+ "DESCENDING " , "DESC " => Direction ::DESCENDING ,
116116 default => throw new InvalidArgumentException ('Argument $order contains invalid order direction: ' . $ value ),
117117 };
118118 }
Original file line number Diff line number Diff line change 66use Aternos \Model \Driver \Test \TestDriver ;
77use Aternos \Model \Query \CountField ;
88use Aternos \Model \Query \DeleteQuery ;
9+ use Aternos \Model \Query \Direction ;
910use Aternos \Model \Query \MaxField ;
1011use Aternos \Model \Query \MinField ;
1112use Aternos \Model \Query \OrderField ;
@@ -337,7 +338,7 @@ public function testSelectLikeEscaping(): void
337338
338339 public function testSelectOrder (): void
339340 {
340- $ models = TestModel::select (order: ["number " => OrderField ::DESCENDING ]);
341+ $ models = TestModel::select (order: ["number " => Direction ::DESCENDING ]);
341342 $ this ->assertCount (10 , $ models );
342343 $ this ->assertEquals ("9J " , $ models [0 ]->id );
343344 $ this ->assertEquals ("J " , $ models [0 ]->text );
You can’t perform that action at this time.
0 commit comments