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