forked from Crell/Serde
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DateTimeZoneExporter.php
47 lines (38 loc) · 1.26 KB
/
DateTimeZoneExporter.php
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
<?php
declare(strict_types=1);
namespace Crell\Serde\PropertyHandler;
use Crell\Serde\Attributes\Field;
use Crell\Serde\Deserializer;
use Crell\Serde\DeformatterResult;
use Crell\Serde\Serializer;
class DateTimeZoneExporter implements Exporter, Importer
{
/**
* @param Serializer $serializer
* @param Field $field
* @param \DateTimeZone $value
* @param mixed $runningValue
* @return mixed
*/
public function exportValue(Serializer $serializer, Field $field, mixed $value, mixed $runningValue): mixed
{
$string = $value->getName();
return $serializer->formatter->serializeString($runningValue, $field, $string);
}
public function canExport(Field $field, mixed $value, string $format): bool
{
return $field->phpType === \DateTimeZone::class;
}
public function importValue(Deserializer $deserializer, Field $field, mixed $source): mixed
{
$string = $deserializer->deformatter->deserializeString($source, $field);
if ($string instanceof DeformatterResult) {
return null;
}
return new \DateTimeZone($string);
}
public function canImport(Field $field, string $format): bool
{
return $field->phpType === \DateTimeZone::class;
}
}