Completed
Push — master ( 22fd50...798002 )
by Neomerx
03:53
created

DateTimeBetween   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A compare() 0 10 3
1
<?php namespace Limoncello\Validation\Rules\Comparisons;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use DateTimeInterface;
20
use Limoncello\Validation\Contracts\Errors\ContextKeys;
21
use Limoncello\Validation\Contracts\Errors\ErrorCodes;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
24
/**
25
 * @package Limoncello\Validation
26
 */
27
final class DateTimeBetween extends BaseTwoValueComparision
28
{
29
    /**
30
     * @param DateTimeInterface $lowerValue
31
     * @param DateTimeInterface $upperValue
32
     */
33
    public function __construct(DateTimeInterface $lowerValue, DateTimeInterface $upperValue)
34
    {
35
        assert($lowerValue <= $upperValue);
36
37
        parent::__construct(
38
            $lowerValue->getTimestamp(),
39
            $upperValue->getTimestamp(),
40
            ErrorCodes::DATE_TIME_BETWEEN,
41
            [
42
                ContextKeys::DATE_TIME_MIN => $lowerValue->getTimestamp(),
43
                ContextKeys::DATE_TIME_MAX => $upperValue->getTimestamp(),
44
            ]
45
        );
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public static function compare($value, ContextInterface $context): bool
52
    {
53
        assert($value instanceof DateTimeInterface);
54
        $result =
55
            $value instanceof DateTimeInterface &&
56
            static::readLowerValue($context) <= ($timestamp = $value->getTimestamp()) &&
57
            $timestamp <= static::readUpperValue($context);
58
59
        return $result;
60
    }
61
}
62