Completed
Push — master ( 2df407...de4379 )
by Neomerx
05:05
created

IsDeliveryDateRule::execute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 6
nop 2
1
<?php namespace Sample\Validation;
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 DateTime;
20
use DateTimeInterface;
21
use Limoncello\Validation\Contracts\Execution\ContextInterface;
22
use Limoncello\Validation\Rules\ExecuteRule;
23
24
/**
25
 * @package Sample
26
 */
27
class IsDeliveryDateRule extends ExecuteRule
28
{
29
    /**
30
     * @param mixed            $value
31
     * @param ContextInterface $context
32
     *
33
     * @return array
34
     */
35
    public static function execute($value, ContextInterface $context): array
36
    {
37
        $isValidDeliveryDate =
38
            $value instanceof DateTimeInterface === true &&
39
            $value >= new DateTime('tomorrow') &&
40
            $value <= new DateTime('+5 days');
41
42
        return $isValidDeliveryDate === true ?
43
            static::createSuccessReply($value) :
44
            static::createErrorReply($context, $value, Errors::IS_DELIVERY_DATE);
45
    }
46
}
47