ExcludeUnless.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Stringable;
  6. class ExcludeUnless implements Stringable
  7. {
  8. /**
  9. * The condition that validates the attribute.
  10. *
  11. * @var (\Closure(): bool)|bool
  12. */
  13. public $condition;
  14. /**
  15. * Create a new exclude validation rule based on a condition.
  16. *
  17. * @param (\Closure(): bool)|bool $condition
  18. *
  19. * @throws \InvalidArgumentException
  20. */
  21. public function __construct($condition)
  22. {
  23. if ($condition instanceof Closure || is_bool($condition)) {
  24. $this->condition = $condition;
  25. } else {
  26. throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
  27. }
  28. }
  29. /**
  30. * Convert the rule to a validation string.
  31. *
  32. * @return string
  33. */
  34. public function __toString()
  35. {
  36. if (is_callable($this->condition)) {
  37. return call_user_func($this->condition) ? '' : 'exclude';
  38. }
  39. return $this->condition ? '' : 'exclude';
  40. }
  41. }