Skip to content

Commit

Permalink
refactor(param-value-converter-registry): wider date converters input…
Browse files Browse the repository at this point in the history
… type and throw exception
  • Loading branch information
simPod committed Jan 30, 2025
1 parent cbbc2d7 commit b1d639a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/Param/ParamValueConverterRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +32,7 @@
* @phpstan-type Converter Closure(mixed, Type|string|null, bool):(StreamInterface|string)
* @phpstan-type ConverterRegistry array<string, Converter>
*/
final readonly class ParamValueConverterRegistry
final class ParamValueConverterRegistry
{
private const CaseInsensitiveTypes = [
'bool',
Expand Down Expand Up @@ -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);

Check warning on line 108 in src/Param/ParamValueConverterRegistry.php

View check run for this annotation

Codecov / codecov/patch

src/Param/ParamValueConverterRegistry.php#L108

Added line #L108 was not covered by tests
},

'Dynamic' => self::noopConverter(),
'Variant' => self::noopConverter(),
Expand Down

0 comments on commit b1d639a

Please sign in to comment.