StringRule.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Traits\Conditionable;
  5. use Stringable;
  6. class StringRule implements Stringable
  7. {
  8. use Conditionable;
  9. /**
  10. * The constraints for the string rule.
  11. */
  12. protected array $constraints = ['string'];
  13. /**
  14. * The field under validation must be entirely alphabetic characters.
  15. *
  16. * @param bool $ascii
  17. * @return $this
  18. */
  19. public function alpha(bool $ascii = false): static
  20. {
  21. return $this->addRule($ascii ? 'alpha:ascii' : 'alpha');
  22. }
  23. /**
  24. * The field under validation must be entirely alpha-numeric characters, dashes, and underscores.
  25. *
  26. * @param bool $ascii
  27. * @return $this
  28. */
  29. public function alphaDash(bool $ascii = false): static
  30. {
  31. return $this->addRule($ascii ? 'alpha_dash:ascii' : 'alpha_dash');
  32. }
  33. /**
  34. * The field under validation must be entirely alpha-numeric characters.
  35. *
  36. * @param bool $ascii
  37. * @return $this
  38. */
  39. public function alphaNumeric(bool $ascii = false): static
  40. {
  41. return $this->addRule($ascii ? 'alpha_num:ascii' : 'alpha_num');
  42. }
  43. /**
  44. * The field under validation must be entirely ASCII characters.
  45. *
  46. * @return $this
  47. */
  48. public function ascii(): static
  49. {
  50. return $this->addRule('ascii');
  51. }
  52. /**
  53. * The field under validation must have a length between the given min and max (inclusive).
  54. *
  55. * @param int $min
  56. * @param int $max
  57. * @return $this
  58. */
  59. public function between(int $min, int $max): static
  60. {
  61. return $this->addRule('between:'.$min.','.$max);
  62. }
  63. /**
  64. * The field under validation must not end with any of the given values.
  65. *
  66. * @param string ...$values
  67. * @return $this
  68. */
  69. public function doesntEndWith(string ...$values): static
  70. {
  71. return $this->addRule('doesnt_end_with:'.implode(',', $values));
  72. }
  73. /**
  74. * The field under validation must not start with any of the given values.
  75. *
  76. * @param string ...$values
  77. * @return $this
  78. */
  79. public function doesntStartWith(string ...$values): static
  80. {
  81. return $this->addRule('doesnt_start_with:'.implode(',', $values));
  82. }
  83. /**
  84. * The field under validation must end with one of the given values.
  85. *
  86. * @param string ...$values
  87. * @return $this
  88. */
  89. public function endsWith(string ...$values): static
  90. {
  91. return $this->addRule('ends_with:'.implode(',', $values));
  92. }
  93. /**
  94. * The field under validation must have an exact length.
  95. *
  96. * @param int $value
  97. * @return $this
  98. */
  99. public function exactly(int $value): static
  100. {
  101. return $this->addRule('size:'.$value);
  102. }
  103. /**
  104. * The field under validation must be entirely lowercase.
  105. *
  106. * @return $this
  107. */
  108. public function lowercase(): static
  109. {
  110. return $this->addRule('lowercase');
  111. }
  112. /**
  113. * The field under validation must not exceed the given length.
  114. *
  115. * @param int $value
  116. * @return $this
  117. */
  118. public function max(int $value): static
  119. {
  120. return $this->addRule('max:'.$value);
  121. }
  122. /**
  123. * The field under validation must have a minimum length.
  124. *
  125. * @param int $value
  126. * @return $this
  127. */
  128. public function min(int $value): static
  129. {
  130. return $this->addRule('min:'.$value);
  131. }
  132. /**
  133. * The field under validation must start with one of the given values.
  134. *
  135. * @param string ...$values
  136. * @return $this
  137. */
  138. public function startsWith(string ...$values): static
  139. {
  140. return $this->addRule('starts_with:'.implode(',', $values));
  141. }
  142. /**
  143. * The field under validation must be entirely uppercase.
  144. *
  145. * @return $this
  146. */
  147. public function uppercase(): static
  148. {
  149. return $this->addRule('uppercase');
  150. }
  151. /**
  152. * Convert the rule to a validation string.
  153. */
  154. public function __toString(): string
  155. {
  156. return implode('|', array_unique($this->constraints));
  157. }
  158. /**
  159. * Add custom rules to the validation rules array.
  160. */
  161. protected function addRule(array|string $rules): static
  162. {
  163. $this->constraints = array_merge($this->constraints, Arr::wrap($rules));
  164. return $this;
  165. }
  166. }