| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
protected $input; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct(array $input) |
| 28 |
{ |
| 29 |
$this->input = $input; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function merge(array $items) |
| 39 |
{ |
| 40 |
return new static(array_merge($this->all(), $items)); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected function data($key = null, $default = null) |
| 72 |
{ |
| 73 |
return $this->input($key, $default); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function keys() |
| 82 |
{ |
| 83 |
return array_keys($this->input()); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function input($key = null, $default = null) |
| 94 |
{ |
| 95 |
return data_get( |
| 96 |
$this->all(), $key, $default |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function toArray() |
| 119 |
{ |
| 120 |
return $this->all(); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function __get($name) |
| 130 |
{ |
| 131 |
return $this->input($name); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function __set($name, $value) |
| 142 |
{ |
| 143 |
$this->input[$name] = $value; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function __isset($name) |
| 153 |
{ |
| 154 |
return $this->exists($name); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function __unset($name) |
| 164 |
{ |
| 165 |
unset($this->input[$name]); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function offsetExists($key): bool |
| 175 |
{ |
| 176 |
return $this->exists($key); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public function offsetGet($key): mixed |
| 186 |
{ |
| 187 |
return $this->input($key); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 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 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function offsetUnset($key): void |
| 213 |
{ |
| 214 |
unset($this->input[$key]); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function getIterator(): Traversable |
| 223 |
{ |
| 224 |
return new ArrayIterator($this->input); |
| 225 |
} |
| 226 |
} |