Passed
Push — main ( fcaac4...013e60 )
by Breno
01:49
created

IsToday::isValid()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 4
nop 2
dl 0
loc 9
rs 10
c 1
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\Rules\AbstractValidation;
8
use DateTimeImmutable;
9
use Throwable;
10
11
#[Attribute(Attribute::TARGET_PROPERTY)]
12
class IsToday extends AbstractValidation
13
{
14
    CONST TODAY_FORMAT = 'Y-m-d';
15
16
    public function __construct(?string $message = 'The date/time should be today')
17
    {
18
        parent::__construct($message);
19
    }
20
21
    protected function isValid($input, array $context = []): bool
22
    {
23
        try {
24
            $now = new DateTimeImmutable();
25
            $datetime = new DateTimeImmutable($input);
26
27
            return $datetime->format(self::TODAY_FORMAT) === $now->format(self::TODAY_FORMAT);
28
        } catch (Throwable) {
29
            return false;
30
        }
31
    }
32
}
33