Skip to content

Commit d52f10e

Browse files
refactor: rename getSupportForIndexObject to getSupportForObjectIndexes and update related logic
1 parent 6d6b975 commit d52f10e

11 files changed

Lines changed: 76 additions & 76 deletions

File tree

src/Database/Adapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ abstract public function getSupportForObject(): bool;
10841084
*
10851085
* @return bool
10861086
*/
1087-
abstract public function getSupportForIndexObject(): bool;
1087+
abstract public function getSupportForObjectIndexes(): bool;
10881088

10891089
/**
10901090
* Does the adapter support null values in spatial indexes?

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ public function getSupportForObject(): bool
21452145
*
21462146
* @return bool
21472147
*/
2148-
public function getSupportForIndexObject(): bool
2148+
public function getSupportForObjectIndexes(): bool
21492149
{
21502150
return false;
21512151
}

src/Database/Adapter/Mongo.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,6 @@ public function castingBefore(Document $collection, Document $document): Documen
13531353
break;
13541354
case Database::VAR_OBJECT:
13551355
$node = json_decode($node);
1356-
$node = $this->convertStdClassToArray($node);
13571356
break;
13581357
default:
13591358
break;
@@ -2555,19 +2554,26 @@ private function flattenWithDotNotation(string $key, mixed $value, string $prefi
25552554
{
25562555
/** @var array<string, mixed> $result */
25572556
$result = [];
2558-
$currentPref = $prefix === '' ? $key : $prefix . '.' . $key;
25592557

2560-
if (\is_array($value) && !\array_is_list($value)) {
2561-
$nextKey = \array_key_first($value);
2562-
if ($nextKey === null) {
2563-
return $result;
2564-
}
2558+
$stack = [];
25652559

2566-
$nextKeyString = (string) $nextKey;
2567-
$result += $this->flattenWithDotNotation($nextKeyString, $value[$nextKey], $currentPref);
2568-
} else {
2569-
// at the leaf node
2570-
$result[$currentPref] = $value;
2560+
$initialKey = $prefix === '' ? $key : $prefix . '.' . $key;
2561+
$stack[] = [$initialKey, $value];
2562+
while (!empty($stack)) {
2563+
[$currentPath, $currentValue] = array_pop($stack);
2564+
if (is_array($currentValue) && !array_is_list($currentValue)) {
2565+
foreach ($currentValue as $nextKey => $nextValue) {
2566+
if ($nextKey === null) {
2567+
continue;
2568+
}
2569+
$nextKey = (string)$nextKey;
2570+
$nextPath = $currentPath === '' ? $nextKey : $currentPath . '.' . $nextKey;
2571+
$stack[] = [$nextPath, $nextValue];
2572+
}
2573+
} else {
2574+
// leaf node
2575+
$result[$currentPath] = $currentValue;
2576+
}
25712577
}
25722578

25732579
return $result;
@@ -2956,7 +2962,7 @@ public function getSupportForObject(): bool
29562962
*
29572963
* @return bool
29582964
*/
2959-
public function getSupportForIndexObject(): bool
2965+
public function getSupportForObjectIndexes(): bool
29602966
{
29612967
return false;
29622968
}

src/Database/Adapter/MySQL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function getSupportForSpatialAxisOrder(): bool
253253
return true;
254254
}
255255

256-
public function getSupportForIndexObject(): bool
256+
public function getSupportForObjectIndexes(): bool
257257
{
258258
return false;
259259
}

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public function getSupportForObject(): bool
605605
return $this->delegate(__FUNCTION__, \func_get_args());
606606
}
607607

608-
public function getSupportForIndexObject(): bool
608+
public function getSupportForObjectIndexes(): bool
609609
{
610610
return $this->delegate(__FUNCTION__, \func_get_args());
611611
}

src/Database/Adapter/Postgres.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,7 @@ public function getSupportForObject(): bool
22292229
*
22302230
* @return bool
22312231
*/
2232-
public function getSupportForIndexObject(): bool
2232+
public function getSupportForObjectIndexes(): bool
22332233
{
22342234
return true;
22352235
}

src/Database/Adapter/SQLite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ public function getSupportForObject(): bool
10181018
*
10191019
* @return bool
10201020
*/
1021-
public function getSupportForIndexObject(): bool
1021+
public function getSupportForObjectIndexes(): bool
10221022
{
10231023
return false;
10241024
}

src/Database/Database.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,12 +1642,7 @@ public function createCollection(string $id, array $attributes = [], array $inde
16421642
$this->adapter->getSupportForAttributes(),
16431643
$this->adapter->getSupportForMultipleFulltextIndexes(),
16441644
$this->adapter->getSupportForIdenticalIndexes(),
1645-
$this->adapter->getSupportForIndexObject(),
1646-
$this->adapter->getSupportForTrigramIndex(),
1647-
$this->adapter->getSupportForSpatialAttributes(),
1648-
$this->adapter->getSupportForIndex(),
1649-
$this->adapter->getSupportForUniqueIndex(),
1650-
$this->adapter->getSupportForFulltextIndex(),
1645+
$this->adapter->getSupportForObjectIndexes(),
16511646
);
16521647
foreach ($indexes as $index) {
16531648
if (!$validator->isValid($index)) {
@@ -2792,7 +2787,7 @@ public function updateAttribute(string $collection, string $id, ?string $type =
27922787
$this->adapter->getSupportForAttributes(),
27932788
$this->adapter->getSupportForMultipleFulltextIndexes(),
27942789
$this->adapter->getSupportForIdenticalIndexes(),
2795-
$this->adapter->getSupportForIndexObject(),
2790+
$this->adapter->getSupportForObjectIndexes(),
27962791
$this->adapter->getSupportForTrigramIndex(),
27972792
$this->adapter->getSupportForSpatialAttributes(),
27982793
$this->adapter->getSupportForIndex(),
@@ -3672,7 +3667,7 @@ public function createIndex(string $collection, string $id, string $type, array
36723667
break;
36733668

36743669
case self::INDEX_OBJECT:
3675-
if (!$this->adapter->getSupportForObject()) {
3670+
if (!$this->adapter->getSupportForObjectIndexes()) {
36763671
throw new DatabaseException('Object indexes are not supported');
36773672
}
36783673
break;
@@ -3733,7 +3728,7 @@ public function createIndex(string $collection, string $id, string $type, array
37333728
$this->adapter->getSupportForAttributes(),
37343729
$this->adapter->getSupportForMultipleFulltextIndexes(),
37353730
$this->adapter->getSupportForIdenticalIndexes(),
3736-
$this->adapter->getSupportForIndexObject(),
3731+
$this->adapter->getSupportForObjectIndexes(),
37373732
$this->adapter->getSupportForTrigramIndex(),
37383733
$this->adapter->getSupportForSpatialAttributes(),
37393734
$this->adapter->getSupportForIndex(),

src/Database/Validator/Query/Filter.php

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s
170170

171171
case Database::VAR_OBJECT:
172172
if (\in_array($method, [Query::TYPE_EQUAL, Query::TYPE_NOT_EQUAL, Query::TYPE_CONTAINS, Query::TYPE_NOT_CONTAINS], true)
173-
&& !$this->isValidObjectQueryValues($values)) {
173+
&& !$this->isValidObjectQueryValues($value)) {
174174
$this->message = 'Invalid object query structure for attribute "' . $attribute . '"';
175175
return false;
176176
}
@@ -302,67 +302,43 @@ protected function isEmpty(array $values): bool
302302
*
303303
* Disallows ambiguous nested structures like:
304304
* ['a' => [1, 'b' => [212]]] // mixed list
305-
* ['role' => ['name' => [...], 'ex' => [...]]] // multiple nested paths
306305
*
307306
* but allows:
308307
* ['a' => [1, 2], 'b' => [212]] // multiple top-level paths
309308
* ['projects' => [[...]]] // list of objects
309+
* ['role' => ['name' => [...], 'ex' => [...]]] // multiple nested paths
310310
*
311-
* @param array<mixed> $values
311+
* @param mixed $values
312312
* @return bool
313313
*/
314-
private function isValidObjectQueryValues(array $values): bool
314+
private function isValidObjectQueryValues(mixed $values): bool
315315
{
316-
$validate = function (mixed $node, int $depth = 0, bool $inDataContext = false) use (&$validate): bool {
317-
if (!\is_array($node)) {
318-
return true;
319-
}
320-
321-
if (\array_is_list($node)) {
322-
// Check if list is mixed (has both assoc arrays and non-assoc items)
323-
$hasAssoc = false;
324-
$hasNonAssoc = false;
325-
326-
foreach ($node as $item) {
327-
if (\is_array($item) && !\array_is_list($item)) {
328-
$hasAssoc = true;
329-
} else {
330-
$hasNonAssoc = true;
331-
}
332-
}
333-
334-
// Mixed lists are invalid
335-
if ($hasAssoc && $hasNonAssoc) {
336-
return false;
337-
}
316+
if (!is_array($values)) {
317+
return true;
318+
}
338319

339-
// If list contains associative arrays, they're data objects
340-
$enterDataContext = $hasAssoc;
320+
$hasInt = false;
321+
$hasString = false;
341322

342-
foreach ($node as $item) {
343-
if (!$validate($item, $depth + 1, $enterDataContext || $inDataContext)) {
344-
return false;
345-
}
346-
}
347-
return true;
323+
foreach (array_keys($values) as $key) {
324+
if (is_int($key)) {
325+
$hasInt = true;
326+
} else {
327+
$hasString = true;
348328
}
329+
}
349330

350-
// Associative array
351-
// If in data context, multiple keys are OK (it's an object)
352-
// If depth > 0 and NOT in data context, only 1 key allowed (navigation)
353-
if (!$inDataContext && $depth > 0 && \count($node) !== 1) {
354-
return false;
355-
}
331+
if ($hasInt && $hasString) {
332+
return false;
333+
}
356334

357-
// Validate all values
358-
foreach ($node as $value) {
359-
if (!$validate($value, $depth + 1, $inDataContext)) {
360-
return false;
361-
}
335+
foreach ($values as $value) {
336+
if (!$this->isValidObjectQueryValues($value)) {
337+
return false;
362338
}
339+
}
363340

364-
return true;
365-
};
341+
return true;
366342

367343
return $validate($values, 0, false);
368344
}

tests/e2e/Adapter/Scopes/ObjectAttributeTests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public function testObjectAttributeGinIndex(): void
580580
/** @var Database $database */
581581
$database = static::getDatabase();
582582

583-
if (!$database->getAdapter()->getSupportForIndexObject()) {
583+
if (!$database->getAdapter()->getSupportForObjectIndexes()) {
584584
$this->markTestSkipped('Adapter does not support object indexes');
585585
}
586586

0 commit comments

Comments
 (0)