|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @see \Illuminate\Validation\Validator (5.3) |
|
10
|
|
|
*/ |
|
11
|
|
|
trait ValidationRules |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get the size of an attribute. |
|
15
|
|
|
* @param string $attribute |
|
16
|
|
|
* @param mixed $value |
|
17
|
|
|
* @return mixed |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract protected function getSize($attribute, $value); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Replace all placeholders. |
|
23
|
|
|
* @param string $message |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
2 |
|
protected function replace($message, array $parameters) |
|
27
|
|
|
{ |
|
28
|
2 |
|
if (!Str::contains('%s', $message)) { |
|
29
|
2 |
|
return $message; |
|
30
|
|
|
} |
|
31
|
|
|
return preg_replace_callback('/(%s)/', function () use (&$parameters) { |
|
32
|
|
|
foreach ($parameters as $key => $value) { |
|
33
|
|
|
return array_shift($parameters); |
|
34
|
|
|
} |
|
35
|
|
|
}, $message); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Validate that an attribute value was "accepted". |
|
40
|
|
|
* This validation rule implies the attribute is "required". |
|
41
|
|
|
* @param mixed $value |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function validateAccepted($value) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$acceptable = ['yes', 'on', '1', 1, true, 'true']; |
|
47
|
2 |
|
return $this->validateRequired($value) && in_array($value, $acceptable, true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Validate the size of an attribute is between a set of values. |
|
52
|
|
|
* @param string $attribute |
|
53
|
|
|
* @param mixed $value |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function validateBetween($value, $attribute, array $parameters) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$this->requireParameterCount(2, $parameters, 'between'); |
|
59
|
2 |
|
$size = $this->getSize($attribute, $value); |
|
60
|
2 |
|
return $size >= $parameters[0] && $size <= $parameters[1]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Validate that an attribute value is a valid e-mail address. |
|
65
|
|
|
* @param mixed $value |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function validateEmail($value) |
|
69
|
|
|
{ |
|
70
|
2 |
|
return false !== filter_var($value, FILTER_VALIDATE_EMAIL); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Validate the size of an attribute is less than a maximum value. |
|
75
|
|
|
* @param string $attribute |
|
76
|
|
|
* @param mixed $value |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function validateMax($value, $attribute, array $parameters) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->requireParameterCount(1, $parameters, 'max'); |
|
82
|
|
|
return $this->getSize($attribute, $value) <= $parameters[0]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Validate the size of an attribute is greater than a minimum value. |
|
87
|
|
|
* @param string $attribute |
|
88
|
|
|
* @param mixed $value |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function validateMin($value, $attribute, array $parameters) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->requireParameterCount(1, $parameters, 'min'); |
|
94
|
|
|
return $this->getSize($attribute, $value) >= $parameters[0]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Validate that an attribute is numeric. |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
public function validateNumber($value) |
|
103
|
|
|
{ |
|
104
|
|
|
return is_numeric($value); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Validate that an attribute passes a regular expression check. |
|
109
|
|
|
* @param string $attribute |
|
110
|
|
|
* @param mixed $value |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function validateRegex($value, $attribute, array $parameters) |
|
114
|
|
|
{ |
|
115
|
|
|
if (!is_string($value) && !is_numeric($value)) { |
|
116
|
|
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
$this->requireParameterCount(1, $parameters, 'regex'); |
|
119
|
|
|
return preg_match($parameters[0], $value) > 0; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Validate that a required attribute exists. |
|
124
|
|
|
* @param mixed $value |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function validateRequired($value) |
|
128
|
|
|
{ |
|
129
|
3 |
|
return is_null($value) |
|
130
|
3 |
|
|| (is_string($value) && in_array(trim($value), ['', '[]'])) |
|
131
|
3 |
|
|| (is_array($value) && empty($value)) |
|
132
|
2 |
|
? false |
|
133
|
3 |
|
: true; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Validate that a value is a valid(ish) telephone number. |
|
138
|
|
|
* @param mixed $value |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function validateTel($value) |
|
142
|
|
|
{ |
|
143
|
|
|
if (!is_string($value) && !is_numeric($value)) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
$digits = (int) preg_match_all('/[0-9]/', $value); |
|
147
|
|
|
$hasValidLength = 4 <= $digits && 15 >= $digits; |
|
148
|
|
|
return $hasValidLength && preg_match('/^([+]?[\d\s\-\(\)]*)$/', $value) > 0; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Validate that a value is a valid URL. |
|
153
|
|
|
* @param mixed $value |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
|
|
public function validateUrl($value) |
|
157
|
|
|
{ |
|
158
|
|
|
if (!is_string($value)) { |
|
159
|
|
|
return false; |
|
160
|
|
|
} |
|
161
|
|
|
// This pattern is derived from Symfony\Component\Validator\Constraints\UrlValidator (5.0.7). |
|
162
|
|
|
// (c) Fabien Potencier <[email protected]> http://symfony.com |
|
163
|
|
|
$pattern = '~^ |
|
164
|
|
|
(https?):// # protocol |
|
165
|
|
|
( |
|
166
|
|
|
([\pL\pN\pS\-\_\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name |
|
167
|
|
|
| # or |
|
168
|
|
|
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IPv4 address |
|
169
|
|
|
) |
|
170
|
|
|
(:[0-9]+)? # a port (optional) |
|
171
|
|
|
(?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )* # a path |
|
172
|
|
|
(?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a query (optional) |
|
173
|
|
|
(?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a fragment (optional) |
|
174
|
|
|
$~ixu'; |
|
175
|
|
|
return preg_match($pattern, $value) > 0; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Require a certain number of parameters to be present. |
|
180
|
|
|
* @param int $count |
|
181
|
|
|
* @param string $rule |
|
182
|
|
|
* @return void |
|
183
|
|
|
* @throws InvalidArgumentException |
|
184
|
|
|
*/ |
|
185
|
2 |
|
protected function requireParameterCount($count, array $parameters, $rule) |
|
186
|
|
|
{ |
|
187
|
2 |
|
if (count($parameters) < $count) { |
|
188
|
|
|
throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); |
|
189
|
|
|
} |
|
190
|
2 |
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|