Passed
Push — main ( 013e60...00f274 )
by Breno
01:56
created

IsToday   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 19
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A evaluate() 0 9 2
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 evaluate($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