Skip to content

Commit 5c15326

Browse files
committed
Added possibility to specify an array of composite type
(in `DefaultTypeConverterFactory::getConverterForTypeSpecification()`)
1 parent 7dab906 commit 5c15326

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

Changelog.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Added
6+
7+
* `converters\DefaultTypeConverterFactory::getConverterForTypeSpecification()` accepts another specification
8+
for an array type: `['' => type]` where `type` may be either a type name or another array for
9+
an unnamed composite type. Empty string is used as a key here as column name / alias cannot legitimately
10+
be an empty string in Postgres.
11+
12+
There was no way previously to specify an "array of composite" other than creating all `TypeConverter` instances
13+
manually.
14+
315
## [3.1.0] - 2025-05-27
416

517
### Added
@@ -410,3 +422,4 @@ Initial release on GitHub
410422
[3.0.0-beta.2]: https://github.com/sad-spirit/pg-wrapper/compare/v3.0.0-beta...v3.0.0-beta.2
411423
[3.0.0]: https://github.com/sad-spirit/pg-wrapper/compare/v3.0.0-beta.2...v3.0.0
412424
[3.1.0]: https://github.com/sad-spirit/pg-wrapper/compare/v3.0.0...v3.1.0
425+
[Unreleased]: https://github.com/sad-spirit/pg-wrapper/compare/v3.1.0...HEAD

src/sad_spirit/pg_wrapper/converters/DefaultTypeConverterFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
exceptions\TypeConversionException,
2323
types
2424
};
25+
use sad_spirit\pg_wrapper\converters\containers\ArrayConverter;
2526

2627
/**
2728
* Creates type converters for database type based on specific DB metadata
@@ -255,6 +256,8 @@ private function updateConnection(TypeConverter $converter): void
255256
* - type name (string), either simple or schema-qualified,
256257
* 'foo[]' is treated as an array of base type 'foo'
257258
* - ['field' => 'type', ...] for composite types
259+
* - ['' => type] is an alternate way to specify array of given type, which may be also
260+
* an array specifying composite type
258261
* - TypeConverter instance. If it implements ConnectionAware, then
259262
* it will receive current Connection
260263
*
@@ -276,6 +279,11 @@ public function getConverterForTypeSpecification(mixed $type): TypeConverter
276279
return $this->getConverterForTypeName($type);
277280

278281
} elseif (\is_array($type)) {
282+
// alternate type specification for arrays, added in 3.2
283+
if (1 === \count($type) && [''] === \array_keys($type)) {
284+
return new ArrayConverter($this->getConverterForTypeSpecification(\reset($type)));
285+
}
286+
279287
// type specification for composite type
280288
return new containers\CompositeConverter(\array_map(
281289
$this->getConverterForTypeSpecification(...),

tests/DefaultTypeConverterFactoryTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public function testGetConverterForSqlStandardTypeArray(string $typeName, TypeCo
9999
$this->factory->getConverterForTypeSpecification($typeName . '[]')
100100
);
101101
}
102+
public function testGetConverterForArrayTypeAlternate(): void
103+
{
104+
$this->assertEquals(
105+
new ArrayConverter(new IntegerConverter()),
106+
$this->factory->getConverterForTypeSpecification(['' => 'integer'])
107+
);
108+
}
102109

103110
#[DataProvider('getInvalidTypeNames')]
104111
public function testInvalidTypeNames(string $typeName, string $exceptionMessage): void
@@ -127,6 +134,26 @@ public function testGetConverterForCompositeTypeUsingArray(): void
127134
);
128135
}
129136

137+
public function testGetConverterForArrayOfCompositeType(): void
138+
{
139+
$this->assertEquals(
140+
new ArrayConverter(new CompositeConverter([
141+
'num' => new IntegerConverter(),
142+
'string' => new StringConverter(),
143+
'strings' => new ArrayConverter(new StringConverter()),
144+
'coord' => new PointConverter()
145+
])),
146+
$this->factory->getConverterForTypeSpecification([
147+
'' => [
148+
'num' => 'integer',
149+
'string' => '"varchar"',
150+
'strings' => 'text[]',
151+
'coord' => 'pg_catalog.point'
152+
]
153+
])
154+
);
155+
}
156+
130157
public function testMissingConverter(): void
131158
{
132159
$this::expectException(RuntimeException::class);

0 commit comments

Comments
 (0)