diff --git a/src/Laravel/Rules/ParentField.php b/src/Laravel/Rules/ParentField.php index 763bfa6..233a111 100644 --- a/src/Laravel/Rules/ParentField.php +++ b/src/Laravel/Rules/ParentField.php @@ -8,8 +8,9 @@ /** * Finds the value of a parent field (`country`) for the attribute being validated. - * Wildcards pair by position: attribute `addresses.2.city` with parent - * `addresses.*.country` reads `addresses.2.country`. + * Each wildcard takes the attribute's key at the same depth: attribute + * `items.0.addresses.2.city` with parent `items.0.addresses.*.country` reads + * `items.0.addresses.2.country`. */ final class ParentField { @@ -21,13 +22,14 @@ public static function value(string $attribute, ?string $field, array $data): mi if ($field === null || $field === '') { return null; } - if (str_contains($field, '*')) { - preg_match_all('/\.(\d+)(?=\.|$)/', '.'.$attribute, $matches); - foreach ($matches[1] as $index) { - $field = (string) preg_replace('/\*/', $index, $field, 1); + $keys = explode('.', $attribute); + $segments = explode('.', $field); + foreach ($segments as $depth => $segment) { + if ($segment === '*' && isset($keys[$depth])) { + $segments[$depth] = $keys[$depth]; } } - return Arr::get($data, $field); + return Arr::get($data, implode('.', $segments)); } } diff --git a/tests/Laravel/RulesTest.php b/tests/Laravel/RulesTest.php index bb61257..77d0735 100644 --- a/tests/Laravel/RulesTest.php +++ b/tests/Laravel/RulesTest.php @@ -6,6 +6,7 @@ use Farzai\Geonames\Laravel\Rules\GeonamesCity; use Farzai\Geonames\Laravel\Rules\GeonamesCountry; use Farzai\Geonames\Laravel\Rules\GeonamesPostalCode; +use Farzai\Geonames\Laravel\Rules\ParentField; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Facades\Validator; @@ -52,6 +53,25 @@ function validate(array $data, array $rules): array expect(array_keys($errors))->toBe(['addresses.2.admin1']); }); +it('pairs each wildcard with the index at the same depth in nested arrays', function () { + $this->fetch(); + + $data = ['items' => [ + ['country' => 'US', 'addresses' => [['country' => 'TH', 'admin1' => '50'], ['country' => 'US', 'admin1' => 'CA']]], + ['country' => 'TH', 'addresses' => [['country' => 'US', 'admin1' => 'CA'], ['country' => 'TH', 'admin1' => 'CA']]], + ]]; + + expect(array_keys(validate($data, ['items.1.addresses.*.admin1' => 'geonames_admin1:items.1.addresses.*.country']))) + ->toBe(['items.1.addresses.1.admin1']) + ->and(array_keys(validate($data, ['items.*.addresses.*.admin1' => 'geonames_admin1:items.*.addresses.*.country']))) + ->toBe(['items.1.addresses.1.admin1']) + ->and(array_keys(validate($data, ['items.*.addresses.*.admin1' => 'geonames_admin1:items.*.country']))) + ->toBe(['items.0.addresses.0.admin1', 'items.1.addresses.0.admin1', 'items.1.addresses.1.admin1']) + ->and(ParentField::value('items', 'items.*.country', $data))->toBeNull() + ->and(ParentField::value('items', null, $data))->toBeNull() + ->and(ParentField::value('items', '', $data))->toBeNull(); +}); + it('says the data is not downloaded instead of throwing out of validation', function () { // Countries are bundled, admin1 is not: nothing was fetched. $errors = validate(['c' => 'TH', 'a' => '50'], ['c' => 'geonames_country', 'a' => 'geonames_admin1:c']);