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

CurrentYear::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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 CurrentYear extends AbstractRule
13
{
14
    const MESSAGE = 'The date/time should be in the current year';
15
    const CURRENT_YEAR = 'Y';
16
17
    public function isValid($input, array $context = []): bool
18
    {
19
        try {
20
            $now = new DateTimeImmutable();
21
            $datetime = new DateTimeImmutable($input);
22
            $format = self::CURRENT_YEAR;
23
            return $datetime->format($format) === $now->format($format);
24
        } catch (Throwable) {
25
            return false;
26
        }
27
    }
28
}
29