Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/Laravel/Rules/ParentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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));
}
}
20 changes: 20 additions & 0 deletions tests/Laravel/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down
Loading