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

CurrentDay::isValid()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 4
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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