| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Forms; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\Utils\Strings; |
| 12 |
use Packetery\Nette\Utils\Validators; |
| 13 |
/** |
| 14 |
* Common validators. |
| 15 |
*/ |
| 16 |
class Validator |
| 17 |
{ |
| 18 |
use \Packetery\Nette\StaticClass; |
| 19 |
/** @var array */ |
| 20 |
public static $messages = [Controls\CsrfProtection::PROTECTION => 'Your session has expired. Please return to the home page and try again.', Form::EQUAL => 'Please enter %s.', Form::NOT_EQUAL => 'This value should not be %s.', Form::FILLED => 'This field is required.', Form::BLANK => 'This field should be blank.', Form::MIN_LENGTH => 'Please enter at least %d characters.', Form::MAX_LENGTH => 'Please enter no more than %d characters.', Form::LENGTH => 'Please enter a value between %d and %d characters long.', Form::EMAIL => 'Please enter a valid email address.', Form::URL => 'Please enter a valid URL.', Form::INTEGER => 'Please enter a valid integer.', Form::FLOAT => 'Please enter a valid number.', Form::MIN => 'Please enter a value greater than or equal to %d.', Form::MAX => 'Please enter a value less than or equal to %d.', Form::RANGE => 'Please enter a value between %d and %d.', Form::MAX_FILE_SIZE => 'The size of the uploaded file can be up to %d bytes.', Form::MAX_POST_SIZE => 'The uploaded data exceeds the limit of %d bytes.', Form::MIME_TYPE => 'The uploaded file is not in the expected format.', Form::IMAGE => 'The uploaded file must be image in format JPEG, GIF, PNG or WebP.', Controls\SelectBox::VALID => 'Please select a valid option.', Controls\UploadControl::VALID => 'An error occurred during file upload.']; |
| 21 |
/** |
| 22 |
* @return string|\Packetery\Nette\HtmlStringable |
| 23 |
* @internal |
| 24 |
*/ |
| 25 |
public static function formatMessage(Rule $rule, bool $withValue = \true) |
| 26 |
{ |
| 27 |
$message = $rule->message; |
| 28 |
if ($message instanceof \Packetery\Nette\HtmlStringable) { |
| 29 |
return $message; |
| 30 |
} elseif ($message === null && \is_string($rule->validator) && isset(static::$messages[$rule->validator])) { |
| 31 |
$message = static::$messages[$rule->validator]; |
| 32 |
} elseif ($message == null) { |
| 33 |
// intentionally == |
| 34 |
\trigger_error("Missing validation message for control '{$rule->control->getName()}'" . (\is_string($rule->validator) ? " (validator '{$rule->validator}')." : '.'), \E_USER_WARNING); |
| 35 |
} |
| 36 |
if ($translator = $rule->control->getForm()->getTranslator()) { |
| 37 |
$message = $translator->translate($message, \is_int($rule->arg) ? $rule->arg : null); |
| 38 |
} |
| 39 |
$message = \preg_replace_callback('#%(name|label|value|\\d+\\$[ds]|[ds])#', function (array $m) use($rule, $withValue, $translator) { |
| 40 |
static $i = -1; |
| 41 |
switch ($m[1]) { |
| 42 |
case 'name': |
| 43 |
return $rule->control->getName(); |
| 44 |
case 'label': |
| 45 |
if ($rule->control instanceof Controls\BaseControl) { |
| 46 |
$caption = $rule->control->getCaption(); |
| 47 |
$caption = $caption instanceof \Packetery\Nette\HtmlStringable ? $caption->getText() : ($translator ? $translator->translate($caption) : $caption); |
| 48 |
return \rtrim((string) $caption, ':'); |
| 49 |
} |
| 50 |
return ''; |
| 51 |
case 'value': |
| 52 |
return $withValue ? $rule->control->getValue() : $m[0]; |
| 53 |
default: |
| 54 |
$args = \is_array($rule->arg) ? $rule->arg : [$rule->arg]; |
| 55 |
$i = (int) $m[1] ? (int) $m[1] - 1 : $i + 1; |
| 56 |
return isset($args[$i]) ? $args[$i] instanceof Control ? $withValue ? $args[$i]->getValue() : "%{$i}" : $args[$i] : ''; |
| 57 |
} |
| 58 |
}, $message); |
| 59 |
return $message; |
| 60 |
} |
| 61 |
/********************* default validators ****************d*g**/ |
| 62 |
/** |
| 63 |
* Is control's value equal with second parameter? |
| 64 |
*/ |
| 65 |
public static function validateEqual(Control $control, $arg) : bool |
| 66 |
{ |
| 67 |
$value = $control->getValue(); |
| 68 |
$values = \is_array($value) ? $value : [$value]; |
| 69 |
$args = \is_array($arg) ? $arg : [$arg]; |
| 70 |
foreach ($values as $val) { |
| 71 |
foreach ($args as $item) { |
| 72 |
if ((string) $val === (string) $item) { |
| 73 |
continue 2; |
| 74 |
} |
| 75 |
} |
| 76 |
return \false; |
| 77 |
} |
| 78 |
return (bool) $values; |
| 79 |
} |
| 80 |
/** |
| 81 |
* Is control's value not equal with second parameter? |
| 82 |
*/ |
| 83 |
public static function validateNotEqual(Control $control, $arg) : bool |
| 84 |
{ |
| 85 |
return !static::validateEqual($control, $arg); |
| 86 |
} |
| 87 |
/** |
| 88 |
* Returns argument. |
| 89 |
*/ |
| 90 |
public static function validateStatic(Control $control, bool $arg) : bool |
| 91 |
{ |
| 92 |
return $arg; |
| 93 |
} |
| 94 |
/** |
| 95 |
* Is control filled? |
| 96 |
*/ |
| 97 |
public static function validateFilled(Controls\BaseControl $control) : bool |
| 98 |
{ |
| 99 |
return $control->isFilled(); |
| 100 |
} |
| 101 |
/** |
| 102 |
* Is control not filled? |
| 103 |
*/ |
| 104 |
public static function validateBlank(Controls\BaseControl $control) : bool |
| 105 |
{ |
| 106 |
return !$control->isFilled(); |
| 107 |
} |
| 108 |
/** |
| 109 |
* Is control valid? |
| 110 |
*/ |
| 111 |
public static function validateValid(Controls\BaseControl $control) : bool |
| 112 |
{ |
| 113 |
return $control->getRules()->validate(); |
| 114 |
} |
| 115 |
/** |
| 116 |
* Is a control's value number in specified range? |
| 117 |
*/ |
| 118 |
public static function validateRange(Control $control, array $range) : bool |
| 119 |
{ |
| 120 |
$range = \array_map(function ($v) { |
| 121 |
return $v === '' ? null : $v; |
| 122 |
}, $range); |
| 123 |
return Validators::isInRange($control->getValue(), $range); |
| 124 |
} |
| 125 |
/** |
| 126 |
* Is a control's value number greater than or equal to the specified minimum? |
| 127 |
*/ |
| 128 |
public static function validateMin(Control $control, $minimum) : bool |
| 129 |
{ |
| 130 |
return Validators::isInRange($control->getValue(), [$minimum === '' ? null : $minimum, null]); |
| 131 |
} |
| 132 |
/** |
| 133 |
* Is a control's value number less than or equal to the specified maximum? |
| 134 |
*/ |
| 135 |
public static function validateMax(Control $control, $maximum) : bool |
| 136 |
{ |
| 137 |
return Validators::isInRange($control->getValue(), [null, $maximum === '' ? null : $maximum]); |
| 138 |
} |
| 139 |
/** |
| 140 |
* Count/length validator. Range is array, min and max length pair. |
| 141 |
* @param array|int $range |
| 142 |
*/ |
| 143 |
public static function validateLength(Control $control, $range) : bool |
| 144 |
{ |
| 145 |
if (!\is_array($range)) { |
| 146 |
$range = [$range, $range]; |
| 147 |
} |
| 148 |
$value = $control->getValue(); |
| 149 |
return Validators::isInRange(\is_array($value) ? \count($value) : Strings::length((string) $value), $range); |
| 150 |
} |
| 151 |
/** |
| 152 |
* Has control's value minimal count/length? |
| 153 |
*/ |
| 154 |
public static function validateMinLength(Control $control, $length) : bool |
| 155 |
{ |
| 156 |
return static::validateLength($control, [$length, null]); |
| 157 |
} |
| 158 |
/** |
| 159 |
* Is control's value count/length in limit? |
| 160 |
*/ |
| 161 |
public static function validateMaxLength(Control $control, $length) : bool |
| 162 |
{ |
| 163 |
return static::validateLength($control, [null, $length]); |
| 164 |
} |
| 165 |
/** |
| 166 |
* Has been button pressed? |
| 167 |
*/ |
| 168 |
public static function validateSubmitted(Controls\SubmitButton $control) : bool |
| 169 |
{ |
| 170 |
return $control->isSubmittedBy(); |
| 171 |
} |
| 172 |
/** |
| 173 |
* Is control's value valid email address? |
| 174 |
*/ |
| 175 |
public static function validateEmail(Control $control) : bool |
| 176 |
{ |
| 177 |
return Validators::isEmail((string) $control->getValue()); |
| 178 |
} |
| 179 |
/** |
| 180 |
* Is control's value valid URL? |
| 181 |
*/ |
| 182 |
public static function validateUrl(Control $control) : bool |
| 183 |
{ |
| 184 |
$value = (string) $control->getValue(); |
| 185 |
if (Validators::isUrl($value)) { |
| 186 |
return \true; |
| 187 |
} |
| 188 |
$value = "https://{$value}"; |
| 189 |
if (Validators::isUrl($value)) { |
| 190 |
$control->setValue($value); |
| 191 |
return \true; |
| 192 |
} |
| 193 |
return \false; |
| 194 |
} |
| 195 |
/** |
| 196 |
* Does the control's value match the regular expression? |
| 197 |
* Case-sensitive to comply with the HTML5 <input /> pattern attribute behaviour |
| 198 |
*/ |
| 199 |
public static function validatePattern(Control $control, string $pattern, bool $caseInsensitive = \false) : bool |
| 200 |
{ |
| 201 |
$regexp = "\x01^(?:{$pattern})\$\x01Du" . ($caseInsensitive ? 'i' : ''); |
| 202 |
foreach (static::toArray($control->getValue()) as $item) { |
| 203 |
$value = $item instanceof \Packetery\Nette\Http\FileUpload ? $item->getName() : $item; |
| 204 |
if (!Strings::match((string) $value, $regexp)) { |
| 205 |
return \false; |
| 206 |
} |
| 207 |
} |
| 208 |
return \true; |
| 209 |
} |
| 210 |
public static function validatePatternCaseInsensitive(Control $control, string $pattern) : bool |
| 211 |
{ |
| 212 |
return self::validatePattern($control, $pattern, \true); |
| 213 |
} |
| 214 |
/** |
| 215 |
* Is a control's value numeric? |
| 216 |
*/ |
| 217 |
public static function validateNumeric(Control $control) : bool |
| 218 |
{ |
| 219 |
$value = $control->getValue(); |
| 220 |
return \is_int($value) && $value >= 0 || \is_string($value) && Strings::match($value, '#^\\d+$#D'); |
| 221 |
} |
| 222 |
/** |
| 223 |
* Is a control's value decimal number? |
| 224 |
*/ |
| 225 |
public static function validateInteger(Control $control) : bool |
| 226 |
{ |
| 227 |
if (Validators::isNumericInt($value = $control->getValue()) && !\is_float($tmp = $value * 1)) { |
| 228 |
$control->setValue($tmp); |
| 229 |
return \true; |
| 230 |
} |
| 231 |
return \false; |
| 232 |
} |
| 233 |
/** |
| 234 |
* Is a control's value float number? |
| 235 |
*/ |
| 236 |
public static function validateFloat(Control $control) : bool |
| 237 |
{ |
| 238 |
$value = $control->getValue(); |
| 239 |
if (\is_string($value)) { |
| 240 |
$value = \str_replace([' ', ','], ['', '.'], $value); |
| 241 |
} |
| 242 |
if (Validators::isNumeric($value)) { |
| 243 |
$control->setValue((float) $value); |
| 244 |
return \true; |
| 245 |
} |
| 246 |
return \false; |
| 247 |
} |
| 248 |
/** |
| 249 |
* Is file size in limit? |
| 250 |
*/ |
| 251 |
public static function validateFileSize(Controls\UploadControl $control, $limit) : bool |
| 252 |
{ |
| 253 |
foreach (static::toArray($control->getValue()) as $file) { |
| 254 |
if ($file->getSize() > $limit || $file->getError() === \UPLOAD_ERR_INI_SIZE) { |
| 255 |
return \false; |
| 256 |
} |
| 257 |
} |
| 258 |
return \true; |
| 259 |
} |
| 260 |
/** |
| 261 |
* Has file specified mime type? |
| 262 |
* @param string|string[] $mimeType |
| 263 |
*/ |
| 264 |
public static function validateMimeType(Controls\UploadControl $control, $mimeType) : bool |
| 265 |
{ |
| 266 |
$mimeTypes = \is_array($mimeType) ? $mimeType : \explode(',', $mimeType); |
| 267 |
foreach (static::toArray($control->getValue()) as $file) { |
| 268 |
$type = \strtolower($file->getContentType()); |
| 269 |
if (!\in_array($type, $mimeTypes, \true) && !\in_array(\preg_replace('#/.*#', '/*', $type), $mimeTypes, \true)) { |
| 270 |
return \false; |
| 271 |
} |
| 272 |
} |
| 273 |
return \true; |
| 274 |
} |
| 275 |
/** |
| 276 |
* Is file image? |
| 277 |
*/ |
| 278 |
public static function validateImage(Controls\UploadControl $control) : bool |
| 279 |
{ |
| 280 |
foreach (static::toArray($control->getValue()) as $file) { |
| 281 |
if (!$file->isImage()) { |
| 282 |
return \false; |
| 283 |
} |
| 284 |
} |
| 285 |
return \true; |
| 286 |
} |
| 287 |
private static function toArray($value) : array |
| 288 |
{ |
| 289 |
return \is_object($value) ? [$value] : (array) $value; |
| 290 |
} |
| 291 |
} |
| 292 |
|