Skip to content

Commit cbbc2d7

Browse files
committed
feat: support clickhouse timezone input in ParamValueConverterRegistry
1 parent a4d85be commit cbbc2d7

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/Param/ParamValueConverterRegistry.php

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

77
use Closure;
88
use DateTimeInterface;
9+
use DateTimeZone;
910
use Psr\Http\Message\StreamInterface;
1011
use SimPod\ClickHouseClient\Exception\UnsupportedParamType;
1112
use SimPod\ClickHouseClient\Sql\Escaper;
13+
use SimPod\ClickHouseClient\Exception\UnsupportedParamValue;
1214
use SimPod\ClickHouseClient\Sql\Type;
1315

1416
use function array_keys;
@@ -18,6 +20,8 @@
1820
use function implode;
1921
use function in_array;
2022
use function is_array;
23+
use function is_float;
24+
use function is_int;
2125
use function is_string;
2226
use function json_encode;
2327
use function sprintf;
@@ -26,13 +30,12 @@
2630
use function trim;
2731

2832
/**
29-
* @phpstan-type Converter = Closure(mixed, Type|string|null, bool):(StreamInterface|string)
30-
* @phpstan-type ConverterRegistry = array<string, Converter>
33+
* @phpstan-type Converter Closure(mixed, Type|string|null, bool):(StreamInterface|string)
34+
* @phpstan-type ConverterRegistry array<string, Converter>
3135
*/
32-
final class ParamValueConverterRegistry
36+
final readonly class ParamValueConverterRegistry
3337
{
34-
/** @var list<string> */
35-
private static array $caseInsensitiveTypes = [
38+
private const CaseInsensitiveTypes = [
3639
'bool',
3740
'date',
3841
'date32',
@@ -90,10 +93,10 @@ public function __construct(array $registry = [])
9093

9194
'bool' => static fn (bool $value) => $value,
9295

93-
'date' => self::dateConverter(),
94-
'date32' => self::dateConverter(),
95-
'datetime' => self::dateTimeConverter(),
96-
'datetime32' => self::dateTimeConverter(),
96+
'date' => self::dateConverter($this->clickHouseTimeZone),
97+
'date32' => self::dateConverter($this->clickHouseTimeZone),
98+
'datetime' => self::dateTimeConverter($this->clickHouseTimeZone),
99+
'datetime32' => self::dateTimeConverter($this->clickHouseTimeZone),
97100
'datetime64' => static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
98101
? $value->format('U.u')
99102
: $value,
@@ -232,7 +235,7 @@ public function get(Type|string $type): Closure
232235

233236
$typeName = strtolower($typeName);
234237
$converter = $this->registry[$typeName] ?? null;
235-
if ($converter !== null && in_array($typeName, self::$caseInsensitiveTypes, true)) {
238+
if ($converter !== null && in_array($typeName, self::CaseInsensitiveTypes, true)) {
236239
return $converter;
237240
}
238241

@@ -271,17 +274,32 @@ private static function decimalConverter(): Closure
271274

272275
private static function dateConverter(): Closure
273276
{
274-
return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
275-
// We cannot convert to timestamp yet https://github.com/ClickHouse/ClickHouse/issues/75217
276-
? $value->format('Y-m-d')
277-
: $value;
277+
return static function (mixed $value) {
278+
if ($value instanceof DateTimeInterface) {
279+
return $value->format('Y-m-d');
280+
}
281+
282+
if (is_string($value) || is_float($value) || is_int($value)) {
283+
return $value;
284+
}
285+
286+
throw UnsupportedParamValue::type($value);
287+
};
278288
}
279289

280290
private static function dateTimeConverter(): Closure
281291
{
282-
return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface
283-
? $value->getTimestamp()
284-
: $value;
292+
return static function (mixed $value) {
293+
if ($value instanceof DateTimeInterface) {
294+
return $value->getTimestamp();
295+
}
296+
297+
if (is_string($value) || is_float($value) || is_int($value)) {
298+
return $value;
299+
}
300+
301+
throw UnsupportedParamValue::type($value);
302+
};
285303
}
286304

287305
private static function dateIntervalConverter(): Closure

0 commit comments

Comments
 (0)