Skip to content

Commit b4242e2

Browse files
committed
Enhance DateRule to support custom date formats and improve validation logic; update tests accordingly
1 parent e08f32f commit b4242e2

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

src/Validation/Rules/DateRule.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace Nuxtifyts\PhpDto\Validation\Rules;
44

5+
use Nuxtifyts\PhpDto\Exceptions\ValidationRuleException;
6+
use Nuxtifyts\PhpDto\Support\Arr;
7+
58
class DateRule implements ValidationRule
69
{
10+
/**
11+
* @var list<string>
12+
*/
13+
protected array $formats = [];
14+
715
public string $name {
816
get {
917
return 'date';
@@ -12,19 +20,42 @@ class DateRule implements ValidationRule
1220

1321
public function evaluate(mixed $value): bool
1422
{
15-
return is_string($value) && strtotime($value) !== false;
23+
return empty($this->formats)
24+
? is_string($value) && strtotime($value) !== false
25+
: is_string($value) && array_any(
26+
$this->formats,
27+
static fn (string $format): bool => (bool) date_create_from_format($format, $value)
28+
);
1629
}
1730

1831
/**
19-
* @param ?array<string, mixed> $parameters
32+
* @param ?array<string, mixed> $parameters
33+
*
34+
* @throws ValidationRuleException
2035
*/
2136
public static function make(?array $parameters = null): self
2237
{
23-
return new self();
38+
$instance = new self();
39+
40+
$formats = Arr::getArray($parameters ?? [], 'formats', []);
41+
42+
if (array_any(
43+
$formats,
44+
static fn (mixed $format): bool => !is_string($format)
45+
)) {
46+
throw ValidationRuleException::invalidParameters();
47+
}
48+
49+
/** @var array<array-key, string> $formats */
50+
$instance->formats = array_values($formats);
51+
52+
return $instance;
2453
}
2554

2655
public function validationMessage(): string
2756
{
28-
return 'The :attribute must be a valid date.';
57+
return empty($this->formats)
58+
? 'The :attribute must be a valid date.'
59+
: 'The :attribute must be a valid date in one of the following formats: ' . implode(', ', $this->formats);
2960
}
3061
}

tests/Unit/Validation/DateRuleTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Nuxtifyts\PhpDto\Tests\Unit\Validation;
44

5-
use Nuxtifyts\PhpDto\Validation\Rules\DateRule;
6-
use PHPUnit\Framework\Attributes\CoversClass;
7-
use PHPUnit\Framework\Attributes\Test;
85
use Throwable;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use Nuxtifyts\PhpDto\Validation\Rules\DateRule;
9+
use Nuxtifyts\PhpDto\Exceptions\ValidationRuleException;
10+
use Nuxtifyts\PhpDto\Validation\Rules\ValidationRule;
911

1012
#[CoversClass(DateRule::class)]
13+
#[CoversClass(ValidationRuleException::class)]
1114
final class DateRuleTest extends ValidationRuleTestCase
1215
{
1316
/**
@@ -24,8 +27,8 @@ public function validate_validation_message(): void
2427
);
2528
}
2629

27-
/**
28-
* @return array<string, array{
30+
/**
31+
* @return array<string, array{
2932
* validationRuleClassString: class-string<ValidationRule>,
3033
* makeParams: ?array<string, mixed>,
3134
* expectedMakeException: ?class-string<ValidationRuleException>,
@@ -64,6 +67,27 @@ public static function data_provider(): array
6467
'valueToBeEvaluated' => '2021-01-01 00:00:00',
6568
'expectedResult' => true
6669
],
70+
'Will evaluate false when a custom datetime string is provided but no formats are set' => [
71+
'validationRuleClassString' => DateRule::class,
72+
'makeParams' => null,
73+
'expectedMakeException' => null,
74+
'valueToBeEvaluated' => '2021/01-01 00/00/00',
75+
'expectedResult' => false
76+
],
77+
'Will evaluate true when a custom datetime string is provided and a format is set' => [
78+
'validationRuleClassString' => DateRule::class,
79+
'makeParams' => ['formats' => ['Y/m-d H/m/s']],
80+
'expectedMakeException' => null,
81+
'valueToBeEvaluated' => '2021/01-01 00/00/00',
82+
'expectedResult' => true
83+
],
84+
'Will throw an exception when an invalid non string format is passed' => [
85+
'validationRuleClassString' => DateRule::class,
86+
'makeParams' => ['formats' => ['Y/m-d H/m/s', 123]],
87+
'expectedMakeException' => ValidationRuleException::class,
88+
'valueToBeEvaluated' => '2021/01-01 00/00/00',
89+
'expectedResult' => false
90+
],
6791
];
6892
}
6993
}

0 commit comments

Comments
 (0)