| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Serializer\Context\Normalizer; |
| 13 |
|
| 14 |
use Symfony\Component\Serializer\Context\ContextBuilderInterface; |
| 15 |
use Symfony\Component\Serializer\Context\ContextBuilderTrait; |
| 16 |
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
| 17 |
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final class DateTimeNormalizerContextBuilder implements ContextBuilderInterface |
| 25 |
{ |
| 26 |
use ContextBuilderTrait; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function withFormat(?string $format): static |
| 34 |
{ |
| 35 |
return $this->with(DateTimeNormalizer::FORMAT_KEY, $format); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function withTimezone(\DateTimeZone|string|null $timezone, ?bool $force = null): static |
| 51 |
{ |
| 52 |
if (null === $timezone) { |
| 53 |
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, null)->with(DateTimeNormalizer::FORCE_TIMEZONE_KEY, $force); |
| 54 |
} |
| 55 |
|
| 56 |
if (\is_string($timezone)) { |
| 57 |
try { |
| 58 |
$timezone = new \DateTimeZone($timezone); |
| 59 |
} catch (\Exception $e) { |
| 60 |
throw new InvalidArgumentException(\sprintf('The "%s" timezone is invalid.', $timezone), previous: $e); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, $timezone)->with(DateTimeNormalizer::FORCE_TIMEZONE_KEY, $force); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function withCast(?string $cast): static |
| 71 |
{ |
| 72 |
return $this->with(DateTimeNormalizer::CAST_KEY, $cast); |
| 73 |
} |
| 74 |
} |