Debian Codegraph @

lib/3rdParty/composer/illuminate/support/ValidatedInput.php

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