NotIn.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Illuminate\Validation\Rules;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Stringable;
  5. use function Illuminate\Support\enum_value;
  6. class NotIn implements Stringable
  7. {
  8. /**
  9. * The name of the rule.
  10. *
  11. * @var string
  12. */
  13. protected $rule = 'not_in';
  14. /**
  15. * The accepted values.
  16. *
  17. * @var array
  18. */
  19. protected $values;
  20. /**
  21. * Create a new "not in" rule instance.
  22. *
  23. * @param \Illuminate\Contracts\Support\Arrayable|\UnitEnum|array|string $values
  24. */
  25. public function __construct($values)
  26. {
  27. if ($values instanceof Arrayable) {
  28. $values = $values->toArray();
  29. }
  30. $this->values = is_array($values) ? $values : func_get_args();
  31. }
  32. /**
  33. * Convert the rule to a validation string.
  34. *
  35. * @return string
  36. */
  37. public function __toString()
  38. {
  39. $values = array_map(function ($value) {
  40. $value = enum_value($value);
  41. return '"'.str_replace('"', '""', $value).'"';
  42. }, $this->values);
  43. return $this->rule.':'.implode(',', $values);
  44. }
  45. }