-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextDecoder.php
More file actions
42 lines (34 loc) · 1002 Bytes
/
TextDecoder.php
File metadata and controls
42 lines (34 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
declare(strict_types=1);
namespace TinyBlocks\Time\Internal;
use DateTimeImmutable;
use TinyBlocks\Time\Internal\Decoders\DatabaseDateTimeDecoder;
use TinyBlocks\Time\Internal\Decoders\Decoder;
use TinyBlocks\Time\Internal\Decoders\OffsetDateTimeDecoder;
use TinyBlocks\Time\Internal\Exceptions\InvalidInstant;
final readonly class TextDecoder
{
/**
* @param list<Decoder> $decoders
*/
private function __construct(private array $decoders)
{
}
public static function create(): TextDecoder
{
return new TextDecoder(decoders: [
new OffsetDateTimeDecoder(),
new DatabaseDateTimeDecoder()
]);
}
public function decode(string $value): DateTimeImmutable
{
foreach ($this->decoders as $decoder) {
$result = $decoder->decode(value: $value);
if ($result !== null) {
return $result;
}
}
throw new InvalidInstant(value: $value);
}
}