IsDeliveryDateRule::execute()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 4
nc 6
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Sample\Validation;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DateTime;
22
use DateTimeInterface;
23
use Limoncello\Validation\Contracts\Execution\ContextInterface;
24
use Limoncello\Validation\Rules\ExecuteRule;
25
26
/**
27
 * @package Sample
28
 */
29
class IsDeliveryDateRule extends ExecuteRule
30
{
31
    /** @var string Message Template */
32
    const MESSAGE_TEMPLATE = 'The value should be a valid delivery date.';
33
34
    /**
35
     * @param mixed            $value
36
     * @param ContextInterface $context
37
     *
38
     * @return array
39
     */
40
    public static function execute($value, ContextInterface $context): array
41
    {
42
        $from = new DateTime('tomorrow');
43
        $to   = new DateTime('+5 days');
44
45
        $isValidDeliveryDate = $value instanceof DateTimeInterface === true && $value >= $from && $value <= $to;
46
47
        return $isValidDeliveryDate === true ?
48
            static::createSuccessReply($value) :
49
            static::createErrorReply(
50
                $context,
51
                $value,
52
                Errors::IS_DELIVERY_DATE,
53
                static::MESSAGE_TEMPLATE,
54
                [$from->getTimestamp(), $to->getTimestamp()]
55
            );
56
    }
57
}
58