Skip to content

Commit 734173f

Browse files
committed
Disable array index through validator
1 parent 83278d6 commit 734173f

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

src/Database/Database.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,10 +1242,6 @@ public function createCollection(string $id, array $attributes = [], array $inde
12421242

12431243
$isArray = $collectionAttribute->getAttribute('array', false);
12441244
if ($isArray) {
1245-
if (!$this->adapter->getSupportForIndexArray()) {
1246-
throw new IndexException('Indexing an array attribute is not supported');
1247-
}
1248-
12491245
if ($this->adapter->getMaxIndexLength() > 0) {
12501246
$lengths[$i] = self::ARRAY_INDEX_LENGTH;
12511247
}
@@ -1274,7 +1270,8 @@ public function createCollection(string $id, array $attributes = [], array $inde
12741270
$validator = new IndexValidator(
12751271
$attributes,
12761272
$this->adapter->getMaxIndexLength(),
1277-
$this->adapter->getInternalIndexesKeys()
1273+
$this->adapter->getInternalIndexesKeys(),
1274+
$this->adapter->getSupportForIndexArray()
12781275
);
12791276
foreach ($indexes as $index) {
12801277
if (!$validator->isValid($index)) {
@@ -2199,7 +2196,8 @@ public function updateAttribute(string $collection, string $id, ?string $type =
21992196
$validator = new IndexValidator(
22002197
$attributes,
22012198
$this->adapter->getMaxIndexLength(),
2202-
$this->adapter->getInternalIndexesKeys()
2199+
$this->adapter->getInternalIndexesKeys(),
2200+
$this->adapter->getSupportForIndexArray()
22032201
);
22042202

22052203
foreach ($indexes as $index) {
@@ -3079,10 +3077,6 @@ public function createIndex(string $collection, string $id, string $type, array
30793077

30803078
$isArray = $collectionAttribute->getAttribute('array', false);
30813079
if ($isArray) {
3082-
if (!$this->adapter->getSupportForIndexArray()) {
3083-
throw new IndexException('Indexing an array attribute is not supported');
3084-
}
3085-
30863080
if ($this->adapter->getMaxIndexLength() > 0) {
30873081
$lengths[$i] = self::ARRAY_INDEX_LENGTH;
30883082
}
@@ -3108,7 +3102,8 @@ public function createIndex(string $collection, string $id, string $type, array
31083102
$validator = new IndexValidator(
31093103
$collection->getAttribute('attributes', []),
31103104
$this->adapter->getMaxIndexLength(),
3111-
$this->adapter->getInternalIndexesKeys()
3105+
$this->adapter->getInternalIndexesKeys(),
3106+
$this->adapter->getSupportForIndexArray()
31123107
);
31133108
if (!$validator->isValid($index)) {
31143109
throw new IndexException($validator->getDescription());

src/Database/Validator/Index.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ class Index extends Validator
2222
*/
2323
protected array $reservedKeys;
2424

25+
protected bool $arrayIndexSupport;
26+
2527
/**
2628
* @param array<Document> $attributes
2729
* @param int $maxLength
2830
* @param array<string> $reservedKeys
2931
* @throws DatabaseException
3032
*/
31-
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [])
33+
public function __construct(array $attributes, int $maxLength, array $reservedKeys = [], $arrayIndexSupport = false)
3234
{
3335
$this->maxLength = $maxLength;
3436
$this->reservedKeys = $reservedKeys;
37+
$this->arrayIndexSupport = $arrayIndexSupport;
3538

3639
foreach ($attributes as $attribute) {
3740
$key = \strtolower($attribute->getAttribute('key', $attribute->getAttribute('$id')));
@@ -156,6 +159,11 @@ public function checkArrayIndex(Document $index): bool
156159
$this->message = 'Invalid index order "' . $direction . '" on array attribute "'. $attribute->getAttribute('key', '') .'"';
157160
return false;
158161
}
162+
163+
if ($this->arrayIndexSupport === false) {
164+
$this->message = 'Indexing an array attribute is temporarily disabled';
165+
return false;
166+
}
159167
} elseif ($attribute->getAttribute('type') !== Database::VAR_STRING && !empty($lengths[$attributePosition])) {
160168
$this->message = 'Cannot set a length on "'. $attribute->getAttribute('type') . '" attributes';
161169
return false;

0 commit comments

Comments
 (0)