From c443273d5258956d44805d852660d836fea8a618 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 30 Jan 2025 11:57:06 +0100 Subject: [PATCH] refactor(param-value-converter-registry): wider date converters input type and throw exception --- src/Param/ParamValueConverterRegistry.php | 25 +++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Param/ParamValueConverterRegistry.php b/src/Param/ParamValueConverterRegistry.php index ecf40a5..0f7969a 100644 --- a/src/Param/ParamValueConverterRegistry.php +++ b/src/Param/ParamValueConverterRegistry.php @@ -6,11 +6,10 @@ use Closure; use DateTimeInterface; -use DateTimeZone; use Psr\Http\Message\StreamInterface; use SimPod\ClickHouseClient\Exception\UnsupportedParamType; -use SimPod\ClickHouseClient\Sql\Escaper; use SimPod\ClickHouseClient\Exception\UnsupportedParamValue; +use SimPod\ClickHouseClient\Sql\Escaper; use SimPod\ClickHouseClient\Sql\Type; use function array_keys; @@ -93,13 +92,21 @@ public function __construct(array $registry = []) 'bool' => static fn (bool $value) => $value, - 'date' => self::dateConverter($this->clickHouseTimeZone), - 'date32' => self::dateConverter($this->clickHouseTimeZone), - 'datetime' => self::dateTimeConverter($this->clickHouseTimeZone), - 'datetime32' => self::dateTimeConverter($this->clickHouseTimeZone), - 'datetime64' => static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface - ? $value->format('U.u') - : $value, + 'date' => self::dateConverter(), + 'date32' => self::dateConverter(), + 'datetime' => self::dateTimeConverter(), + 'datetime32' => self::dateTimeConverter(), + 'datetime64' => static function (mixed $value) { + if ($value instanceof DateTimeInterface) { + return $value->format('U.u'); + } + + if (is_string($value) || is_float($value) || is_int($value)) { + return $value; + } + + throw UnsupportedParamValue::type($value); + }, 'Dynamic' => self::noopConverter(), 'Variant' => self::noopConverter(),