Skip to content

Commit ace404e

Browse files
authored
Merge branch 'master' into master
2 parents 3f67288 + eec567d commit ace404e

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

src/Helper.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public static function arrayHas(array $array, string $key): bool
5858
* Get an item from an array using "dot" notation.
5959
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Arr.php#L246
6060
*
61-
* @param array $array
62-
* @param string $key
63-
* @param mixed $default
61+
* @param array $array
62+
* @param string|null $key
63+
* @param mixed $default
6464
* @return mixed
6565
*/
66-
public static function arrayGet(array $array, string $key, $default = null)
66+
public static function arrayGet(array $array, $key, $default = null)
6767
{
6868
if (is_null($key)) {
6969
return $array;
@@ -111,14 +111,21 @@ public static function arrayDot(array $array, string $prepend = ''): array
111111
* Set an item on an array or object using dot notation.
112112
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/helpers.php#L437
113113
*
114-
* @param mixed $target
115-
* @param string|array $key
116-
* @param mixed $value
117-
* @param bool $overwrite
114+
* @param mixed $target
115+
* @param string|array|null $key
116+
* @param mixed $value
117+
* @param bool $overwrite
118118
* @return mixed
119119
*/
120120
public static function arraySet(&$target, $key, $value, $overwrite = true): array
121121
{
122+
if (is_null($key)) {
123+
if ($overwrite) {
124+
return $target = array_merge($target, $value);
125+
}
126+
return $target = array_merge($value, $target);
127+
}
128+
122129
$segments = is_array($key) ? $key : explode('.', $key);
123130

124131
if (($segment = array_shift($segments)) === '*') {

src/Validation.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public function extractValuesForWildcards(array $data, string $attributeKey): ar
282282
* Allows us to not spin through all of the flattened data for some operations.
283283
*
284284
* @param string $attributeKey
285-
* @return string
285+
* @return string|null null when root wildcard
286286
*/
287-
protected function getLeadingExplicitAttributePath(string $attributeKey): string
287+
protected function getLeadingExplicitAttributePath(string $attributeKey)
288288
{
289289
return rtrim(explode('*', $attributeKey)[0], '.') ?: null;
290290
}
@@ -295,10 +295,10 @@ protected function getLeadingExplicitAttributePath(string $attributeKey): string
295295
*
296296
* Used to extract a sub-section of the data for faster iteration.
297297
*
298-
* @param string $attributeKey
298+
* @param string|null $attributeKey
299299
* @return array
300300
*/
301-
protected function extractDataFromPath(string $attributeKey): array
301+
protected function extractDataFromPath($attributeKey): array
302302
{
303303
$results = [];
304304

tests/ValidatorTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use DateTime;
66
use PHPUnit\Framework\TestCase;
7+
use Rakit\Validation\Rule;
78
use Rakit\Validation\Rules\UploadedFile;
89
use Rakit\Validation\Validator;
910

1011
class ValidatorTest extends TestCase
1112
{
12-
13+
/** @var Validator */
1314
protected $validator;
1415

1516
protected function setUp()
@@ -812,6 +813,71 @@ public function testEmptyArrayAssocValidation()
812813

813814
$this->assertFalse($validation->passes());
814815
}
816+
817+
/**
818+
* Test root asterisk validation.
819+
*
820+
* @dataProvider rootAsteriskProvider
821+
*/
822+
public function testRootAsteriskValidation(array $data, array $rules, $errors = null)
823+
{
824+
$validation = $this->validator->validate($data, $rules);
825+
$this->assertSame(empty($errors), $validation->passes());
826+
$errorBag = $validation->errors();
827+
if (!empty($errors)) {
828+
foreach ($errors as $error) {
829+
$field = $error[0];
830+
$rule = $error[1] ?? null;
831+
$error = $errorBag->get($field);
832+
$this->assertNotEmpty($error);
833+
if ($rule !== null) {
834+
$this->assertArrayHasKey($rule, $error);
835+
}
836+
}
837+
}
838+
}
839+
840+
public function rootAsteriskProvider(): array
841+
{
842+
return [
843+
'control sample success' => [
844+
['Body' => ['a' => 1, 'b' => 2]],
845+
['Body.*' => 'integer|min:0'],
846+
],
847+
'control sample failure' => [
848+
['Body' => ['a' => 1, 'b' => -2]],
849+
['Body.*' => 'integer|min:0'],
850+
[['Body.b', 'min']],
851+
],
852+
'root field success' => [
853+
['a' => 1, 'b' => 2],
854+
['*' => 'integer|min:0'],
855+
],
856+
'root field failure' => [
857+
['a' => 1, 'b' => -2],
858+
['*' => 'integer|min:0'],
859+
[['b', 'min']],
860+
],
861+
'root array success' => [
862+
[[1], [2]],
863+
['*.*' => 'integer|min:0'],
864+
],
865+
'root array failure' => [
866+
[[1], [-2]],
867+
['*.*' => 'integer|min:0'],
868+
[['1.0', 'min']],
869+
],
870+
'root dict success' => [
871+
['a' => ['c' => 1, 'd' => 4], 'b' => ['c' => 'e', 'd' => 8]],
872+
['*.c' => 'required'],
873+
],
874+
'root dict failure' => [
875+
['a' => ['c' => 1, 'd' => 4], 'b' => ['d' => 8]],
876+
['*.c' => 'required'],
877+
[['b.c', 'required']],
878+
],
879+
];
880+
}
815881

816882
public function testArrayValidation()
817883
{

0 commit comments

Comments
 (0)