Rules
6 days ago
Rule.php
6 days ago
RuleFactory.php
6 days ago
RuleParser.php
6 days ago
ValidationRule.php
6 days ago
Validator.php
6 days ago
Rule.php
447 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Rule class. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Validation |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Validation; |
| 11 | |
| 12 | use Kirki\Framework\Contracts\Support\Arrayable; |
| 13 | use Kirki\Framework\Supports\Arr; |
| 14 | use Kirki\Framework\Validation\Rules\ArrayRule; |
| 15 | use Kirki\Framework\Validation\Rules\BooleanRule; |
| 16 | use Kirki\Framework\Validation\Rules\DateRule; |
| 17 | use Kirki\Framework\Validation\Rules\ExistsRule; |
| 18 | use Kirki\Framework\Validation\Rules\FileRule; |
| 19 | use Kirki\Framework\Validation\Rules\InRule; |
| 20 | use Kirki\Framework\Validation\Rules\NotInRule; |
| 21 | use Kirki\Framework\Validation\Rules\NullableRule; |
| 22 | use Kirki\Framework\Validation\Rules\NumericRule; |
| 23 | use Kirki\Framework\Validation\Rules\ObjectRule; |
| 24 | use Kirki\Framework\Validation\Rules\ProhibitedIfRule; |
| 25 | use Kirki\Framework\Validation\Rules\ProhibitedRule; |
| 26 | use Kirki\Framework\Validation\Rules\ProhibitedUnlessRule; |
| 27 | use Kirki\Framework\Validation\Rules\RequiredIfRule; |
| 28 | use Kirki\Framework\Validation\Rules\RequiredRule; |
| 29 | use Kirki\Framework\Validation\Rules\RequiredUnlessRule; |
| 30 | use Kirki\Framework\Validation\Rules\SameAsRule; |
| 31 | use Kirki\Framework\Validation\Rules\SometimesRule; |
| 32 | use Kirki\Framework\Validation\Rules\StringRule; |
| 33 | use Kirki\Framework\Validation\Rules\UniqueRule; |
| 34 | use Kirki\Framework\Validation\Rules\ConditionalRule; |
| 35 | \defined('ABSPATH') || exit; |
| 36 | class Rule |
| 37 | { |
| 38 | /** |
| 39 | * Create a new string rule. |
| 40 | * |
| 41 | * @return StringRule |
| 42 | * |
| 43 | * @since 1.0.0 |
| 44 | */ |
| 45 | public static function string() |
| 46 | { |
| 47 | return new StringRule(); |
| 48 | } |
| 49 | /** |
| 50 | * Create a new email rule. |
| 51 | * |
| 52 | * @return StringRule |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | */ |
| 56 | public static function email() |
| 57 | { |
| 58 | return (new StringRule())->email(); |
| 59 | } |
| 60 | /** |
| 61 | * Create a new url rule. |
| 62 | * |
| 63 | * @return StringRule |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | public static function url() |
| 68 | { |
| 69 | return (new StringRule())->url(); |
| 70 | } |
| 71 | /** |
| 72 | * Create a new regex rule. |
| 73 | * |
| 74 | * @param string $pattern The regex pattern. |
| 75 | * |
| 76 | * @return StringRule |
| 77 | * |
| 78 | * @since 1.0.0 |
| 79 | */ |
| 80 | public static function regex(string $pattern) |
| 81 | { |
| 82 | return (new StringRule())->regex($pattern); |
| 83 | } |
| 84 | /** |
| 85 | * Create a new required rule. |
| 86 | * |
| 87 | * @return RequiredRule |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | public static function required() |
| 92 | { |
| 93 | return new RequiredRule(); |
| 94 | } |
| 95 | /** |
| 96 | * Create a new required if rule. |
| 97 | * |
| 98 | * @param string $other The other field to check. |
| 99 | * @param mixed $value The value to check. |
| 100 | * |
| 101 | * @return RequiredIfRule |
| 102 | * |
| 103 | * @since 1.0.0 |
| 104 | */ |
| 105 | public static function required_if($other, $value = null) |
| 106 | { |
| 107 | return new RequiredIfRule(static::process_conditional_arguments($other, $value)); |
| 108 | } |
| 109 | /** |
| 110 | * Create a new required unless rule. |
| 111 | * |
| 112 | * @param string $other The other field to check. |
| 113 | * @param mixed $value The value to check. |
| 114 | * |
| 115 | * @return RequiredUnlessRule |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | */ |
| 119 | public static function required_unless($other, $value = null) |
| 120 | { |
| 121 | return new RequiredUnlessRule(static::process_conditional_arguments($other, $value)); |
| 122 | } |
| 123 | /** |
| 124 | * Create a new array rule. |
| 125 | * |
| 126 | * @return ArrayRule |
| 127 | * |
| 128 | * @since 1.0.0 |
| 129 | */ |
| 130 | public static function array() |
| 131 | { |
| 132 | return new ArrayRule(); |
| 133 | } |
| 134 | /** |
| 135 | * Create a new numeric rule. |
| 136 | * |
| 137 | * @return NumericRule |
| 138 | * |
| 139 | * @since 1.0.0 |
| 140 | */ |
| 141 | public static function numeric() |
| 142 | { |
| 143 | return new NumericRule(); |
| 144 | } |
| 145 | /** |
| 146 | * Create a new number rule. |
| 147 | * |
| 148 | * @return NumericRule |
| 149 | * |
| 150 | * @since 1.0.0 |
| 151 | */ |
| 152 | public static function number() |
| 153 | { |
| 154 | return static::numeric(); |
| 155 | } |
| 156 | /** |
| 157 | * Create a new float rule. |
| 158 | * |
| 159 | * @return NumericRule |
| 160 | * |
| 161 | * @since 1.0.0 |
| 162 | */ |
| 163 | public static function float() |
| 164 | { |
| 165 | return static::numeric()->float(); |
| 166 | } |
| 167 | /** |
| 168 | * Create a new integer rule. |
| 169 | * |
| 170 | * @return NumericRule |
| 171 | * |
| 172 | * @since 1.0.0 |
| 173 | */ |
| 174 | public static function integer() |
| 175 | { |
| 176 | return (new NumericRule())->integer(); |
| 177 | } |
| 178 | /** |
| 179 | * Create a new nullable rule. |
| 180 | * |
| 181 | * @return NullableRule |
| 182 | * |
| 183 | * @since 1.0.0 |
| 184 | */ |
| 185 | public static function nullable() |
| 186 | { |
| 187 | return new NullableRule(); |
| 188 | } |
| 189 | /** |
| 190 | * Create a new sometimes rule. |
| 191 | * |
| 192 | * @return SometimesRule |
| 193 | * |
| 194 | * @since 1.0.0 |
| 195 | */ |
| 196 | public static function sometimes() |
| 197 | { |
| 198 | return new SometimesRule(); |
| 199 | } |
| 200 | /** |
| 201 | * Create a new in rule. |
| 202 | * |
| 203 | * @param array|Arrayable $values The allowed values. |
| 204 | * |
| 205 | * @return InRule |
| 206 | * |
| 207 | * @since 1.0.0 |
| 208 | */ |
| 209 | public static function in($values) |
| 210 | { |
| 211 | if ($values instanceof Arrayable) { |
| 212 | $values = $values->to_array(); |
| 213 | } |
| 214 | return new InRule(\is_array($values) ? $values : \func_get_args()); |
| 215 | } |
| 216 | /** |
| 217 | * Create a new not in rule. |
| 218 | * |
| 219 | * @param array|Arrayable $values The disallowed values. |
| 220 | * |
| 221 | * @return NotInRule |
| 222 | * |
| 223 | * @since 1.0.0 |
| 224 | */ |
| 225 | public static function not_in($values) |
| 226 | { |
| 227 | if ($values instanceof Arrayable) { |
| 228 | $values = $values->to_array(); |
| 229 | } |
| 230 | return new NotInRule(\is_array($values) ? $values : \func_get_args()); |
| 231 | } |
| 232 | /** |
| 233 | * Create a new boolean rule. |
| 234 | * |
| 235 | * @param string|null $args The rule arguments, pass 'strict' for strict boolean checks. |
| 236 | * |
| 237 | * @return BooleanRule |
| 238 | * |
| 239 | * @since 1.0.0 |
| 240 | */ |
| 241 | public static function boolean($args = null) |
| 242 | { |
| 243 | return new BooleanRule($args); |
| 244 | } |
| 245 | /** |
| 246 | * Create a new date rule. |
| 247 | * |
| 248 | * @return DateRule |
| 249 | * |
| 250 | * @since 1.0.0 |
| 251 | */ |
| 252 | public static function date() |
| 253 | { |
| 254 | return new DateRule(); |
| 255 | } |
| 256 | /** |
| 257 | * Create a new datetime rule. |
| 258 | * |
| 259 | * @return DateRule |
| 260 | * |
| 261 | * @since 1.0.0 |
| 262 | */ |
| 263 | public static function datetime() |
| 264 | { |
| 265 | return (new DateRule())->datetime(); |
| 266 | } |
| 267 | /** |
| 268 | * Create a new object rule. |
| 269 | * |
| 270 | * @return ObjectRule |
| 271 | * |
| 272 | * @since 1.0.0 |
| 273 | */ |
| 274 | public static function object() |
| 275 | { |
| 276 | return new ObjectRule(); |
| 277 | } |
| 278 | /** |
| 279 | * Create a new prohibited rule. |
| 280 | * |
| 281 | * @return ProhibitedRule |
| 282 | * |
| 283 | * @since 1.0.0 |
| 284 | */ |
| 285 | public static function prohibited() |
| 286 | { |
| 287 | return new ProhibitedRule(); |
| 288 | } |
| 289 | /** |
| 290 | * Create a new prohibited if rule. |
| 291 | * |
| 292 | * @param string $other The other field to check. |
| 293 | * @param mixed $value The value to check. |
| 294 | * |
| 295 | * @return ProhibitedIfRule |
| 296 | * |
| 297 | * @since 1.0.0 |
| 298 | */ |
| 299 | public static function prohibited_if($other, $value = null) |
| 300 | { |
| 301 | return new ProhibitedIfRule(static::process_conditional_arguments($other, $value)); |
| 302 | } |
| 303 | /** |
| 304 | * Create a new prohibited unless rule. |
| 305 | * |
| 306 | * @param string $other The other field to check. |
| 307 | * @param mixed $value The value to check. |
| 308 | * |
| 309 | * @return ProhibitedUnlessRule |
| 310 | * |
| 311 | * @since 1.0.0 |
| 312 | */ |
| 313 | public static function prohibited_unless($other, $value = null) |
| 314 | { |
| 315 | return new ProhibitedUnlessRule(static::process_conditional_arguments($other, $value)); |
| 316 | } |
| 317 | /** |
| 318 | * Create a new same as rule. |
| 319 | * |
| 320 | * @param string $field The field to match against. |
| 321 | * |
| 322 | * @return SameAsRule |
| 323 | * |
| 324 | * @since 1.0.0 |
| 325 | */ |
| 326 | public static function same_as($field) |
| 327 | { |
| 328 | return new SameAsRule($field); |
| 329 | } |
| 330 | /** |
| 331 | * Create a new exists rule. |
| 332 | * |
| 333 | * @param string|array $table The table name, or [table, column] arguments. |
| 334 | * @param string|null $column The column name, defaults to the field name. |
| 335 | * |
| 336 | * @return ExistsRule |
| 337 | * |
| 338 | * @since 1.0.0 |
| 339 | */ |
| 340 | public static function exists($table, $column = null) |
| 341 | { |
| 342 | return new ExistsRule(static::table_arguments($table, $column)); |
| 343 | } |
| 344 | /** |
| 345 | * Create a new unique rule. |
| 346 | * |
| 347 | * @param string|array $table The table name, or [table, column, ignore_id] arguments. |
| 348 | * @param string|null $column The column name, defaults to the field name. |
| 349 | * @param int|string|null $ignore_id The id of a record to ignore. |
| 350 | * |
| 351 | * @return UniqueRule |
| 352 | * |
| 353 | * @since 1.0.0 |
| 354 | */ |
| 355 | public static function unique($table, $column = null, $ignore_id = null) |
| 356 | { |
| 357 | return new UniqueRule(static::table_arguments($table, $column, $ignore_id)); |
| 358 | } |
| 359 | /** |
| 360 | * Create a new image rule. |
| 361 | * |
| 362 | * @return FileRule |
| 363 | * |
| 364 | * @since 1.0.0 |
| 365 | */ |
| 366 | public static function image() |
| 367 | { |
| 368 | return (new FileRule())->image(); |
| 369 | } |
| 370 | /** |
| 371 | * Create a new file rule. |
| 372 | * |
| 373 | * @return FileRule |
| 374 | * |
| 375 | * @since 1.0.0 |
| 376 | */ |
| 377 | public static function file() |
| 378 | { |
| 379 | return new FileRule(); |
| 380 | } |
| 381 | /** |
| 382 | * Create a new when rule. |
| 383 | * |
| 384 | * @param bool|callable $condition The boolean condition for which the rules should be applied. |
| 385 | * @param array $rules The rules to apply when the condition is true. |
| 386 | * @param array $default_rules The rules to apply when the condition is false. |
| 387 | * |
| 388 | * @return ConditionalRule |
| 389 | * |
| 390 | * @since 1.0.0 |
| 391 | */ |
| 392 | public static function when($condition, $rules, $default_rules = []) |
| 393 | { |
| 394 | return new ConditionalRule($condition, $rules, $default_rules); |
| 395 | } |
| 396 | /** |
| 397 | * Create a new unless rule. |
| 398 | * |
| 399 | * @param bool|callable $condition The boolean condition for which the rules should be applied. |
| 400 | * @param array $rules The rules to apply when the condition is false. |
| 401 | * @param array $default_rules The rules to apply when the condition is true. |
| 402 | * |
| 403 | * @return ConditionalRule |
| 404 | * |
| 405 | * @since 1.0.0 |
| 406 | */ |
| 407 | public static function unless($condition, $rules, $default_rules = []) |
| 408 | { |
| 409 | return new ConditionalRule($condition, $default_rules, $rules); |
| 410 | } |
| 411 | /** |
| 412 | * Normalize table based rule arguments into a single array. |
| 413 | * |
| 414 | * @param string|array $table The table name or a full arguments array. |
| 415 | * @param string|null $column The column name. |
| 416 | * @param int|string|null $ignore_id The id of a record to ignore. |
| 417 | * |
| 418 | * @return array |
| 419 | * |
| 420 | * @since 1.0.0 |
| 421 | */ |
| 422 | protected static function table_arguments($table, $column = null, $ignore_id = null) |
| 423 | { |
| 424 | if (\is_array($table)) { |
| 425 | return $table; |
| 426 | } |
| 427 | return \array_filter([$table, $column, $ignore_id], fn($argument) => $argument !== null); |
| 428 | } |
| 429 | /** |
| 430 | * Normalize required if rule arguments into a single array. |
| 431 | * |
| 432 | * @param string $other The other field to check. |
| 433 | * @param mixed $value The value to check. |
| 434 | * |
| 435 | * @return array |
| 436 | * |
| 437 | * @since 1.0.0 |
| 438 | */ |
| 439 | protected static function process_conditional_arguments($other, $value) |
| 440 | { |
| 441 | if (\is_array($other)) { |
| 442 | return $other; |
| 443 | } |
| 444 | return \array_merge(Arr::wrap($other), Arr::wrap($value)); |
| 445 | } |
| 446 | } |
| 447 |