TimestampValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 28
ccs 0
cts 14
cp 0
crap 20
rs 9.7
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/validize project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Validize\Validator;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\ValueObject\Timestamp;
13
14
final class TimestampValidator extends Validator
15
{
16
    /** @param mixed $input */
17
    protected function validate($input): Timestamp
18
    {
19
        $settings = $this->getSettings();
20
        $before = $settings['before'] ?? false;
21
        $after = $settings['after'] ?? false;
22
23
        if (!$input instanceof Timestamp) {
24
            Assertion::notEmpty($input, 'Must not be empty.');
25
            $timestamp = Timestamp::fromNative($input);
26
        } else {
27
            $timestamp = $input;
28
        }
29
30
        if ($before !== false) {
31
            Assertion::true(
32
                $timestamp->isBefore(Timestamp::fromNative($before)),
33
                'Timestamp must be before given time.'
34
            );
35
        }
36
37
        if ($after !== false) {
38
            Assertion::true(
39
                $timestamp->isAfter(Timestamp::fromNative($after)),
40
                'Timestamp must be after given time.'
41
            );
42
        }
43
44
        return $timestamp;
45
    }
46
}
47