Skip to content

Commit 02f4207

Browse files
further edge case handlings
1 parent f8b99a4 commit 02f4207

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/MartinGeorgiev/Utils/DataStructure.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ public static function transformPostgresTextArrayToPHPArray(string $postgresArra
3939

4040
$jsonArray = '['.\trim($trimmed, '{}').']';
4141

42+
/** @var array<int, mixed>|null $decoded */
4243
$decoded = \json_decode($jsonArray, true, 512, JSON_BIGINT_AS_STRING);
4344
if ($decoded === null && \json_last_error() !== JSON_ERROR_NONE) {
4445
throw new \InvalidArgumentException('Invalid array format: '.\json_last_error_msg());
4546
}
4647

4748
return \array_map(
48-
static fn ($value): mixed => \is_string($value) ? self::unescapeString($value) : $value,
49-
$decoded
49+
static fn (mixed $value): mixed => \is_string($value) ? self::unescapeString($value) : $value,
50+
(array) $decoded
5051
);
5152
}
5253

@@ -64,7 +65,11 @@ public static function transformPHPArrayToPostgresTextArray(array $phpArray): st
6465
throw new \InvalidArgumentException('Only single-dimensioned arrays are supported');
6566
}
6667

67-
$processed = \array_map(static fn ($value): string => self::formatValue($value), $phpArray);
68+
/** @var array<int|string, string> */
69+
$processed = \array_map(
70+
static fn (mixed $value): string => self::formatValue($value),
71+
$phpArray
72+
);
6873

6974
return '{'.\implode(',', $processed).'}';
7075
}
@@ -84,8 +89,29 @@ private static function formatValue(mixed $value): string
8489
return (string) $value;
8590
}
8691

87-
// Convert to string if not already
88-
$stringValue = (string) $value;
92+
// Handle booleans
93+
if (\is_bool($value)) {
94+
return $value ? 'true' : 'false';
95+
}
96+
97+
// Handle objects that implement __toString()
98+
if (\is_object($value)) {
99+
if (\method_exists($value, '__toString')) {
100+
$stringValue = $value->__toString();
101+
} else {
102+
// For objects without __toString, use a default representation
103+
$stringValue = $value::class;
104+
}
105+
} else {
106+
// For all other types, force string conversion
107+
// This covers strings, resources, and other types
108+
$stringValue = match (true) {
109+
\is_resource($value) => '(resource)',
110+
default => (string) $value // @phpstan-ignore-line
111+
};
112+
}
113+
114+
\assert(\is_string($stringValue));
89115

90116
// Handle empty string
91117
if ($stringValue === '') {

tests/MartinGeorgiev/Utils/DataStructureTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ public function can_transform_to_php_value(array $phpValue, string $postgresValu
3434
}
3535

3636
/**
37-
* @see https://stackoverflow.com/a/27964420/3425372 Kudos to dmikam for the inspiration
38-
*
39-
* @return list<array{
40-
* phpValue: array,
41-
* postgresValue: string
42-
* }>
37+
* @return array<string, array{phpValue: array, postgresValue: string}>
4338
*/
4439
public static function provideValidTransformations(): array
4540
{

0 commit comments

Comments
 (0)