Skip to content

Commit f2f5ee3

Browse files
added default handling for the spatial types
1 parent 5d21db2 commit f2f5ee3

2 files changed

Lines changed: 109 additions & 2 deletions

File tree

src/Database/Database.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,8 +1909,11 @@ protected function validateDefaultTypes(string $type, mixed $default): void
19091909
}
19101910

19111911
if ($defaultType === 'array') {
1912-
foreach ($default as $value) {
1913-
$this->validateDefaultTypes($type, $value);
1912+
// spatial types require the array itself
1913+
if (!in_array($type, Database::SPATIAL_TYPES)) {
1914+
foreach ($default as $value) {
1915+
$this->validateDefaultTypes($type, $value);
1916+
}
19141917
}
19151918
return;
19161919
}

tests/e2e/Adapter/Scopes/SpatialTests.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,4 +1865,108 @@ public function testUpdateSpatialAttributes(): void
18651865
$database->deleteCollection($collectionName);
18661866
}
18671867
}
1868+
1869+
public function testSpatialAttributeDefaults(): void
1870+
{
1871+
/** @var Database $database */
1872+
$database = static::getDatabase();
1873+
if (!$database->getAdapter()->getSupportForSpatialAttributes()) {
1874+
$this->markTestSkipped('Adapter does not support spatial attributes');
1875+
}
1876+
1877+
$collectionName = 'spatial_defaults_';
1878+
try {
1879+
$database->createCollection($collectionName);
1880+
1881+
// Create spatial attributes with defaults and no indexes to avoid nullability/index constraints
1882+
$this->assertEquals(true, $database->createAttribute($collectionName, 'pt', Database::VAR_POINT, 0, false, [1.0, 2.0]));
1883+
$this->assertEquals(true, $database->createAttribute($collectionName, 'ln', Database::VAR_LINESTRING, 0, false, [[0.0, 0.0], [1.0, 1.0]]));
1884+
$this->assertEquals(true, $database->createAttribute($collectionName, 'pg', Database::VAR_POLYGON, 0, false, [[[0.0, 0.0], [0.0, 2.0], [2.0, 2.0], [0.0, 0.0]]]));
1885+
1886+
// Create non-spatial attributes (mix of defaults and no defaults)
1887+
$this->assertEquals(true, $database->createAttribute($collectionName, 'title', Database::VAR_STRING, 255, false, 'Untitled'));
1888+
$this->assertEquals(true, $database->createAttribute($collectionName, 'count', Database::VAR_INTEGER, 0, false, 0));
1889+
$this->assertEquals(true, $database->createAttribute($collectionName, 'rating', Database::VAR_FLOAT, 0, false)); // no default
1890+
$this->assertEquals(true, $database->createAttribute($collectionName, 'active', Database::VAR_BOOLEAN, 0, false, true));
1891+
1892+
// Create document without providing spatial values, expect defaults applied
1893+
$doc = $database->createDocument($collectionName, new Document([
1894+
'$id' => ID::custom('d1'),
1895+
'$permissions' => [Permission::read(Role::any())]
1896+
]));
1897+
$this->assertInstanceOf(Document::class, $doc);
1898+
$this->assertEquals([1.0, 2.0], $doc->getAttribute('pt'));
1899+
$this->assertEquals([[0.0, 0.0], [1.0, 1.0]], $doc->getAttribute('ln'));
1900+
$this->assertEquals([[[0.0, 0.0], [0.0, 2.0], [2.0, 2.0], [0.0, 0.0]]], $doc->getAttribute('pg'));
1901+
// Non-spatial defaults
1902+
$this->assertEquals('Untitled', $doc->getAttribute('title'));
1903+
$this->assertEquals(0, $doc->getAttribute('count'));
1904+
$this->assertNull($doc->getAttribute('rating'));
1905+
$this->assertTrue($doc->getAttribute('active'));
1906+
1907+
// Create document overriding defaults
1908+
$doc2 = $database->createDocument($collectionName, new Document([
1909+
'$id' => ID::custom('d2'),
1910+
'$permissions' => [Permission::read(Role::any())],
1911+
'pt' => [9.0, 9.0],
1912+
'ln' => [[2.0, 2.0], [3.0, 3.0]],
1913+
'pg' => [[[1.0, 1.0], [1.0, 3.0], [3.0, 3.0], [1.0, 1.0]]],
1914+
'title' => 'Custom',
1915+
'count' => 5,
1916+
'rating' => 4.5,
1917+
'active' => false
1918+
]));
1919+
$this->assertInstanceOf(Document::class, $doc2);
1920+
$this->assertEquals([9.0, 9.0], $doc2->getAttribute('pt'));
1921+
$this->assertEquals([[2.0, 2.0], [3.0, 3.0]], $doc2->getAttribute('ln'));
1922+
$this->assertEquals([[[1.0, 1.0], [1.0, 3.0], [3.0, 3.0], [1.0, 1.0]]], $doc2->getAttribute('pg'));
1923+
$this->assertEquals('Custom', $doc2->getAttribute('title'));
1924+
$this->assertEquals(5, $doc2->getAttribute('count'));
1925+
$this->assertEquals(4.5, $doc2->getAttribute('rating'));
1926+
$this->assertFalse($doc2->getAttribute('active'));
1927+
1928+
// Update defaults and ensure they are applied for new documents
1929+
$database->updateAttributeDefault($collectionName, 'pt', [5.0, 6.0]);
1930+
$database->updateAttributeDefault($collectionName, 'ln', [[10.0, 10.0], [20.0, 20.0]]);
1931+
$database->updateAttributeDefault($collectionName, 'pg', [[[5.0, 5.0], [5.0, 7.0], [7.0, 7.0], [5.0, 5.0]]]);
1932+
$database->updateAttributeDefault($collectionName, 'title', 'Updated');
1933+
$database->updateAttributeDefault($collectionName, 'count', 10);
1934+
$database->updateAttributeDefault($collectionName, 'active', false);
1935+
1936+
$doc3 = $database->createDocument($collectionName, new Document([
1937+
'$id' => ID::custom('d3'),
1938+
'$permissions' => [Permission::read(Role::any())]
1939+
]));
1940+
$this->assertInstanceOf(Document::class, $doc3);
1941+
$this->assertEquals([5.0, 6.0], $doc3->getAttribute('pt'));
1942+
$this->assertEquals([[10.0, 10.0], [20.0, 20.0]], $doc3->getAttribute('ln'));
1943+
$this->assertEquals([[[5.0, 5.0], [5.0, 7.0], [7.0, 7.0], [5.0, 5.0]]], $doc3->getAttribute('pg'));
1944+
$this->assertEquals('Updated', $doc3->getAttribute('title'));
1945+
$this->assertEquals(10, $doc3->getAttribute('count'));
1946+
$this->assertNull($doc3->getAttribute('rating'));
1947+
$this->assertFalse($doc3->getAttribute('active'));
1948+
1949+
// Invalid defaults should raise errors
1950+
try {
1951+
$database->updateAttributeDefault($collectionName, 'pt', [[1.0, 2.0]]); // wrong dimensionality
1952+
$this->fail('Expected exception for invalid point default shape');
1953+
} catch (\Throwable $e) {
1954+
$this->assertTrue(true);
1955+
}
1956+
try {
1957+
$database->updateAttributeDefault($collectionName, 'ln', [1.0, 2.0]); // wrong dimensionality
1958+
$this->fail('Expected exception for invalid linestring default shape');
1959+
} catch (\Throwable $e) {
1960+
$this->assertTrue(true);
1961+
}
1962+
try {
1963+
$database->updateAttributeDefault($collectionName, 'pg', [[1.0, 2.0]]); // wrong dimensionality
1964+
$this->fail('Expected exception for invalid polygon default shape');
1965+
} catch (\Throwable $e) {
1966+
$this->assertTrue(true);
1967+
}
1968+
} finally {
1969+
$database->deleteCollection($collectionName);
1970+
}
1971+
}
18681972
}

0 commit comments

Comments
 (0)