Skip to content

Commit b1d639a

Browse files
committed
refactor(param-value-converter-registry): wider date converters input type and throw exception
1 parent cbbc2d7 commit b1d639a

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/Param/ParamValueConverterRegistry.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
use Closure;
88
use DateTimeInterface;
9-
use DateTimeZone;
109
use Psr\Http\Message\StreamInterface;
1110
use SimPod\ClickHouseClient\Exception\UnsupportedParamType;
12-
use SimPod\ClickHouseClient\Sql\Escaper;
1311
use SimPod\ClickHouseClient\Exception\UnsupportedParamValue;
12+
use SimPod\ClickHouseClient\Sql\Escaper;
1413
use SimPod\ClickHouseClient\Sql\Type;
1514

1615
use function array_keys;
@@ -33,7 +32,7 @@
3332
* @phpstan-type Converter Closure(mixed, Type|string|null, bool):(StreamInterface|string)
3433
* @phpstan-type ConverterRegistry array<string, Converter>
3534
*/
36-
final readonly class ParamValueConverterRegistry
35+
final class ParamValueConverterRegistry
3736
{
3837
private const CaseInsensitiveTypes = [
3938
'bool',
@@ -93,13 +92,21 @@ public function __construct(array $registry = [])
9392

9493
'bool' => static fn (bool $value) => $value,
9594

96-
'date' => self::dateConverter($this->clickHouseTimeZone),
97-
'date32' => self::dateConverter($this->clickHouseTimeZone),
98-
'datetime' => self::dateTimeConverter($this->clickHouseTimeZone),
99-
'datetime32' => self::dateTimeConverter($this->clickHouseTimeZone),
100-
'datetime64' => static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
101-
? $value->format('U.u')
102-
: $value,
95+
'date' => self::dateConverter(),
96+
'date32' => self::dateConverter(),
97+
'datetime' => self::dateTimeConverter(),
98+
'datetime32' => self::dateTimeConverter(),
99+
'datetime64' => static function (mixed $value) {
100+
if ($value instanceof DateTimeInterface) {
101+
return $value->format('U.u');
102+
}
103+
104+
if (is_string($value) || is_float($value) || is_int($value)) {
105+
return $value;
106+
}
107+
108+
throw UnsupportedParamValue::type($value);
109+
},
103110

104111
'Dynamic' => self::noopConverter(),
105112
'Variant' => self::noopConverter(),

0 commit comments

Comments
 (0)