| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentValidator; |
| 4 |
|
| 5 |
use Countable; |
| 6 |
use InvalidArgumentException; |
| 7 |
use FluentValidator\Contracts\File; |
| 8 |
|
| 9 |
trait ValidatesAttributes |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Require a certain number of parameters to be present. |
| 13 |
* |
| 14 |
* @param int $count |
| 15 |
* @param array $parameters |
| 16 |
* @param string $rule |
| 17 |
* |
| 18 |
* @return void |
| 19 |
* |
| 20 |
* @throws \InvalidArgumentException |
| 21 |
*/ |
| 22 |
protected function requireParameterCount($count, $parameters, $rule) |
| 23 |
{ |
| 24 |
if (count($parameters) < $count) { |
| 25 |
throw new InvalidArgumentException( |
| 26 |
"Validation rule $rule requires at least $count parameters." |
| 27 |
); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get the size of an attribute. |
| 33 |
* |
| 34 |
* @param string $attribute |
| 35 |
* @param mixed $value |
| 36 |
* |
| 37 |
* @return mixed |
| 38 |
*/ |
| 39 |
protected function getSize($attribute, $value) |
| 40 |
{ |
| 41 |
// This method will determine if the attribute is a number, string, or file and |
| 42 |
// return the proper size accordingly. If it is a number, then number itself |
| 43 |
// is the size. If it is a file, we take kilobytes, and for a string the |
| 44 |
// entire length of the string will be considered the attribute size. |
| 45 |
$type = $this->deduceType($value); |
| 46 |
|
| 47 |
switch ($type) { |
| 48 |
case 'numeric': |
| 49 |
return $value; |
| 50 |
case 'array': |
| 51 |
return count($value); |
| 52 |
case 'file': |
| 53 |
return $value->getSize() / 1024; |
| 54 |
default: |
| 55 |
return mb_strlen($value); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Deduce the value type of an attribute. |
| 61 |
* |
| 62 |
* @param $value |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
protected function deduceType($value) |
| 67 |
{ |
| 68 |
if (is_numeric($value)) { |
| 69 |
return 'numeric'; |
| 70 |
} elseif (is_array($value)) { |
| 71 |
return 'array'; |
| 72 |
} elseif ($value instanceof File) { |
| 73 |
return 'file'; |
| 74 |
} |
| 75 |
|
| 76 |
return 'string'; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Convert the given values to boolean if they are string "true" / "false". |
| 81 |
* |
| 82 |
* @param array $values |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
protected function convertValuesToBoolean($values) |
| 87 |
{ |
| 88 |
return array_map(function ($value) { |
| 89 |
if ($value === 'true') { |
| 90 |
return true; |
| 91 |
} elseif ($value === 'false') { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
return $value; |
| 96 |
}, $values); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Validate that a required attribute exists. |
| 101 |
* |
| 102 |
* @param string $attribute |
| 103 |
* @param mixed $value |
| 104 |
* |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
protected function validateRequired($attribute, $value) |
| 108 |
{ |
| 109 |
if (is_null($value)) { |
| 110 |
return false; |
| 111 |
} elseif (is_string($value) && trim($value) === '') { |
| 112 |
return false; |
| 113 |
} elseif ((is_array($value) || $value instanceof Countable) && count($value) < 1) { |
| 114 |
return false; |
| 115 |
} elseif ($value instanceof File) { |
| 116 |
return (string) $value->getPath() != ''; |
| 117 |
} |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Validate that an attribute exists when another attribute has a given value. |
| 124 |
* |
| 125 |
* @param string $attribute |
| 126 |
* @param mixed $value |
| 127 |
* @param mixed $parameters |
| 128 |
* |
| 129 |
* @return bool |
| 130 |
*/ |
| 131 |
protected function validateRequiredIf($attribute, $value, $parameters) |
| 132 |
{ |
| 133 |
$this->requireParameterCount(2, $parameters, 'required_if'); |
| 134 |
|
| 135 |
$other = Arr::get($this->data, $parameters[0]); |
| 136 |
|
| 137 |
$values = array_slice($parameters, 1); |
| 138 |
|
| 139 |
if (is_bool($other)) { |
| 140 |
$values = $this->convertValuesToBoolean($values); |
| 141 |
} |
| 142 |
|
| 143 |
if (in_array($other, $values)) { |
| 144 |
return $this->validateRequired($attribute, $value); |
| 145 |
} |
| 146 |
|
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Validate that an attribute is a valid e-mail address. |
| 152 |
* |
| 153 |
* @param string $attribute |
| 154 |
* @param mixed $value |
| 155 |
* |
| 156 |
* @return bool |
| 157 |
*/ |
| 158 |
protected function validateEmail($attribute, $value) |
| 159 |
{ |
| 160 |
return ! ! is_email($value); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Validate the size of an attribute. |
| 165 |
* |
| 166 |
* @param string $attribute |
| 167 |
* @param mixed $value |
| 168 |
* @param array $parameters |
| 169 |
* |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
protected function validateSize($attribute, $value, $parameters) |
| 173 |
{ |
| 174 |
$this->requireParameterCount(1, $parameters, 'size'); |
| 175 |
|
| 176 |
return $this->getSize($attribute, $value) == $parameters[0]; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Validate the size of an attribute is greater than a minimum value. |
| 181 |
* |
| 182 |
* @param string $attribute |
| 183 |
* @param mixed $value |
| 184 |
* @param array $parameters |
| 185 |
* |
| 186 |
* @return bool |
| 187 |
*/ |
| 188 |
protected function validateMin($attribute, $value, $parameters) |
| 189 |
{ |
| 190 |
$this->requireParameterCount(1, $parameters, 'min'); |
| 191 |
|
| 192 |
return $this->getSize($attribute, $value) >= $parameters[0]; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Validate the size of an attribute is less than a maximum value. |
| 197 |
* |
| 198 |
* @param string $attribute |
| 199 |
* @param mixed $value |
| 200 |
* @param array $parameters |
| 201 |
* |
| 202 |
* @return bool |
| 203 |
*/ |
| 204 |
protected function validateMax($attribute, $value, $parameters) |
| 205 |
{ |
| 206 |
$this->requireParameterCount(1, $parameters, 'max'); |
| 207 |
|
| 208 |
return $this->getSize($attribute, $value) <= $parameters[0]; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Validate that two attributes match. |
| 213 |
* |
| 214 |
* @param string $attribute |
| 215 |
* @param mixed $value |
| 216 |
* @param array $parameters |
| 217 |
* |
| 218 |
* @return bool |
| 219 |
*/ |
| 220 |
protected function validateSame($attribute, $value, $parameters) |
| 221 |
{ |
| 222 |
$this->requireParameterCount(1, $parameters, 'same'); |
| 223 |
|
| 224 |
$other = @$this->data[$parameters[0]]; |
| 225 |
|
| 226 |
return $value === $other; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Validate that an attribute is a valid URL. |
| 231 |
* |
| 232 |
* @param string $attribute |
| 233 |
* @param mixed $value |
| 234 |
* |
| 235 |
* @return bool |
| 236 |
*/ |
| 237 |
protected function validateUrl($attribute, $value) |
| 238 |
{ |
| 239 |
$result = (bool) filter_var($value, FILTER_VALIDATE_URL); |
| 240 |
return apply_filters('fluentform_url_validator', $result, $value); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Validate that an attribute is numeric. |
| 245 |
* |
| 246 |
* @param string $attribute |
| 247 |
* @param mixed $value |
| 248 |
* |
| 249 |
* @return bool |
| 250 |
*/ |
| 251 |
protected function validateNumeric($attribute, $value) |
| 252 |
{ |
| 253 |
return is_numeric($value); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Validate the guessed extension of a file upload is in a set of file extensions. |
| 258 |
* |
| 259 |
* @param string $attribute |
| 260 |
* @param mixed $value |
| 261 |
* @param array $parameters |
| 262 |
* |
| 263 |
* @return bool |
| 264 |
*/ |
| 265 |
protected function validateMimes($attribute, $value, $parameters) |
| 266 |
{ |
| 267 |
if (! $this->isValidFileInstance($value)) { |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
if ($this->shouldBlockPhpUpload($value, $parameters)) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* @var $value \FluentForm\Framework\Request\File |
| 277 |
*/ |
| 278 |
return $value->getPath() != '' && in_array($value->guessExtension(), $parameters); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Check that the given value is a valid file instance. |
| 283 |
* |
| 284 |
* @param mixed $value |
| 285 |
* |
| 286 |
* @return bool |
| 287 |
*/ |
| 288 |
public function isValidFileInstance($value) |
| 289 |
{ |
| 290 |
return $value instanceof File && $value->isValid(); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Check if PHP uploads are explicitly allowed. |
| 295 |
* |
| 296 |
* @param mixed $value |
| 297 |
* @param array $parameters |
| 298 |
* |
| 299 |
* @return bool |
| 300 |
*/ |
| 301 |
protected function shouldBlockPhpUpload($value, $parameters) |
| 302 |
{ |
| 303 |
if (in_array('php', $parameters)) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
return strtolower($value->getClientOriginalExtension()) === 'php'; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Validate that an attribute exists even if not filled. |
| 312 |
* |
| 313 |
* @param string $attribute |
| 314 |
* @param mixed $value |
| 315 |
* |
| 316 |
* @return bool |
| 317 |
*/ |
| 318 |
protected function validatePresent($attribute, $value) |
| 319 |
{ |
| 320 |
return Arr::has($this->data, $attribute); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Validate that an attribute has a given number of digits. |
| 325 |
* |
| 326 |
* @param string $attribute |
| 327 |
* @param mixed $value |
| 328 |
* @param array $parameters |
| 329 |
* |
| 330 |
* @return bool |
| 331 |
*/ |
| 332 |
public function validateDigits($attribute, $value, $parameters) |
| 333 |
{ |
| 334 |
$this->requireParameterCount(1, $parameters, 'digits'); |
| 335 |
|
| 336 |
return $this->validateNumeric($attribute, $value) |
| 337 |
&& strlen((string) $value) == $parameters[0]; |
| 338 |
} |
| 339 |
} |