Skip to content

Commit 0a38fd6

Browse files
HajMotaylorotwell
andauthored
[8.x] Add merge() function to ValidatedInput (#38640)
* Add `merge()` function * Add test for `merge()` function * Fix the test * Update ValidatedInput.php * Update ValidatedInputTest.php Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent cdb14c7 commit 0a38fd6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Illuminate/Support/ValidatedInput.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public function except($keys)
6868
return $results;
6969
}
7070

71+
/**
72+
* Merge the validated input with the given array of additional data.
73+
*
74+
* @param array $items
75+
* @return static
76+
*/
77+
public function merge(array $items)
78+
{
79+
return new static(array_merge($this->input, $items));
80+
}
81+
7182
/**
7283
* Get the input as a collection.
7384
*

tests/Support/ValidatedInputTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@ public function test_can_access_input()
1717
$this->assertEquals(['name' => 'Taylor'], $input->except(['votes']));
1818
$this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all());
1919
}
20+
21+
public function test_can_merge_items()
22+
{
23+
$input = new ValidatedInput(['name' => 'Taylor']);
24+
25+
$input = $input->merge(['votes' => 100]);
26+
27+
$this->assertEquals('Taylor', $input->name);
28+
$this->assertEquals('Taylor', $input['name']);
29+
$this->assertEquals(['name' => 'Taylor'], $input->only(['name']));
30+
$this->assertEquals(['name' => 'Taylor'], $input->except(['votes']));
31+
$this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all());
32+
}
2033
}

0 commit comments

Comments
 (0)