Skip to content

Commit

Permalink
make data_set handle non array targets (#13224)
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid authored and taylorotwell committed Apr 21, 2016
1 parent c16f026 commit a675623
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ function data_set(&$target, $key, $value, $overwrite = true)
} elseif ($overwrite || ! isset($target->{$segment})) {
$target->{$segment} = $value;
}
} else {
$target = [];

if ($segments) {
data_set($target[$segment], $segments, $value, $overwrite);
} elseif ($overwrite) {
$target[$segment] = $value;
}
}

return $target;
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ protected function initializeAttributeOnData($attribute)
return $data;
}

return data_fill($data, $attribute, null);
return data_set($data, $attribute, null, true);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,16 @@ public function testDataSet()
['foo' => ['bar' => 'boom'], 'baz' => 'kaboom'],
data_set($data, 'foo.bar', 'boom')
);

$this->assertEquals(
['foo' => ['bar' => 'boom'], 'baz' => ['bar' => 'boom']],
data_set($data, 'baz.bar', 'boom')
);

$this->assertEquals(
['foo' => ['bar' => 'boom'], 'baz' => ['bar' => ['boom' => ['kaboom' => 'boom']]]],
data_set($data, 'baz.bar.boom.kaboom', 'boom')
);
}

public function testDataSetWithStar()
Expand Down
4 changes: 4 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,10 @@ public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey()
{
$trans = $this->getRealTranslator();

$data = ['companies' => ['spark']];
$v = new Validator($trans, $data, ['companies.*.name' => 'required']);
$this->assertFalse($v->passes());

$data = ['names' => [['second' => 'I have no first']]];
$v = new Validator($trans, $data, ['names.*.first' => 'required']);
$this->assertFalse($v->passes());
Expand Down

0 comments on commit a675623

Please sign in to comment.