From f9c2a8aef05f6ea984b1af08a61b4d961d406e41 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 4 Mar 2026 20:19:36 +1300 Subject: [PATCH] Fix type checks --- src/Database/Database.php | 13 +++++++++++-- src/Database/Validator/Operator.php | 6 +++--- src/Database/Validator/Query/Filter.php | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index e97908c7b..6fa6797da 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -69,6 +69,15 @@ class Database public const VAR_LINESTRING = 'linestring'; public const VAR_POLYGON = 'polygon'; + // All string types + public const STRING_TYPES = [ + self::VAR_STRING, + self::VAR_VARCHAR, + self::VAR_TEXT, + self::VAR_MEDIUMTEXT, + self::VAR_LONGTEXT, + ]; + // All spatial types public const SPATIAL_TYPES = [ self::VAR_POINT, @@ -1683,7 +1692,7 @@ public function createCollection(string $id, array $attributes = [], array $inde /** * mysql does not save length in collection when length = attributes size */ - if ($collectionAttribute->getAttribute('type') === Database::VAR_STRING) { + if (in_array($collectionAttribute->getAttribute('type'), self::STRING_TYPES)) { if (!empty($lengths[$i]) && $lengths[$i] === $collectionAttribute->getAttribute('size') && $this->adapter->getMaxIndexLength() > 0) { $lengths[$i] = null; } @@ -4478,7 +4487,7 @@ public function createIndex(string $collection, string $id, string $type, array /** * mysql does not save length in collection when length = attributes size */ - if ($attributeType === self::VAR_STRING) { + if (in_array($attributeType, self::STRING_TYPES)) { if (!empty($lengths[$i]) && $lengths[$i] === $collectionAttribute->getAttribute('size') && $this->adapter->getMaxIndexLength() > 0) { $lengths[$i] = null; } diff --git a/src/Database/Validator/Operator.php b/src/Database/Validator/Operator.php index 842a4861e..46f1b8db3 100644 --- a/src/Database/Validator/Operator.php +++ b/src/Database/Validator/Operator.php @@ -402,7 +402,7 @@ private function validateOperatorForAttribute( break; case DatabaseOperator::TYPE_STRING_CONCAT: - if ($type !== Database::VAR_STRING || $isArray) { + if (!in_array($type, Database::STRING_TYPES) || $isArray) { $this->message = "Cannot apply {$method} operator to non-string field '{$operator->getAttribute()}'"; return false; } @@ -412,7 +412,7 @@ private function validateOperatorForAttribute( return false; } - if ($this->currentDocument !== null && $type === Database::VAR_STRING) { + if ($this->currentDocument !== null && in_array($type, Database::STRING_TYPES)) { $currentString = $this->currentDocument->getAttribute($operator->getAttribute()) ?? ''; $concatValue = $values[0]; $predictedLength = strlen($currentString) + strlen($concatValue); @@ -430,7 +430,7 @@ private function validateOperatorForAttribute( break; case DatabaseOperator::TYPE_STRING_REPLACE: // Replace only works on string types - if ($type !== Database::VAR_STRING) { + if (!in_array($type, Database::STRING_TYPES)) { $this->message = "Cannot apply {$method} operator to non-string field '{$operator->getAttribute()}'"; return false; } diff --git a/src/Database/Validator/Query/Filter.php b/src/Database/Validator/Query/Filter.php index 71b6b74f2..dd07e44c8 100644 --- a/src/Database/Validator/Query/Filter.php +++ b/src/Database/Validator/Query/Filter.php @@ -267,7 +267,7 @@ protected function isValidAttributeAndValues(string $attribute, array $values, s if ( !$array && in_array($method, [Query::TYPE_CONTAINS, Query::TYPE_CONTAINS_ANY, Query::TYPE_CONTAINS_ALL, Query::TYPE_NOT_CONTAINS]) && - $attributeSchema['type'] !== Database::VAR_STRING && + !in_array($attributeSchema['type'], Database::STRING_TYPES) && $attributeSchema['type'] !== Database::VAR_OBJECT && !in_array($attributeSchema['type'], Database::SPATIAL_TYPES) ) {