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 Limoncello\Validation\Contracts\Errors\ErrorCodes; |
20
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
21
|
|
|
use Limoncello\Validation\Rules\Generic\AndOperator; |
22
|
|
|
use Limoncello\Validation\Rules\Generic\Enum; |
23
|
|
|
use Limoncello\Validation\Rules\Generic\Fail; |
24
|
|
|
use Limoncello\Validation\Rules\Generic\Filter; |
25
|
|
|
use Limoncello\Validation\Rules\Generic\IfOperator; |
26
|
|
|
use Limoncello\Validation\Rules\Generic\OrOperator; |
27
|
|
|
use Limoncello\Validation\Rules\Generic\Required; |
28
|
|
|
use Limoncello\Validation\Rules\Generic\Success; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Limoncello\Validation |
32
|
|
|
*/ |
33
|
|
|
trait Generics |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @param RuleInterface $first |
37
|
|
|
* @param RuleInterface $second |
38
|
|
|
* |
39
|
|
|
* @return RuleInterface |
40
|
|
|
*/ |
41
|
1 |
|
protected static function andX(RuleInterface $first, RuleInterface $second): RuleInterface |
42
|
|
|
{ |
43
|
1 |
|
return new AndOperator($first, $second); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param RuleInterface $primary |
48
|
|
|
* @param RuleInterface $secondary |
49
|
|
|
* |
50
|
|
|
* @return RuleInterface |
51
|
|
|
*/ |
52
|
1 |
|
protected static function orX(RuleInterface $primary, RuleInterface $secondary): RuleInterface |
53
|
|
|
{ |
54
|
1 |
|
return new OrOperator($primary, $secondary); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param callable $condition |
59
|
|
|
* @param RuleInterface $onTrue |
60
|
|
|
* @param RuleInterface $onFalse |
61
|
|
|
* @param array $settings |
62
|
|
|
* |
63
|
|
|
* @return RuleInterface |
64
|
|
|
*/ |
65
|
2 |
|
protected static function ifX( |
66
|
|
|
callable $condition, |
67
|
|
|
RuleInterface $onTrue, |
68
|
|
|
RuleInterface $onFalse, |
69
|
|
|
array $settings = [] |
70
|
|
|
): RuleInterface { |
71
|
2 |
|
return new IfOperator($condition, $onTrue, $onFalse, $settings); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return RuleInterface |
76
|
|
|
*/ |
77
|
10 |
|
protected static function success(): RuleInterface |
78
|
|
|
{ |
79
|
10 |
|
return new Success(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int $errorCode |
84
|
|
|
* @param null|mixed $errorContext |
85
|
|
|
* |
86
|
|
|
* @return RuleInterface |
87
|
|
|
*/ |
88
|
3 |
|
protected static function fail(int $errorCode = ErrorCodes::INVALID_VALUE, $errorContext = null): RuleInterface |
89
|
|
|
{ |
90
|
3 |
|
return new Fail($errorCode, $errorContext); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $values |
95
|
|
|
* @param RuleInterface|null $next |
96
|
|
|
* |
97
|
|
|
* @return RuleInterface |
98
|
|
|
*/ |
99
|
1 |
|
protected static function enum(array $values, RuleInterface $next = null): RuleInterface |
100
|
|
|
{ |
101
|
1 |
|
return $next === null ? new Enum($values) : new AndOperator(static::enum($values), $next); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int $filterId |
106
|
|
|
* @param mixed $options |
107
|
|
|
* @param RuleInterface|null $next |
108
|
|
|
* |
109
|
|
|
* @return RuleInterface |
110
|
|
|
*/ |
111
|
1 |
|
protected static function filter(int $filterId, $options = null, RuleInterface $next = null): RuleInterface |
112
|
|
|
{ |
113
|
1 |
|
return $next === null ? |
114
|
1 |
|
new Filter($filterId, $options) : new AndOperator(static::filter($filterId, $options), $next); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param RuleInterface $rule |
|
|
|
|
119
|
|
|
* |
120
|
|
|
* @return RuleInterface |
121
|
|
|
*/ |
122
|
1 |
|
protected static function required(RuleInterface $rule = null): RuleInterface |
123
|
|
|
{ |
124
|
1 |
|
return $rule === null ? new Required(static::success()) : new Required($rule); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.