diff --git a/src/sammo/DTO/Converter/DateTimeConverter.php b/src/sammo/DTO/Converter/DateTimeConverter.php new file mode 100644 index 00000000..187782d8 --- /dev/null +++ b/src/sammo/DTO/Converter/DateTimeConverter.php @@ -0,0 +1,46 @@ + 0) { + if(!is_bool($args[0])) { + throw new \Exception('DateTimeConverter constructor argument must be boolean'); + } + $this->useFraction = $args[0]; + } else { + $this->useFraction = false; + } + } + + public function convertFrom(string|array|int|float|bool|null $raw): mixed + { + if ($raw === null && array_search('null', $this->types, true) !== false) { + return null; + } + if (!is_string($raw)){ + throw new \Exception('DateTimeConverter can not convert non-string'); + } + if (array_search('DateTime', $this->types, true) === false) { + return new \DateTime($raw); + } + return new \DateTimeImmutable($raw); + } + + public function convertTo(mixed $data): string|array|int|float|bool|null + { + if ($data === null && array_search('null', $this->types, true) !== false) { + return null; + } + if (!$data instanceof \DateTimeInterface) { + throw new \Exception('DateTimeConverter can not convert non-DateTimeInterface'); + } + return TimeUtil::format($data, $this->useFraction); + } +}