From b1d639af9e32444a68a2276afa05b5c45790bb32 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 | 27 ++++++++++++++--------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/Param/ParamValueConverterRegistry.php b/src/Param/ParamValueConverterRegistry.php index ecf40a5..190629f 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; @@ -33,7 +32,7 @@ * @phpstan-type Converter Closure(mixed, Type|string|null, bool):(StreamInterface|string) * @phpstan-type ConverterRegistry array */ -final readonly class ParamValueConverterRegistry +final class ParamValueConverterRegistry { private const CaseInsensitiveTypes = [ 'bool', @@ -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(),