Skip to content

Commit 4768e94

Browse files
[9.x] Added has and missing methods to ValidatedInput (#42184)
* Added and methods to ValidatedInput. * Fixed style * Updated doc-block * Update src/Illuminate/Support/ValidatedInput.php Co-authored-by: Dries Vints <[email protected]> * Fixed all instances of array|mixed * Changed to missing and updated comments * Switched to Arr::has() Co-authored-by: Dries Vints <[email protected]>
1 parent a880574 commit 4768e94

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Illuminate/Support/ValidatedInput.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(array $input)
3030
/**
3131
* Get a subset containing the provided keys with values from the input data.
3232
*
33-
* @param array|mixed $keys
33+
* @param mixed $keys
3434
* @return array
3535
*/
3636
public function only($keys)
@@ -55,7 +55,7 @@ public function only($keys)
5555
/**
5656
* Get all of the input except for a specified array of items.
5757
*
58-
* @param array|mixed $keys
58+
* @param mixed $keys
5959
* @return array
6060
*/
6161
public function except($keys)
@@ -100,6 +100,36 @@ public function all()
100100
return $this->input;
101101
}
102102

103+
/**
104+
* Determine if the validated input has one or more keys.
105+
*
106+
* @param mixed $keys
107+
* @return bool
108+
*/
109+
public function has($keys)
110+
{
111+
$keys = is_array($keys) ? $keys : func_get_args();
112+
113+
foreach ($keys as $key) {
114+
if (! Arr::has($this->input, $key)) {
115+
return false;
116+
}
117+
}
118+
119+
return true;
120+
}
121+
122+
/**
123+
* Determine if the validated input is missing one or more keys.
124+
*
125+
* @param mixed $keys
126+
* @return bool
127+
*/
128+
public function missing($keys)
129+
{
130+
return ! $this->has($keys);
131+
}
132+
103133
/**
104134
* Get the instance as an array.
105135
*

tests/Support/ValidatedInputTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public function test_can_merge_items()
3030
$this->assertEquals(['name' => 'Taylor'], $input->except(['votes']));
3131
$this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all());
3232
}
33+
34+
public function test_input_existence()
35+
{
36+
$inputA = new ValidatedInput(['name' => 'Taylor']);
37+
38+
$this->assertEquals(true, $inputA->has('name'));
39+
$this->assertEquals(true, $inputA->missing('votes'));
40+
$this->assertEquals(true, $inputA->missing(['votes']));
41+
$this->assertEquals(false, $inputA->missing('name'));
42+
43+
$inputB = new ValidatedInput(['name' => 'Taylor', 'votes' => 100]);
44+
45+
$this->assertEquals(true, $inputB->has(['name', 'votes']));
46+
}
3347
}

0 commit comments

Comments
 (0)