Skip to content

Commit 3591526

Browse files
[8.x] Validated subsets (#38366)
* conditional rules * simplify some code * allow closures on conditional * Apply fixes from StyleCI * fix type hint * onlyValidated and exceptValidated * change approach * Apply fixes from StyleCI * magic methods * Update src/Illuminate/Support/ValidatedInput.php Co-authored-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * use validator method * add test case Co-authored-by: Taylor Otwell <[email protected]> Co-authored-by: Mior Muhammad Zaki <[email protected]>
1 parent f3470df commit 3591526

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Illuminate\Contracts\Support;
4+
5+
use ArrayAccess;
6+
use IteratorAggregate;
7+
8+
interface ValidatedData extends Arrayable, ArrayAccess, IteratorAggregate
9+
{
10+
//
11+
}

src/Illuminate/Foundation/Http/FormRequest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ protected function failedAuthorization()
185185
throw new AuthorizationException;
186186
}
187187

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

src/Illuminate/Validation/Validator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Support\Fluent;
1515
use Illuminate\Support\MessageBag;
1616
use Illuminate\Support\Str;
17+
use Illuminate\Support\ValidatedInput;
1718
use RuntimeException;
1819
use stdClass;
1920
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -500,6 +501,16 @@ public function validateWithBag(string $errorBag)
500501
}
501502
}
502503

504+
/**
505+
* Get a validated input container for the validated input.
506+
*
507+
* @return \Illuminate\Support\ValidatedInput
508+
*/
509+
public function safe()
510+
{
511+
return new ValidatedInput($this->validated());
512+
}
513+
503514
/**
504515
* Get the attributes and values that were validated.
505516
*

tests/Support/ValidatedInputTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Support;
4+
5+
use Illuminate\Support\ValidatedInput;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ValidatedInputTest extends TestCase
9+
{
10+
public function test_can_access_input()
11+
{
12+
$input = new ValidatedInput(['name' => 'Taylor', 'votes' => 100]);
13+
14+
$this->assertEquals('Taylor', $input->name);
15+
$this->assertEquals('Taylor', $input['name']);
16+
$this->assertEquals(['name' => 'Taylor'], $input->only(['name']));
17+
$this->assertEquals(['name' => 'Taylor'], $input->except(['votes']));
18+
$this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all());
19+
}
20+
}

0 commit comments

Comments
 (0)