Rules   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 73
c 0
b 0
f 0
wmc 7
lcom 2
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A sku() 0 4 1
A amount() 0 6 1
A deliveryDate() 0 4 1
A email() 0 12 1
A address1() 0 4 1
A address2() 0 4 1
A areTermsAccepted() 0 4 1
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 Limoncello\Validation\Contracts\Rules\RuleInterface;
23
use function assert;
24
25
/**
26
 * @package Sample
27
 */
28
class Rules extends \Limoncello\Validation\Rules
29
{
30
    /** @var string Message Template */
31
    const MESSAGE_TEMPLATE_EMAIL = 'The value should be a valid email address.';
32
33
    /**
34
     * @return RuleInterface
35
     */
36
    public static function sku(): RuleInterface
37
    {
38
        return static::stringToInt(new IsSkuRule());
39
    }
40
41
    /**
42
     * @param int $max
43
     *
44
     * @return RuleInterface
45
     */
46
    public static function amount(int $max): RuleInterface
47
    {
48
        assert($max > 0);
49
50
        return static::stringToInt(static::between(1, $max));
51
    }
52
53
    /**
54
     * @return RuleInterface
55
     */
56
    public static function deliveryDate(): RuleInterface
57
    {
58
        return static::stringToDateTime(DateTime::ISO8601, new IsDeliveryDateRule());
59
    }
60
61
    /**
62
     * @return RuleInterface
63
     */
64
    public static function email(): RuleInterface
65
    {
66
        return static::isString(
67
            static::filter(
68
                FILTER_VALIDATE_EMAIL,
69
                null,
70
                Errors::IS_EMAIL,
71
                static::MESSAGE_TEMPLATE_EMAIL,
72
                static::stringLengthMax(255)
73
            )
74
        );
75
    }
76
77
    /**
78
     * @return RuleInterface
79
     */
80
    public static function address1(): RuleInterface
81
    {
82
        return static::isString(static::stringLengthBetween(1, 255));
83
    }
84
85
    /**
86
     * @return RuleInterface
87
     */
88
    public static function address2(): RuleInterface
89
    {
90
        return static::nullable(static::isString(static::stringLengthMax(255)));
91
    }
92
93
    /**
94
     * @return RuleInterface
95
     */
96
    public static function areTermsAccepted(): RuleInterface
97
    {
98
        return static::stringToBool(static::equals(true));
99
    }
100
}
101