1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Validator; |
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 Limoncello\Validation\Contracts\Errors\ErrorCodes; |
22
|
|
|
use Limoncello\Validation\Contracts\Rules\RuleInterface; |
23
|
|
|
use Limoncello\Validation\I18n\Messages; |
24
|
|
|
use Limoncello\Validation\Rules\Generic\AndOperator; |
25
|
|
|
use Limoncello\Validation\Rules\Generic\Enum; |
26
|
|
|
use Limoncello\Validation\Rules\Generic\Fail; |
27
|
|
|
use Limoncello\Validation\Rules\Generic\Filter; |
28
|
|
|
use Limoncello\Validation\Rules\Generic\IfOperator; |
29
|
|
|
use Limoncello\Validation\Rules\Generic\OrOperator; |
30
|
|
|
use Limoncello\Validation\Rules\Generic\Required; |
31
|
|
|
use Limoncello\Validation\Rules\Generic\Success; |
32
|
|
|
use Limoncello\Validation\Rules\Generic\Value; |
33
|
|
|
use function assert; |
34
|
|
|
use function is_resource; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @package Limoncello\Validation |
38
|
|
|
*/ |
39
|
|
|
trait Generics |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @param RuleInterface $first |
43
|
|
|
* @param RuleInterface $second |
44
|
|
|
* |
45
|
|
|
* @return RuleInterface |
46
|
|
|
*/ |
47
|
1 |
|
protected static function andX(RuleInterface $first, RuleInterface $second): RuleInterface |
48
|
|
|
{ |
49
|
1 |
|
return new AndOperator($first, $second); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param RuleInterface $primary |
54
|
|
|
* @param RuleInterface $secondary |
55
|
|
|
* |
56
|
|
|
* @return RuleInterface |
57
|
|
|
*/ |
58
|
1 |
|
protected static function orX(RuleInterface $primary, RuleInterface $secondary): RuleInterface |
59
|
|
|
{ |
60
|
1 |
|
return new OrOperator($primary, $secondary); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param callable $condition |
65
|
|
|
* @param RuleInterface $onTrue |
66
|
|
|
* @param RuleInterface $onFalse |
67
|
|
|
* @param array $settings |
68
|
|
|
* |
69
|
|
|
* @return RuleInterface |
70
|
|
|
*/ |
71
|
3 |
|
protected static function ifX( |
72
|
|
|
callable $condition, |
73
|
|
|
RuleInterface $onTrue, |
74
|
|
|
RuleInterface $onFalse, |
75
|
|
|
array $settings = [] |
76
|
|
|
): RuleInterface { |
77
|
3 |
|
return new IfOperator($condition, $onTrue, $onFalse, $settings); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return RuleInterface |
82
|
|
|
*/ |
83
|
10 |
|
protected static function success(): RuleInterface |
84
|
|
|
{ |
85
|
10 |
|
return new Success(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $errorCode |
90
|
|
|
* @param string $messageTemplate |
91
|
|
|
* @param array $messageParams |
92
|
|
|
* |
93
|
|
|
* @return RuleInterface |
94
|
|
|
*/ |
95
|
3 |
|
protected static function fail( |
96
|
|
|
int $errorCode = ErrorCodes::INVALID_VALUE, |
97
|
|
|
string $messageTemplate = Messages::INVALID_VALUE, |
98
|
|
|
array $messageParams = [] |
99
|
|
|
): RuleInterface { |
100
|
3 |
|
return new Fail($errorCode, $messageTemplate, $messageParams); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param mixed $value |
105
|
|
|
* |
106
|
|
|
* @return RuleInterface |
107
|
|
|
*/ |
108
|
1 |
|
protected static function value($value): RuleInterface |
109
|
|
|
{ |
110
|
|
|
// check the value is not a resource and can be represented as string |
111
|
1 |
|
assert(is_resource($value) === false); |
112
|
|
|
|
113
|
1 |
|
return new Value($value); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $values |
118
|
|
|
* @param RuleInterface|null $next |
119
|
|
|
* |
120
|
|
|
* @return RuleInterface |
121
|
|
|
*/ |
122
|
1 |
|
protected static function enum(array $values, RuleInterface $next = null): RuleInterface |
123
|
|
|
{ |
124
|
1 |
|
return $next === null ? new Enum($values) : new AndOperator(static::enum($values), $next); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $filterId |
129
|
|
|
* @param mixed $options |
130
|
|
|
* @param int $errorCode |
131
|
|
|
* @param string $messageTemplate |
132
|
|
|
* @param RuleInterface|null $next |
133
|
|
|
* |
134
|
|
|
* @return RuleInterface |
135
|
|
|
*/ |
136
|
1 |
|
protected static function filter( |
137
|
|
|
int $filterId, |
138
|
|
|
$options = null, |
139
|
|
|
int $errorCode = ErrorCodes::INVALID_VALUE, |
140
|
|
|
string $messageTemplate = Messages::INVALID_VALUE, |
141
|
|
|
RuleInterface $next = null |
142
|
|
|
): RuleInterface { |
143
|
1 |
|
$filterRule = new Filter($filterId, $options, $errorCode, $messageTemplate); |
144
|
|
|
|
145
|
1 |
|
return $next === null ? $filterRule : new AndOperator($filterRule, $next); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param RuleInterface $rule |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @return RuleInterface |
152
|
|
|
*/ |
153
|
1 |
|
protected static function required(RuleInterface $rule = null): RuleInterface |
154
|
|
|
{ |
155
|
1 |
|
return $rule === null ? new Required(static::success()) : new Required($rule); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
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.