1
|
|
|
<?php namespace Limoncello\Validation; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2016 [email protected] (www.neomerx.com) |
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\ErrorAggregatorInterface; |
20
|
|
|
use Limoncello\Validation\Contracts\RuleInterface; |
21
|
|
|
use Limoncello\Validation\Contracts\ValidatorInterface; |
22
|
|
|
use Limoncello\Validation\Errors\ErrorAggregator; |
23
|
|
|
use Limoncello\Validation\Validator\Captures as CapturesX; |
24
|
|
|
use Limoncello\Validation\Validator\Compares; |
25
|
|
|
use Limoncello\Validation\Validator\ExpressionsX; |
26
|
|
|
use Limoncello\Validation\Validator\Generics; |
27
|
|
|
use Limoncello\Validation\Validator\Types; |
28
|
|
|
use Limoncello\Validation\Validator\ValidatorTrait; |
29
|
|
|
use Limoncello\Validation\Validator\Values; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Validation |
33
|
|
|
*/ |
34
|
|
|
class Validator implements ValidatorInterface |
35
|
|
|
{ |
36
|
|
|
use ExpressionsX { |
37
|
|
|
andX as public; |
38
|
|
|
orX as public; |
39
|
|
|
ifX as public; |
40
|
|
|
arrayX as public; |
41
|
|
|
eachX as public; |
42
|
|
|
objectX as public; |
43
|
|
|
callableX as public; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
use Generics { |
47
|
|
|
success as public; |
48
|
|
|
fail as public; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
use Types { |
52
|
|
|
isString as public; |
53
|
|
|
isBool as public; |
54
|
|
|
isInt as public; |
55
|
|
|
isFloat as public; |
56
|
|
|
isNumeric as public; |
57
|
|
|
isDate as public; |
58
|
|
|
isArray as public; |
59
|
|
|
inValues as public; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
use Values { |
63
|
|
|
required as public; |
64
|
|
|
isNull as public; |
65
|
|
|
notNull as public; |
66
|
|
|
regExp as public; |
67
|
|
|
between as public; |
68
|
|
|
stringLength as public; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
use Compares { |
72
|
|
|
equals as public; |
73
|
|
|
notEquals as public; |
74
|
|
|
lessThan as public; |
75
|
|
|
lessOrEquals as public; |
76
|
|
|
moreThan as public; |
77
|
|
|
moreOrEquals as public; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
use CapturesX { |
81
|
|
|
singleCapture as public; |
82
|
|
|
multiCapture as public; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
use ValidatorTrait; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var RuleInterface |
89
|
|
|
*/ |
90
|
|
|
private $rule; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param RuleInterface $rule |
94
|
|
|
*/ |
95
|
33 |
|
public function __construct(RuleInterface $rule) |
96
|
|
|
{ |
97
|
33 |
|
$this->rule = $rule; |
98
|
33 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
33 |
|
public function validate($input) |
104
|
|
|
{ |
105
|
33 |
|
foreach (static::validateData($this->rule, $input, $this->createErrorAggregator()) as $error) { |
106
|
30 |
|
yield $error; |
107
|
|
|
} |
108
|
33 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param RuleInterface $rule |
112
|
|
|
* |
113
|
|
|
* @return static |
114
|
|
|
*/ |
115
|
33 |
|
public static function validator(RuleInterface $rule) |
116
|
|
|
{ |
117
|
33 |
|
return new static ($rule); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return ErrorAggregatorInterface |
|
|
|
|
122
|
|
|
*/ |
123
|
33 |
|
protected function createErrorAggregator() |
124
|
|
|
{ |
125
|
33 |
|
return new ErrorAggregator(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.