1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Validator; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Much of the code in this class is derived from \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
|
|
|
/** |
23
|
|
|
* Replace all placeholders for the between rule. |
24
|
|
|
* @param string $message |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
protected function replaceBetween( $message, array $parameters ) |
28
|
|
|
{ |
29
|
|
|
return str_replace( [':min', ':max'], $parameters, $message ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Replace all placeholders for the max rule. |
34
|
|
|
* @param string $message |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function replaceMax( $message, array $parameters ) |
38
|
|
|
{ |
39
|
|
|
return str_replace( ':max', $parameters[0], $message ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Replace all placeholders for the min rule. |
44
|
|
|
* @param string $message |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
protected function replaceMin( $message, array $parameters ) |
48
|
|
|
{ |
49
|
|
|
return str_replace( ':min', $parameters[0], $message ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Validate that an attribute was "accepted". |
54
|
|
|
* This validation rule implies the attribute is "required". |
55
|
|
|
* @param string $attribute |
56
|
|
|
* @param mixed $value |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function validateAccepted( $attribute, $value ) |
60
|
|
|
{ |
61
|
|
|
$acceptable = ['yes', 'on', '1', 1, true, 'true']; |
62
|
|
|
return $this->validateRequired( $attribute, $value ) && in_array( $value, $acceptable, true ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Validate the size of an attribute is between a set of values. |
67
|
|
|
* @param string $attribute |
68
|
|
|
* @param mixed $value |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function validateBetween( $attribute, $value, array $parameters ) |
72
|
|
|
{ |
73
|
|
|
$this->requireParameterCount( 2, $parameters, 'between' ); |
74
|
|
|
$size = $this->getSize( $attribute, $value ); |
75
|
|
|
return $size >= $parameters[0] && $size <= $parameters[1]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Validate that an attribute is a valid e-mail address. |
80
|
|
|
* @param string $attribute |
81
|
|
|
* @param mixed $value |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function validateEmail( $attribute, $value ) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
return filter_var( $value, FILTER_VALIDATE_EMAIL ) !== false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Validate the size of an attribute is less than a maximum value. |
91
|
|
|
* @param string $attribute |
92
|
|
|
* @param mixed $value |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function validateMax( $attribute, $value, array $parameters ) |
96
|
|
|
{ |
97
|
|
|
$this->requireParameterCount( 1, $parameters, 'max' ); |
98
|
|
|
return $this->getSize( $attribute, $value ) <= $parameters[0]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Validate the size of an attribute is greater than a minimum value. |
103
|
|
|
* @param string $attribute |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function validateMin( $attribute, $value, array $parameters ) |
108
|
|
|
{ |
109
|
|
|
$this->requireParameterCount( 1, $parameters, 'min' ); |
110
|
|
|
return $this->getSize( $attribute, $value ) >= $parameters[0]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Validate that an attribute is numeric. |
115
|
|
|
* @param string $attribute |
116
|
|
|
* @param mixed $value |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function validateNumeric( $attribute, $value ) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return is_numeric( $value ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Validate that a required attribute exists. |
126
|
|
|
* @param string $attribute |
127
|
|
|
* @param mixed $value |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function validateRequired( $attribute, $value ) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
return is_null( $value ) |
133
|
|
|
|| ( is_string( $value ) && trim( $value ) === '' ) |
134
|
|
|
|| ( is_array( $value ) && count( $value ) < 1 ) |
135
|
|
|
? false |
136
|
|
|
: true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Require a certain number of parameters to be present. |
141
|
|
|
* @param int $count |
142
|
|
|
* @param string $rule |
143
|
|
|
* @return void |
144
|
|
|
* @throws InvalidArgumentException |
145
|
|
|
*/ |
146
|
|
|
protected function requireParameterCount( $count, array $parameters, $rule ) |
147
|
|
|
{ |
148
|
|
|
if( count( $parameters ) < $count ) { |
149
|
|
|
throw new InvalidArgumentException( "Validation rule $rule requires at least $count parameters." ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.