ArrayRule.php 1006 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 ArrayRule implements Stringable
  7. {
  8. /**
  9. * The accepted keys.
  10. *
  11. * @var array
  12. */
  13. protected $keys;
  14. /**
  15. * Create a new array rule instance.
  16. *
  17. * @param \Illuminate\Contracts\Support\Arrayable|array|null $keys
  18. */
  19. public function __construct($keys = null)
  20. {
  21. if ($keys instanceof Arrayable) {
  22. $keys = $keys->toArray();
  23. }
  24. $this->keys = is_array($keys) ? $keys : func_get_args();
  25. }
  26. /**
  27. * Convert the rule to a validation string.
  28. *
  29. * @return string
  30. */
  31. public function __toString()
  32. {
  33. if (empty($this->keys)) {
  34. return 'array';
  35. }
  36. $keys = array_map(
  37. static fn ($key) => enum_value($key),
  38. $this->keys,
  39. );
  40. return 'array:'.implode(',', $keys);
  41. }
  42. }