|
4 | 4 |
|
5 | 5 | use Illuminate\Support\Str; |
6 | 6 | use Saritasa\Enum; |
| 7 | +use Saritasa\Laravel\Validation\Rules\ContainsLowercase; |
| 8 | +use Saritasa\Laravel\Validation\Rules\ContainsNumeral; |
| 9 | +use Saritasa\Laravel\Validation\Rules\ContainsUppercase; |
7 | 10 |
|
8 | 11 | /** |
9 | 12 | * Rules, that are reasonable for strings only |
|
22 | 25 | */ |
23 | 26 | class StringRuleSet extends RuleSet |
24 | 27 | { |
25 | | - const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex', 'enum']; |
| 28 | + const EXPOSED_RULES = ['email', 'regex', 'timezone', 'phoneRegex', 'enum', ]; |
26 | 29 |
|
27 | 30 | const TRIVIAL_STRING_RULES = [ |
28 | 31 | 'activeUrl', |
@@ -79,6 +82,44 @@ public function enum(string $enumClass): StringRuleSet |
79 | 82 | return $this->appendIfNotExists(Rule::in($enumClass::getConstants())); |
80 | 83 | } |
81 | 84 |
|
| 85 | + public function enumName(string $enumClass): StringRuleSet |
| 86 | + { |
| 87 | + if (!is_a($enumClass, Enum::class, true)) { |
| 88 | + throw new \UnexpectedValueException('Class is not enum'); |
| 89 | + } |
| 90 | + return $this->appendIfNotExists(Rule::enumName(array_keys($enumClass::getConstants()))); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * The field under validation must contain lowercase letters |
| 95 | + * |
| 96 | + * @return StringRuleSet |
| 97 | + */ |
| 98 | + public function containsLowercase(): StringRuleSet |
| 99 | + { |
| 100 | + return $this->regex(ContainsLowercase::REGEX); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * The field under validation must contain uppercase letters |
| 105 | + * |
| 106 | + * @return StringRuleSet |
| 107 | + */ |
| 108 | + public function containsUppercase(): StringRuleSet |
| 109 | + { |
| 110 | + return $this->regex(ContainsUppercase::REGEX); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * The field under validation must contain numbers |
| 115 | + * |
| 116 | + * @return StringRuleSet |
| 117 | + */ |
| 118 | + public function containsNumeral(): StringRuleSet |
| 119 | + { |
| 120 | + return $this->regex(ContainsNumeral::REGEX); |
| 121 | + } |
| 122 | + |
82 | 123 | /** |
83 | 124 | * Shortcut method for validating phone with use regex. |
84 | 125 | * The method uses E.164 format for validation. (ex: +12345678901) |
|
0 commit comments