feat: add Date::earliestOf and Date::latestOf helper functions#960
Conversation
|
Wdyt about making this a bit more semantic with a rename to |
| /** | ||
| * @return self The earliest date from the provided dates. | ||
| */ | ||
| public static function min(self $date, self ...$otherDates): self |
There was a problem hiding this comment.
| public static function min(self $date, self ...$otherDates): self | |
| public static function min(self $initialCutOffDate, self ...$otherDates): self |
So it is clear that the intention here is to use the date in a particular way.
|
Martin Georgiev (@martin-georgiev) I like the Not sure if the I was wondering if these methods should allow empty list of dates or not and ruled it out because it would make the return value nullable which is often a pain in the ass to handle on the user side. |
Date::max and Date::min helper functionsDate::earliestOf and Date::latestOf helper functions
| /** | ||
| * @return self The earliest date from the provided dates. | ||
| */ | ||
| public static function earliestOf(self ...$datesToCompare): self |
There was a problem hiding this comment.
| public static function earliestOf(self ...$datesToCompare): self | |
| public static function earliestOf(self $date, self ...$datesToCompare): self |
nit: this will require at least one argument in the fn invocation.
There was a problem hiding this comment.
Addressed, had it like this before, but changed it thinking I can enforce it with PHPStan, but that seemed impossible.
|
Nice, it reads more intuitive now and code feels self-documented. Upvote from me ;) |
I often find it necessary to compare two or more
Dateinstances in order to establish which one of them is the earliest or the latest.My proposal here is to add two static methods
Date::maxandDate::minthat mimic PHP's nativemaxandminfunction.There's also an alternative naming to consider
latestandearliest, but since we already use nativemaxandminwithDateTimeI picked these ones.I also considered adding these as non-static methods that would be used like
but I think it feels a little wrong and calling it this way
seems better.
Update
Methods were renamed to
Date::earliestOfandDate::latestOf.