Skip to content

Commit 8649aca

Browse files
update var_object to be a filter similar to other types
1 parent fd74c74 commit 8649aca

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

src/Database/Database.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class Database
6868
self::VAR_POLYGON
6969
];
7070

71+
// All types which requires filters
72+
public const ATTRIBUTE_FILTER_TYPES = [
73+
...self::SPATIAL_TYPES,
74+
self::VAR_VECTOR,
75+
self::VAR_OBJECT,
76+
];
77+
7178
// Index Types
7279
public const INDEX_KEY = 'key';
7380
public const INDEX_FULLTEXT = 'fulltext';
@@ -622,6 +629,35 @@ function (?string $value) {
622629
return is_array($decoded) ? $decoded : $value;
623630
}
624631
);
632+
633+
self::addFilter(
634+
Database::VAR_OBJECT,
635+
/**
636+
* @param mixed $value
637+
* @return mixed
638+
*/
639+
function (mixed $value) {
640+
if (!\is_array($value)) {
641+
return $value;
642+
}
643+
644+
return \json_encode($value);
645+
},
646+
/**
647+
* @param string|null $value
648+
* @return array|null
649+
*/
650+
function (?string $value) {
651+
if (is_null($value)) {
652+
return null;
653+
}
654+
if (!is_string($value)) {
655+
return $value;
656+
}
657+
$decoded = json_decode($value, true);
658+
return is_array($decoded) ? $decoded : $value;
659+
}
660+
);
625661
}
626662

627663
/**
@@ -1438,7 +1474,7 @@ public function delete(?string $database = null): bool
14381474
public function createCollection(string $id, array $attributes = [], array $indexes = [], ?array $permissions = null, bool $documentSecurity = true): Document
14391475
{
14401476
foreach ($attributes as &$attribute) {
1441-
if (in_array($attribute['type'], Database::SPATIAL_TYPES) || $attribute['type'] === Database::VAR_VECTOR) {
1477+
if (in_array($attribute['type'], self::ATTRIBUTE_FILTER_TYPES)) {
14421478
$existingFilters = $attribute['filters'] ?? [];
14431479
if (!is_array($existingFilters)) {
14441480
$existingFilters = [$existingFilters];
@@ -1811,11 +1847,8 @@ public function createAttribute(string $collection, string $id, string $type, in
18111847
if ($collection->isEmpty()) {
18121848
throw new NotFoundException('Collection not found');
18131849
}
1814-
if (in_array($type, Database::SPATIAL_TYPES)) {
1815-
$filters[] = $type;
1816-
$filters = array_unique($filters);
1817-
}
1818-
if ($type === Database::VAR_VECTOR) {
1850+
1851+
if (in_array($type, self::ATTRIBUTE_FILTER_TYPES)) {
18191852
$filters[] = $type;
18201853
$filters = array_unique($filters);
18211854
}
@@ -2138,6 +2171,9 @@ private function validateAttribute(
21382171
if ($this->adapter->getSupportForSpatialAttributes()) {
21392172
\array_push($supportedTypes, ...self::SPATIAL_TYPES);
21402173
}
2174+
if ($this->adapter->getSupportForObject()) {
2175+
$supportedTypes[] = self::VAR_OBJECT;
2176+
}
21412177
throw new DatabaseException('Unknown attribute type: ' . $type . '. Must be one of ' . implode(', ', $supportedTypes));
21422178
}
21432179

@@ -7729,12 +7765,6 @@ public function casting(Document $collection, Document $document): Document
77297765
case self::VAR_FLOAT:
77307766
$node = (float)$node;
77317767
break;
7732-
case self::VAR_OBJECT:
7733-
// Decode JSONB string to array
7734-
if (is_string($node)) {
7735-
$node = json_decode($node, true);
7736-
}
7737-
break;
77387768
default:
77397769
break;
77407770
}

src/Database/Validator/ObjectValidator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ public function getDescription(): string
2121
*/
2222
public function isValid(mixed $value): bool
2323
{
24-
return empty($value) || is_array($value) && !array_is_list($value);
24+
if (is_string($value)) {
25+
// Check if it's valid JSON
26+
json_decode($value);
27+
return json_last_error() === JSON_ERROR_NONE;
28+
}
29+
30+
// Allow empty or associative arrays (non-list)
31+
return empty($value) || (is_array($value) && !array_is_list($value));
2532
}
2633

2734
/**

0 commit comments

Comments
 (0)