Date.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use DateTimeInterface;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Traits\Conditionable;
  6. use Illuminate\Support\Traits\Macroable;
  7. use Stringable;
  8. class Date implements Stringable
  9. {
  10. use Conditionable, Macroable;
  11. /**
  12. * The format of the date.
  13. */
  14. protected ?string $format = null;
  15. /**
  16. * The constraints for the date rule.
  17. */
  18. protected array $constraints = [];
  19. /**
  20. * Ensure the date has the given format.
  21. */
  22. public function format(string $format): static
  23. {
  24. $this->format = $format;
  25. return $this;
  26. }
  27. /**
  28. * Ensure the date is before today.
  29. */
  30. public function beforeToday(): static
  31. {
  32. return $this->before('today');
  33. }
  34. /**
  35. * Ensure the date is after today.
  36. */
  37. public function afterToday(): static
  38. {
  39. return $this->after('today');
  40. }
  41. /**
  42. * Ensure the date is before or equal to today.
  43. */
  44. public function todayOrBefore(): static
  45. {
  46. return $this->beforeOrEqual('today');
  47. }
  48. /**
  49. * Ensure the date is after or equal to today.
  50. */
  51. public function todayOrAfter(): static
  52. {
  53. return $this->afterOrEqual('today');
  54. }
  55. /**
  56. * Ensure the date is in the past.
  57. */
  58. public function past(): static
  59. {
  60. return $this->before('now');
  61. }
  62. /**
  63. * Ensure the date is in the future.
  64. */
  65. public function future(): static
  66. {
  67. return $this->after('now');
  68. }
  69. /**
  70. * Ensure the date is now or in the past.
  71. */
  72. public function nowOrPast(): static
  73. {
  74. return $this->beforeOrEqual('now');
  75. }
  76. /**
  77. * Ensure the date is now or in the future.
  78. */
  79. public function nowOrFuture(): static
  80. {
  81. return $this->afterOrEqual('now');
  82. }
  83. /**
  84. * Ensure the date is before the given date or date field.
  85. */
  86. public function before(DateTimeInterface|string $date): static
  87. {
  88. return $this->addRule('before:'.$this->formatDate($date));
  89. }
  90. /**
  91. * Ensure the date is after the given date or date field.
  92. */
  93. public function after(DateTimeInterface|string $date): static
  94. {
  95. return $this->addRule('after:'.$this->formatDate($date));
  96. }
  97. /**
  98. * Ensure the date is on or before the specified date or date field.
  99. */
  100. public function beforeOrEqual(DateTimeInterface|string $date): static
  101. {
  102. return $this->addRule('before_or_equal:'.$this->formatDate($date));
  103. }
  104. /**
  105. * Ensure the date is on or after the given date or date field.
  106. */
  107. public function afterOrEqual(DateTimeInterface|string $date): static
  108. {
  109. return $this->addRule('after_or_equal:'.$this->formatDate($date));
  110. }
  111. /**
  112. * Ensure the date is between two dates or date fields.
  113. */
  114. public function between(DateTimeInterface|string $from, DateTimeInterface|string $to): static
  115. {
  116. return $this->after($from)->before($to);
  117. }
  118. /**
  119. * Ensure the date is between or equal to two dates or date fields.
  120. */
  121. public function betweenOrEqual(DateTimeInterface|string $from, DateTimeInterface|string $to): static
  122. {
  123. return $this->afterOrEqual($from)->beforeOrEqual($to);
  124. }
  125. /**
  126. * Add custom rules to the validation rules array.
  127. */
  128. protected function addRule(array|string $rules): static
  129. {
  130. $this->constraints = array_merge($this->constraints, Arr::wrap($rules));
  131. return $this;
  132. }
  133. /**
  134. * Format the date for the validation rule.
  135. */
  136. protected function formatDate(DateTimeInterface|string $date): string
  137. {
  138. return $date instanceof DateTimeInterface
  139. ? $date->format($this->format ?? 'Y-m-d')
  140. : $date;
  141. }
  142. /**
  143. * Convert the rule to a validation string.
  144. */
  145. public function __toString(): string
  146. {
  147. return implode('|', [
  148. $this->format === null ? 'date' : 'date_format:'.$this->format,
  149. ...$this->constraints,
  150. ]);
  151. }
  152. }