Abstracts
2 years ago
Boolean.php
2 years ago
Currency.php
2 years ago
DateTime.php
2 years ago
Email.php
2 years ago
Exclude.php
2 years ago
ExcludeIf.php
2 years ago
ExcludeUnless.php
2 years ago
In.php
2 years ago
InStrict.php
2 years ago
Integer.php
2 years ago
Max.php
2 years ago
Min.php
2 years ago
Nullable.php
2 years ago
NullableIf.php
2 years ago
NullableUnless.php
2 years ago
Numeric.php
2 years ago
Optional.php
2 years ago
OptionalIf.php
2 years ago
OptionalUnless.php
2 years ago
Required.php
2 years ago
Size.php
2 years ago
In.php
79 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * |
| 5 | * Modified by impress-org on 30-October-2023 using Strauss. |
| 6 | * @see https://github.com/BrianHenryIE/strauss |
| 7 | */ |
| 8 | |
| 9 | namespace Give\Vendors\StellarWP\Validation\Rules; |
| 10 | |
| 11 | use Closure; |
| 12 | use Give\Vendors\StellarWP\Validation\Config; |
| 13 | use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 14 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 15 | |
| 16 | class In implements ValidationRule, ValidatesOnFrontEnd |
| 17 | { |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $acceptedValues; |
| 22 | |
| 23 | /** |
| 24 | * @since 1.2.0 |
| 25 | */ |
| 26 | public static function id(): string |
| 27 | { |
| 28 | return 'in'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @since 1.2.0 |
| 33 | */ |
| 34 | final public function __construct(...$acceptedValues) |
| 35 | { |
| 36 | if (empty($acceptedValues)) { |
| 37 | Config::throwInvalidArgumentException('The In rule requires at least one value to be specified.'); |
| 38 | } |
| 39 | |
| 40 | $this->acceptedValues = $acceptedValues; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @since 1.2.0 |
| 45 | */ |
| 46 | public static function fromString(string $options = null): ValidationRule |
| 47 | { |
| 48 | if (empty(trim($options))) { |
| 49 | Config::throwInvalidArgumentException('The In rule requires at least one value to be specified.'); |
| 50 | } |
| 51 | |
| 52 | $values = explode(',', $options); |
| 53 | |
| 54 | if (empty($values)) { |
| 55 | Config::throwInvalidArgumentException('The In rule requires at least one value to be specified.'); |
| 56 | } |
| 57 | |
| 58 | return new static(...$values); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @since 1.2.0 |
| 63 | */ |
| 64 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 65 | { |
| 66 | if (!in_array($value, $this->acceptedValues)) { |
| 67 | $fail(sprintf(__('%s must be one of %s', 'give'), '{field}', implode(', ', $this->acceptedValues))); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @since 1.2.0 |
| 73 | */ |
| 74 | public function serializeOption(): array |
| 75 | { |
| 76 | return $this->acceptedValues; |
| 77 | } |
| 78 | } |
| 79 |