| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Illuminate\Validation\Rules;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Traits\Conditionable;
- use Stringable;
- class StringRule implements Stringable
- {
- use Conditionable;
- /**
- * The constraints for the string rule.
- */
- protected array $constraints = ['string'];
- /**
- * The field under validation must be entirely alphabetic characters.
- *
- * @param bool $ascii
- * @return $this
- */
- public function alpha(bool $ascii = false): static
- {
- return $this->addRule($ascii ? 'alpha:ascii' : 'alpha');
- }
- /**
- * The field under validation must be entirely alpha-numeric characters, dashes, and underscores.
- *
- * @param bool $ascii
- * @return $this
- */
- public function alphaDash(bool $ascii = false): static
- {
- return $this->addRule($ascii ? 'alpha_dash:ascii' : 'alpha_dash');
- }
- /**
- * The field under validation must be entirely alpha-numeric characters.
- *
- * @param bool $ascii
- * @return $this
- */
- public function alphaNumeric(bool $ascii = false): static
- {
- return $this->addRule($ascii ? 'alpha_num:ascii' : 'alpha_num');
- }
- /**
- * The field under validation must be entirely ASCII characters.
- *
- * @return $this
- */
- public function ascii(): static
- {
- return $this->addRule('ascii');
- }
- /**
- * The field under validation must have a length between the given min and max (inclusive).
- *
- * @param int $min
- * @param int $max
- * @return $this
- */
- public function between(int $min, int $max): static
- {
- return $this->addRule('between:'.$min.','.$max);
- }
- /**
- * The field under validation must not end with any of the given values.
- *
- * @param string ...$values
- * @return $this
- */
- public function doesntEndWith(string ...$values): static
- {
- return $this->addRule('doesnt_end_with:'.implode(',', $values));
- }
- /**
- * The field under validation must not start with any of the given values.
- *
- * @param string ...$values
- * @return $this
- */
- public function doesntStartWith(string ...$values): static
- {
- return $this->addRule('doesnt_start_with:'.implode(',', $values));
- }
- /**
- * The field under validation must end with one of the given values.
- *
- * @param string ...$values
- * @return $this
- */
- public function endsWith(string ...$values): static
- {
- return $this->addRule('ends_with:'.implode(',', $values));
- }
- /**
- * The field under validation must have an exact length.
- *
- * @param int $value
- * @return $this
- */
- public function exactly(int $value): static
- {
- return $this->addRule('size:'.$value);
- }
- /**
- * The field under validation must be entirely lowercase.
- *
- * @return $this
- */
- public function lowercase(): static
- {
- return $this->addRule('lowercase');
- }
- /**
- * The field under validation must not exceed the given length.
- *
- * @param int $value
- * @return $this
- */
- public function max(int $value): static
- {
- return $this->addRule('max:'.$value);
- }
- /**
- * The field under validation must have a minimum length.
- *
- * @param int $value
- * @return $this
- */
- public function min(int $value): static
- {
- return $this->addRule('min:'.$value);
- }
- /**
- * The field under validation must start with one of the given values.
- *
- * @param string ...$values
- * @return $this
- */
- public function startsWith(string ...$values): static
- {
- return $this->addRule('starts_with:'.implode(',', $values));
- }
- /**
- * The field under validation must be entirely uppercase.
- *
- * @return $this
- */
- public function uppercase(): static
- {
- return $this->addRule('uppercase');
- }
- /**
- * Convert the rule to a validation string.
- */
- public function __toString(): string
- {
- return implode('|', array_unique($this->constraints));
- }
- /**
- * Add custom rules to the validation rules array.
- */
- protected function addRule(array|string $rules): static
- {
- $this->constraints = array_merge($this->constraints, Arr::wrap($rules));
- return $this;
- }
- }
|