ValidatedInput.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayIterator;
  4. use Illuminate\Contracts\Support\ValidatedData;
  5. use Illuminate\Support\Traits\InteractsWithData;
  6. use Symfony\Component\VarDumper\VarDumper;
  7. use Traversable;
  8. class ValidatedInput implements ValidatedData
  9. {
  10. use 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 validated inputs items and end the script.
  88. *
  89. * @param mixed ...$keys
  90. * @return never
  91. */
  92. public function dd(...$keys)
  93. {
  94. $this->dump(...$keys);
  95. exit(1);
  96. }
  97. /**
  98. * Dump the items.
  99. *
  100. * @param mixed $keys
  101. * @return $this
  102. */
  103. public function dump($keys = [])
  104. {
  105. $keys = is_array($keys) ? $keys : func_get_args();
  106. VarDumper::dump(count($keys) > 0 ? $this->only($keys) : $this->all());
  107. return $this;
  108. }
  109. /**
  110. * Get the instance as an array.
  111. *
  112. * @return array
  113. */
  114. public function toArray()
  115. {
  116. return $this->all();
  117. }
  118. /**
  119. * Dynamically access input data.
  120. *
  121. * @param string $name
  122. * @return mixed
  123. */
  124. public function __get($name)
  125. {
  126. return $this->input($name);
  127. }
  128. /**
  129. * Dynamically set input data.
  130. *
  131. * @param string $name
  132. * @param mixed $value
  133. * @return mixed
  134. */
  135. public function __set($name, $value)
  136. {
  137. $this->input[$name] = $value;
  138. }
  139. /**
  140. * Determine if an input item is set.
  141. *
  142. * @return bool
  143. */
  144. public function __isset($name)
  145. {
  146. return $this->exists($name);
  147. }
  148. /**
  149. * Remove an input item.
  150. *
  151. * @param string $name
  152. * @return void
  153. */
  154. public function __unset($name)
  155. {
  156. unset($this->input[$name]);
  157. }
  158. /**
  159. * Determine if an item exists at an offset.
  160. *
  161. * @param mixed $key
  162. * @return bool
  163. */
  164. public function offsetExists($key): bool
  165. {
  166. return $this->exists($key);
  167. }
  168. /**
  169. * Get an item at a given offset.
  170. *
  171. * @param mixed $key
  172. * @return mixed
  173. */
  174. public function offsetGet($key): mixed
  175. {
  176. return $this->input($key);
  177. }
  178. /**
  179. * Set the item at a given offset.
  180. *
  181. * @param mixed $key
  182. * @param mixed $value
  183. * @return void
  184. */
  185. public function offsetSet($key, $value): void
  186. {
  187. if (is_null($key)) {
  188. $this->input[] = $value;
  189. } else {
  190. $this->input[$key] = $value;
  191. }
  192. }
  193. /**
  194. * Unset the item at a given offset.
  195. *
  196. * @param string $key
  197. * @return void
  198. */
  199. public function offsetUnset($key): void
  200. {
  201. unset($this->input[$key]);
  202. }
  203. /**
  204. * Get an iterator for the input.
  205. *
  206. * @return \ArrayIterator
  207. */
  208. public function getIterator(): Traversable
  209. {
  210. return new ArrayIterator($this->input);
  211. }
  212. }