Skip to content

Commit

Permalink
[8.x] Add merge() function to ValidatedInput (#38640)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
HajMo and taylorotwell authored Sep 2, 2021
1 parent cdb14c7 commit 0a38fd6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Support/ValidatedInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ public function except($keys)
return $results;
}

/**
* Merge the validated input with the given array of additional data.
*
* @param array $items
* @return static
*/
public function merge(array $items)
{
return new static(array_merge($this->input, $items));
}

/**
* Get the input as a collection.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Support/ValidatedInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ public function test_can_access_input()
$this->assertEquals(['name' => 'Taylor'], $input->except(['votes']));
$this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all());
}

public function test_can_merge_items()
{
$input = new ValidatedInput(['name' => 'Taylor']);

$input = $input->merge(['votes' => 100]);

$this->assertEquals('Taylor', $input->name);
$this->assertEquals('Taylor', $input['name']);
$this->assertEquals(['name' => 'Taylor'], $input->only(['name']));
$this->assertEquals(['name' => 'Taylor'], $input->except(['votes']));
$this->assertEquals(['name' => 'Taylor', 'votes' => 100], $input->all());
}
}

0 comments on commit 0a38fd6

Please sign in to comment.