Skip to content

Commit c81fc6d

Browse files
committed
fix(Helpers): check remaining segments per item after wildcard in dotKeyExists()
1 parent 9f8d308 commit c81fc6d

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Release Date: Unreleased
2121
- **Database:** Fixed a bug where `SQLSRV\Builder::_upsertBatch()` concatenated `$table` unescaped into `Object_ID()`, causing syntax errors for table names with quotes or special characters.
2222
- **Database:** Fixed a bug where `SQLSRV\Forge::_alterTable()` interpolated table and column names without escaping in default constraint lookups.
2323
- **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters.
24-
- **Helpers:** Fixed a bug in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination and returned `true`, and non-array intermediate values threw a `TypeError`.
24+
- **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements.
2525
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
2626
- **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names.
2727
- **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format.

‎system/Helpers/Array/ArrayHelper.php‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,33 +143,35 @@ public static function dotKeyExists(string $index, array $array): bool
143143
return false;
144144
}
145145

146-
$currentArray = $array;
146+
return self::checkDotKeyExists($indexes, $array);
147+
}
147148

149+
/**
150+
* Recursively check if the key exists in the array with wildcards.
151+
*
152+
* @used-by dotKeyExists()
153+
*
154+
* @param list<string> $indexes
155+
* @param array<array-key, mixed> $currentArray
156+
*/
157+
private static function checkDotKeyExists(array $indexes, array $currentArray): bool
158+
{
148159
// Grab the current index
149160
while ($indexes !== []) {
150161
$currentIndex = array_shift($indexes);
151162

152163
if ($currentIndex === '*') {
153-
$currentIndex = array_shift($indexes);
154-
155-
if (! is_array($currentArray)) {
164+
if (! is_array($currentArray) || $currentArray === []) {
156165
return false;
157166
}
158167

159168
foreach ($currentArray as $item) {
160-
if (! is_array($item) || ! array_key_exists($currentIndex, $item)) {
169+
if (! is_array($item) || ! self::checkDotKeyExists($indexes, $item)) {
161170
return false;
162171
}
163172
}
164173

165-
// If indexes is empty, all elements are checked.
166-
if ($indexes === []) {
167-
return true;
168-
}
169-
170-
$currentArray = self::dotSearch('*.' . $currentIndex, $currentArray);
171-
172-
continue;
174+
return true;
173175
}
174176

175177
if (! is_array($currentArray) || ! array_key_exists($currentIndex, $currentArray)) {

‎tests/system/Helpers/Array/ArrayHelperDotKeyExistsTest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,31 @@ public function testDotKeyExistsWithWildCard(): void
8383
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.*.age.sub', $this->array));
8484
$this->assertFalse(ArrayHelper::dotKeyExists('contacts.friends.1.age.*.sub', $this->array));
8585
$this->assertFalse(ArrayHelper::dotKeyExists('*.name', [['name' => 'Fred'], 'not-an-array']));
86+
$this->assertFalse(ArrayHelper::dotKeyExists('*.name', []));
87+
}
88+
89+
public function testDotKeyExistsWithZeroIndexAfterWildCard(): void
90+
{
91+
$array = [
92+
'users' => [
93+
['contacts' => [['email' => 'alice@example.com']]],
94+
['contacts' => [['email' => 'bob@example.com']]],
95+
],
96+
];
97+
98+
$this->assertTrue(ArrayHelper::dotKeyExists('users.*.contacts.0.email', $array));
99+
$this->assertTrue(ArrayHelper::dotKeyExists('users.*.contacts.*.email', $array));
100+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.0.nonexistent', $array));
101+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.1.email', $array));
102+
103+
$arrayWithMissingKey = [
104+
'users' => [
105+
['contacts' => [['email' => 'alice@example.com']]],
106+
['contacts' => [['name' => 'Bob']]],
107+
],
108+
];
109+
110+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.0.email', $arrayWithMissingKey));
111+
$this->assertFalse(ArrayHelper::dotKeyExists('users.*.contacts.*.email', $arrayWithMissingKey));
86112
}
87113
}

0 commit comments

Comments
 (0)