ValidatedInput.php 4.1 KB

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