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

Comparisons::regexp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Limoncello\Validation\Validator;
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\Rules\RuleInterface;
21
use Limoncello\Validation\Rules\Comparisons\DateTimeBetween;
22
use Limoncello\Validation\Rules\Comparisons\DateTimeEquals;
23
use Limoncello\Validation\Rules\Comparisons\DateTimeLessOrEquals;
24
use Limoncello\Validation\Rules\Comparisons\DateTimeLessThan;
25
use Limoncello\Validation\Rules\Comparisons\DateTimeMoreOrEquals;
26
use Limoncello\Validation\Rules\Comparisons\DateTimeMoreThan;
27
use Limoncello\Validation\Rules\Comparisons\DateTimeNotEquals;
28
use Limoncello\Validation\Rules\Comparisons\NumericBetween;
29
use Limoncello\Validation\Rules\Comparisons\NumericLessOrEquals;
30
use Limoncello\Validation\Rules\Comparisons\NumericLessThan;
31
use Limoncello\Validation\Rules\Comparisons\NumericMoreOrEqualsThan;
32
use Limoncello\Validation\Rules\Comparisons\NumericMoreThan;
33
use Limoncello\Validation\Rules\Comparisons\ScalarEquals;
34
use Limoncello\Validation\Rules\Comparisons\ScalarInValues;
35
use Limoncello\Validation\Rules\Comparisons\ScalarNotEquals;
36
use Limoncello\Validation\Rules\Comparisons\StringLengthBetween;
37
use Limoncello\Validation\Rules\Comparisons\StringLengthMax;
38
use Limoncello\Validation\Rules\Comparisons\StringLengthMin;
39
use Limoncello\Validation\Rules\Comparisons\StringRegExp;
40
use Limoncello\Validation\Rules\Generic\OrOperator;
41
42
/**
43
 * @package Limoncello\Validation
44
 */
45
trait Comparisons
46
{
47
    /**
48
     * @param mixed $value
49
     *
50
     * @return RuleInterface
51
     */
52
    protected static function equals($value): RuleInterface
53
    {
54
        return $value instanceof DateTimeInterface ? new DateTimeEquals($value) : new ScalarEquals($value);
55
    }
56
57
    /**
58
     * @param mixed $value
59
     *
60
     * @return RuleInterface
61
     */
62
    protected static function notEquals($value): RuleInterface
63
    {
64
        return $value instanceof DateTimeInterface ? new DateTimeNotEquals($value) : new ScalarNotEquals($value);
65
    }
66
67
    /**
68
     * @param array $scalars
69
     *
70
     * @return RuleInterface
71
     */
72
    protected static function inValues(array $scalars): RuleInterface
73
    {
74
        return new ScalarInValues($scalars);
75
    }
76
77
    /**
78
     * @param mixed $value
79
     *
80
     * @return RuleInterface
81
     */
82
    protected static function lessThan($value): RuleInterface
83
    {
84
        return $value instanceof DateTimeInterface ? new DateTimeLessThan($value) : new NumericLessThan($value);
85
    }
86
87
    /**
88
     * @param mixed $value
89
     *
90
     * @return RuleInterface
91
     */
92
    protected static function lessOrEquals($value): RuleInterface
93
    {
94
        return $value instanceof DateTimeInterface ? new DateTimeLessOrEquals($value) : new NumericLessOrEquals($value);
95
    }
96
97
    /**
98
     * @param mixed $value
99
     *
100
     * @return RuleInterface
101
     */
102
    protected static function moreThan($value): RuleInterface
103
    {
104
        return $value instanceof DateTimeInterface ? new DateTimeMoreThan($value) : new NumericMoreThan($value);
105
    }
106
107
    /**
108
     * @param mixed $value
109
     *
110
     * @return RuleInterface
111
     */
112
    protected static function moreOrEquals($value): RuleInterface
113
    {
114
        return $value instanceof DateTimeInterface ?
115
            new DateTimeMoreOrEquals($value) : new NumericMoreOrEqualsThan($value);
116
    }
117
118
    /**
119
     * @param mixed $lowerLimit
120
     * @param mixed $upperLimit
121
     *
122
     * @return RuleInterface
123
     */
124
    protected static function between($lowerLimit, $upperLimit): RuleInterface
125
    {
126
        return ($lowerLimit instanceof DateTimeInterface && $upperLimit instanceof DateTimeInterface) ?
127
            new DateTimeBetween($lowerLimit, $upperLimit) : new NumericBetween($lowerLimit, $upperLimit);
128
    }
129
130
    /**
131
     * @param int $min
132
     * @param int $max
133
     *
134
     * @return RuleInterface
135
     */
136
    protected static function stringLengthBetween(int $min, int $max): RuleInterface
137
    {
138
        return new StringLengthBetween($min, $max);
139
    }
140
141
    /**
142
     * @param int $min
143
     *
144
     * @return RuleInterface
145
     */
146
    protected static function stringLengthMin(int $min): RuleInterface
147
    {
148
        return new StringLengthMin($min);
149
    }
150
151
    /**
152
     * @param int $max
153
     *
154
     * @return RuleInterface
155
     */
156
    protected static function stringLengthMax(int $max): RuleInterface
157
    {
158
        return new StringLengthMax($max);
159
    }
160
161
    /**
162
     * @param string $pattern
163
     *
164
     * @return RuleInterface
165
     */
166
    protected static function regexp(string $pattern): RuleInterface
167
    {
168
        return new StringRegExp($pattern);
169
    }
170
171
    /**
172
     * @param RuleInterface $rule
173
     *
174
     * @return RuleInterface
175
     */
176
    protected static function nullable(RuleInterface $rule): RuleInterface
177
    {
178
        return new OrOperator(static::equals(null), $rule);
179
    }
180
}
181