Skip to content

Commit

Permalink
Merge now filters away invalid keys
Browse files Browse the repository at this point in the history
  • Loading branch information
prewk committed May 8, 2017
1 parent c375500 commit 6156eed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,14 @@ public function make(array $init = []) {
*/
public function merge($mergee) {
$data = $this->_recordData;
$fields = $this->getFields();
$record = new static($this->_validator);

foreach ($mergee as $key => $value) {
$record->validate($key, $value);
$data[$key] = $value;
if (in_array($key, $fields)) {
$record->validate($key, $value);
$data[$key] = $value;
}
}

$record->force($data);
Expand Down
15 changes: 15 additions & 0 deletions tests/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ public function test_that_it_merges_with_an_array()
$this->assertEquals(456, $record->get("baz"));
}

public function test_that_merging_filters_invalid_keys()
{
$record = new TestWithDefaultsRecord;

$record = $record->merge([
"foo" => "Foo",
"bar" => "Bar",
"qux" => "IGNORE",
]);

$this->assertEquals("Foo", $record->get("foo"));
$this->assertEquals("Bar", $record->get("bar"));
$this->assertEquals(456, $record->get("baz"));
}

public function test_that_it_merges_with_another_record()
{
$record = new TestWithDefaultsRecord;
Expand Down

0 comments on commit 6156eed

Please sign in to comment.