|
8 | 8 | use DateTimeInterface; |
9 | 9 | use Psr\Http\Message\StreamInterface; |
10 | 10 | use SimPod\ClickHouseClient\Exception\UnsupportedParamType; |
| 11 | +use SimPod\ClickHouseClient\Exception\UnsupportedParamValue; |
11 | 12 | use SimPod\ClickHouseClient\Sql\Escaper; |
12 | 13 | use SimPod\ClickHouseClient\Sql\Type; |
13 | 14 |
|
|
18 | 19 | use function implode; |
19 | 20 | use function in_array; |
20 | 21 | use function is_array; |
| 22 | +use function is_float; |
| 23 | +use function is_int; |
21 | 24 | use function is_string; |
22 | 25 | use function json_encode; |
23 | 26 | use function sprintf; |
|
26 | 29 | use function trim; |
27 | 30 |
|
28 | 31 | /** |
29 | | - * @phpstan-type Converter = Closure(mixed, Type|string|null, bool):(StreamInterface|string) |
30 | | - * @phpstan-type ConverterRegistry = array<string, Converter> |
| 32 | + * @phpstan-type Converter Closure(mixed, Type|string|null, bool):(StreamInterface|string) |
| 33 | + * @phpstan-type ConverterRegistry array<string, Converter> |
31 | 34 | */ |
32 | 35 | final class ParamValueConverterRegistry |
33 | 36 | { |
34 | | - /** @var list<string> */ |
35 | | - private static array $caseInsensitiveTypes = [ |
| 37 | + private const CaseInsensitiveTypes = [ |
36 | 38 | 'bool', |
37 | 39 | 'date', |
38 | 40 | 'date32', |
@@ -94,9 +96,17 @@ public function __construct(array $registry = []) |
94 | 96 | 'date32' => self::dateConverter(), |
95 | 97 | 'datetime' => self::dateTimeConverter(), |
96 | 98 | 'datetime32' => self::dateTimeConverter(), |
97 | | - 'datetime64' => static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface |
98 | | - ? $value->format('U.u') |
99 | | - : $value, |
| 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 | + }, |
100 | 110 |
|
101 | 111 | 'Dynamic' => self::noopConverter(), |
102 | 112 | 'Variant' => self::noopConverter(), |
@@ -232,7 +242,7 @@ public function get(Type|string $type): Closure |
232 | 242 |
|
233 | 243 | $typeName = strtolower($typeName); |
234 | 244 | $converter = $this->registry[$typeName] ?? null; |
235 | | - if ($converter !== null && in_array($typeName, self::$caseInsensitiveTypes, true)) { |
| 245 | + if ($converter !== null && in_array($typeName, self::CaseInsensitiveTypes, true)) { |
236 | 246 | return $converter; |
237 | 247 | } |
238 | 248 |
|
@@ -271,17 +281,32 @@ private static function decimalConverter(): Closure |
271 | 281 |
|
272 | 282 | private static function dateConverter(): Closure |
273 | 283 | { |
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; |
| 284 | + return static function (mixed $value) { |
| 285 | + if ($value instanceof DateTimeInterface) { |
| 286 | + return $value->format('Y-m-d'); |
| 287 | + } |
| 288 | + |
| 289 | + if (is_string($value) || is_float($value) || is_int($value)) { |
| 290 | + return $value; |
| 291 | + } |
| 292 | + |
| 293 | + throw UnsupportedParamValue::type($value); |
| 294 | + }; |
278 | 295 | } |
279 | 296 |
|
280 | 297 | private static function dateTimeConverter(): Closure |
281 | 298 | { |
282 | | - return static fn (DateTimeInterface|string|int|float $value) => $value instanceof DateTimeInterface |
283 | | - ? $value->getTimestamp() |
284 | | - : $value; |
| 299 | + return static function (mixed $value) { |
| 300 | + if ($value instanceof DateTimeInterface) { |
| 301 | + return $value->getTimestamp(); |
| 302 | + } |
| 303 | + |
| 304 | + if (is_string($value) || is_float($value) || is_int($value)) { |
| 305 | + return $value; |
| 306 | + } |
| 307 | + |
| 308 | + throw UnsupportedParamValue::type($value); |
| 309 | + }; |
285 | 310 | } |
286 | 311 |
|
287 | 312 | private static function dateIntervalConverter(): Closure |
|
0 commit comments