Total Complexity | 6 |
Total Lines | 37 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
14 | #[Attribute(Attribute::TARGET_PROPERTY)] |
||
15 | class DateTimeCompareWith extends AbstractRule |
||
16 | { |
||
17 | const MESSAGE = 'The date/time should be %s `%s`'; |
||
18 | |||
19 | use Comparator; |
||
20 | |||
21 | public function __construct( |
||
22 | private string $operator, |
||
23 | private string $other, |
||
24 | ?string $message = null, |
||
25 | ?int $stopOnFailure = null, |
||
26 | ?int $priority = null |
||
27 | ) { |
||
28 | parent::__construct($message, $stopOnFailure, $priority); |
||
29 | } |
||
30 | |||
31 | public function isValid($input, array $context = []): bool |
||
32 | { |
||
33 | try { |
||
34 | $other = fn() => new DateTimeImmutable($context[$this->other]); |
||
35 | $datetime = |
||
36 | $input instanceof DateTimeInterface ? |
||
37 | $input : |
||
38 | new DateTimeImmutable($input); |
||
39 | |||
40 | return array_key_exists($this->other, $context) |
||
41 | && $this->compare($datetime, $this->operator, $other); |
||
42 | } catch (Throwable) { |
||
43 | return false; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | public function translatedMessage(): ?string |
||
51 | } |
||
52 | } |
||
53 |