-
Notifications
You must be signed in to change notification settings - Fork 772
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
Rule/date time diff #1463
base: main
Are you sure you want to change the base?
Rule/date time diff #1463
Changes from 1 commit
9e631a8
b0d2e88
85d5b9e
205a0b7
4ebf053
906f722
caee0e4
5ea05c9
94d9d46
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 |
---|---|---|
|
@@ -20,7 +20,6 @@ | |
use Respect\Validation\Rules\Core\Standard; | ||
use Respect\Validation\Validatable; | ||
|
||
use function in_array; | ||
use function is_scalar; | ||
|
||
#[Template( | ||
|
@@ -36,22 +35,20 @@ final class DateTimeDiff extends Standard | |
private readonly Validatable $rule; | ||
|
||
/** | ||
* @param string $type "years"|"months"|"days"|"hours"|"minutes"|"seconds"|"microseconds" | ||
* @param string|null $format Example: "Y-m-d H:i:s.u" | ||
* @param string $type DateInterval format examples: (y, m, d, days, h, i, s, f) | ||
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. I do understand the appeal of using DateInterval formats directly. However, I think having more descriptive values is better as it's more understandable. With the literal constant, there's no reason to translate or document anything. |
||
* @param DateTimeImmutable|null $now The value that will be compared to the input | ||
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. I'm a against adding documentation to the code like that, I think the name of the parameter should be pretty obvious for what it does, otherwise we should do that for everything. I understand that it can be useful to add some documentation here and there, but for this specific method, I would rather not have it. |
||
*/ | ||
public function __construct( | ||
Validatable $rule, | ||
private readonly string $type = 'years', | ||
private readonly string $type = 'y', | ||
private readonly ?string $format = null, | ||
private readonly ?DateTimeImmutable $now = null, | ||
) { | ||
$availableTypes = ['years', 'months', 'days', 'hours', 'minutes', 'seconds', 'microseconds']; | ||
if (!in_array($this->type, $availableTypes, true)) { | ||
if (!$this->isDateIntervalType($this->type)) { | ||
throw new InvalidRuleConstructorException( | ||
'"%s" is not a valid type of age (Available: %s)', | ||
$this->type, | ||
$availableTypes | ||
['y', 'm', 'd', 'days', 'h', 'i', 's', 'f'] | ||
); | ||
} | ||
$this->rule = $this->extractSiblingSuitableRule( | ||
|
@@ -82,22 +79,17 @@ public function evaluate(mixed $input): Result | |
->evaluate($this->comparisonValue($now, $compareTo)) | ||
->withNameIfMissing($input instanceof DateTimeInterface ? $input->format('c') : $input); | ||
|
||
$parameters = ['type' => $this->type, 'now' => $this->nowParameter($now)]; | ||
$parameters = [ | ||
'type' => $this->getTranslatedType($this->type), | ||
'now' => $this->nowParameter($now) | ||
]; | ||
|
||
return (new Result($nextSibling->isValid, $input, $this, $parameters))->withNextSibling($nextSibling); | ||
} | ||
|
||
private function comparisonValue(DateTimeInterface $now, DateTimeInterface $compareTo) | ||
{ | ||
return match ($this->type) { | ||
'years' => $compareTo->diff($now)->y, | ||
'months' => $compareTo->diff($now)->m, | ||
'days' => $compareTo->diff($now)->days, | ||
'hours' => $compareTo->diff($now)->h, | ||
'minutes' => $compareTo->diff($now)->i, | ||
'seconds' => $compareTo->diff($now)->s, | ||
'microseconds' => $compareTo->diff($now)->f, | ||
}; | ||
return $compareTo->diff($now)->{$this->type}; | ||
} | ||
|
||
private function nowParameter(DateTimeInterface $now): string | ||
|
@@ -135,4 +127,18 @@ private function createDateTimeObject(mixed $input): ?DateTimeInterface | |
|
||
return $dateTime; | ||
} | ||
|
||
private function getTranslatedType(string $type): string | ||
{ | ||
return match ($type) { | ||
'y' => 'years', | ||
'm' => 'months', | ||
'd' => 'days', | ||
'days' => 'full days', | ||
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. Very nice catch! |
||
'h' => 'hours', | ||
'i' => 'minutes', | ||
's' => 'seconds', | ||
'f' => 'microseconds', | ||
}; | ||
} | ||
} |
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.
I know this seems weird, but i wanted to use those DateInterval vars. what do you guys think about it? How do we can improve this validation?