Passed
Push — main ( 11848c...4790f2 )
by Breno
02:08
created

CurrentDay::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\DateTime;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractRule;
8
use DateTimeImmutable;
9
use Throwable;
10
11
#[Attribute(Attribute::TARGET_PROPERTY)]
12
class CurrentDay extends AbstractRule
13
{
14
    const MESSAGE = 'The date/time should be today';
15
    const TODAY_FORMAT = 'Y-m-d';
16
17
    public function isValid($input, array $context = []): bool
18
    {
19
        try {
20
            $now = new DateTimeImmutable();
21
            $datetime = new DateTimeImmutable($input);
22
23
            return $datetime->format(self::TODAY_FORMAT) === $now->format(self::TODAY_FORMAT);
24
        } catch (Throwable) {
25
            return false;
26
        }
27
    }
28
}
29