forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeTypeEncoder.php
More file actions
56 lines (46 loc) · 1.35 KB
/
DateTimeTypeEncoder.php
File metadata and controls
56 lines (46 loc) · 1.35 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace Soap\Encoding\Encoder\SimpleType;
use DateTimeImmutable;
use DateTimeInterface;
use Soap\Encoding\Encoder\Context;
use Soap\Encoding\Encoder\XmlEncoder;
use VeeWee\Reflecta\Iso\Iso;
/**
* @implements XmlEncoder<DateTimeInterface, string>
*/
final class DateTimeTypeEncoder implements XmlEncoder
{
public const DATE_FORMAT_TIME_ZONED = 'Y-m-d\TH:i:sP';
public const DATE_FORMAT_LOCAL = 'Y-m-d\TH:i:s';
public function __construct(
private readonly string $dateFormat
) {
}
public static function default(): self
{
return self::timeZoned();
}
public static function local(): self
{
/** @psalm-var DateTimeTypeEncoder $instance */
static $instance = new self(self::DATE_FORMAT_LOCAL);
return $instance;
}
public static function timeZoned(): self
{
/** @psalm-var DateTimeTypeEncoder $instance */
static $instance = new self(self::DATE_FORMAT_TIME_ZONED);
return $instance;
}
/**
* @return Iso<DateTimeInterface, string>
*/
public function iso(Context $context): Iso
{
return (new Iso(
fn (DateTimeInterface $value): string => $value->format($this->dateFormat),
static fn (string $value): DateTimeInterface => new DateTimeImmutable($value)
));
}
}