-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Order enum #389
Introduce Order enum #389
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,15 @@ | |
*/ | ||
class Criteria | ||
{ | ||
final public const ASC = 'ASC'; | ||
/** @deprecated use Order::Ascending instead */ | ||
final public const ASC = 'ASC'; | ||
|
||
/** @deprecated use Order::Descending instead */ | ||
final public const DESC = 'DESC'; | ||
|
||
private static ExpressionBuilder|null $expressionBuilder = null; | ||
|
||
/** @var array<string, string> */ | ||
/** @var array<string, Order> */ | ||
private array $orderings = []; | ||
|
||
private int|null $firstResult = null; | ||
|
@@ -57,7 +60,7 @@ public static function expr() | |
/** | ||
* Construct a new Criteria. | ||
* | ||
* @param array<string, string>|null $orderings | ||
* @param array<string, string|Order>|null $orderings | ||
*/ | ||
public function __construct( | ||
private Expression|null $expression = null, | ||
|
@@ -151,29 +154,71 @@ public function getWhereExpression() | |
/** | ||
* Gets the current orderings of this Criteria. | ||
* | ||
* @deprecated use orderings() instead | ||
* | ||
* @return array<string, string> | ||
*/ | ||
public function getOrderings() | ||
{ | ||
Deprecation::trigger( | ||
'doctrine/collections', | ||
'https://github.com/doctrine/collections/pull/389', | ||
'Calling %s() is deprecated. Use %s::orderings() instead.', | ||
__METHOD__, | ||
self::class, | ||
); | ||
|
||
return array_map( | ||
static fn (Order $ordering): string => $ordering->value, | ||
$this->orderings, | ||
); | ||
} | ||
|
||
/** | ||
* Gets the current orderings of this Criteria. | ||
* | ||
* @return array<string, Order> | ||
*/ | ||
public function orderings(): array | ||
{ | ||
return $this->orderings; | ||
} | ||
|
||
/** | ||
* Sets the ordering of the result of this Criteria. | ||
* | ||
* Keys are field and values are the order, being either ASC or DESC. | ||
* Keys are field and values are the order, being a valid Order enum case. | ||
* | ||
* @see Criteria::ASC | ||
* @see Criteria::DESC | ||
Comment on lines
-166
to
-167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deprecate the constants? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* @see Order::Ascending | ||
* @see Order::Descending | ||
* | ||
* @param array<string, string> $orderings | ||
* @param array<string, string|Order> $orderings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The types of the constructor signature have to be adjusted as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
* | ||
* @return $this | ||
*/ | ||
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; | ||
} | ||
|
||
static $triggered = false; | ||
|
||
if (! $triggered) { | ||
Deprecation::trigger( | ||
'doctrine/collections', | ||
'https://github.com/doctrine/collections/pull/389', | ||
'Passing non-Order enum values to %s() is deprecated. Pass Order enum values instead.', | ||
__METHOD__, | ||
); | ||
} | ||
|
||
$triggered = true; | ||
|
||
return strtoupper($ordering) === Order::Ascending->value ? Order::Ascending : Order::Descending; | ||
}, | ||
$orderings, | ||
); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Common\Collections; | ||
|
||
enum Order: string | ||
{ | ||
case Ascending = 'ASC'; | ||
case Descending = 'DESC'; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, we would have
@return array<string, Order>
hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I should use
array<string, string|Order>
right now so that people start adapting their code, even if in practice we don't break BC and stay witharray<string, string>
? 🤔 either that or introducing a new method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should provide a way to opt into the future behavior of this method. We could do so by adding a boolean flag (meh) or by adding a new method and deprecating this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I'll try to find a new name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there is no setter, I went with just
orderings()