ValidatedInput.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayIterator;
  4. use Illuminate\Contracts\Support\ValidatedData;
  5. use Illuminate\Support\Traits\Dumpable;
  6. use Illuminate\Support\Traits\InteractsWithData;
  7. use Traversable;
  8. class ValidatedInput implements ValidatedData
  9. {
  10. use Dumpable, InteractsWithData;
  11. /**
  12. * The underlying input.
  13. *
  14. * @var array
  15. */
  16. protected $input;
  17. /**
  18. * Create a new validated input container.
  19. *
  20. * @param array $input
  21. */
  22. public function __construct(array $input)
  23. {
  24. $this->input = $input;
  25. }
  26. /**
  27. * Merge the validated input with the given array of additional data.
  28. *
  29. * @param array $items
  30. * @return static
  31. */
  32. public function merge(array $items)
  33. {
  34. return new static(array_merge($this->all(), $items));
  35. }
  36. /**
  37. * Get the raw, underlying input array.
  38. *
  39. * @param mixed $keys
  40. * @return array
  41. */
  42. public function all($keys = null)
  43. {
  44. if (! $keys) {
  45. return $this->input;
  46. }
  47. $input = [];
  48. foreach (is_array($keys) ? $keys : func_get_args() as $key) {
  49. Arr::set($input, $key, Arr::get($this->input, $key));
  50. }
  51. return $input;
  52. }
  53. /**
  54. * Retrieve data from the instance.
  55. *
  56. * @param string|null $key
  57. * @param mixed $default
  58. * @return mixed
  59. */
  60. protected function data($key = null, $default = null)
  61. {
  62. return $this->input($key, $default);
  63. }
  64. /**
  65. * Get the keys for all of the input.
  66. *
  67. * @return array
  68. */
  69. public function keys()
  70. {
  71. return array_keys($this->input());
  72. }
  73. /**
  74. * Retrieve an input item from the validated inputs.
  75. *
  76. * @param string|null $key
  77. * @param mixed $default
  78. * @return mixed
  79. */
  80. public function input($key = null, $default = null)
  81. {
  82. return data_get(
  83. $this->all(), $key, $default
  84. );
  85. }
  86. /**
  87. * Dump the items.
  88. *
  89. * @param mixed ...$keys
  90. * @return $this
  91. */
  92. public function dump(...$keys)
  93. {
  94. dump(count($keys) > 0 ? $this->only($keys) : $this->all());
  95. return $this;
  96. }
  97. /**
  98. * Get the instance as an array.
  99. *
  100. * @return array
  101. */
  102. public function toArray()
  103. {
  104. return $this->all();
  105. }
  106. /**
  107. * Dynamically access input data.
  108. *
  109. * @param string $name
  110. * @return mixed
  111. */
  112. public function __get($name)
  113. {
  114. return $this->input($name);
  115. }
  116. /**
  117. * Dynamically set input data.
  118. *
  119. * @param string $name
  120. * @param mixed $value
  121. * @return mixed
  122. */
  123. public function __set($name, $value)
  124. {
  125. $this->input[$name] = $value;
  126. }
  127. /**
  128. * Determine if an input item is set.
  129. *
  130. * @param string $name
  131. * @return bool
  132. */
  133. public function __isset($name)
  134. {
  135. return $this->exists($name);
  136. }
  137. /**
  138. * Remove an input item.
  139. *
  140. * @param string $name
  141. * @return void
  142. */
  143. public function __unset($name)
  144. {
  145. unset($this->input[$name]);
  146. }
  147. /**
  148. * Determine if an item exists at an offset.
  149. *
  150. * @param mixed $key
  151. * @return bool
  152. */
  153. public function offsetExists($key): bool
  154. {
  155. return $this->exists($key);
  156. }
  157. /**
  158. * Get an item at a given offset.
  159. *
  160. * @param mixed $key
  161. * @return mixed
  162. */
  163. public function offsetGet($key): mixed
  164. {
  165. return $this->input($key);
  166. }
  167. /**
  168. * Set the item at a given offset.
  169. *
  170. * @param mixed $key
  171. * @param mixed $value
  172. * @return void
  173. */
  174. public function offsetSet($key, $value): void
  175. {
  176. if (is_null($key)) {
  177. $this->input[] = $value;
  178. } else {
  179. $this->input[$key] = $value;
  180. }
  181. }
  182. /**
  183. * Unset the item at a given offset.
  184. *
  185. * @param string $key
  186. * @return void
  187. */
  188. public function offsetUnset($key): void
  189. {
  190. unset($this->input[$key]);
  191. }
  192. /**
  193. * Get an iterator for the input.
  194. *
  195. * @return \ArrayIterator
  196. */
  197. public function getIterator(): Traversable
  198. {
  199. return new ArrayIterator($this->input);
  200. }
  201. }