<?php
namespace Symfony\Component\Serializer\Context\Normalizer;
use Symfony\Component\Serializer\Context\ContextBuilderInterface;
use Symfony\Component\Serializer\Context\ContextBuilderTrait;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
final class DateTimeNormalizerContextBuilder implements ContextBuilderInterface
{
use ContextBuilderTrait;
public function withFormat(?string $format): static
{
return $this->with(DateTimeNormalizer::FORMAT_KEY, $format);
}
public function withTimezone(\DateTimeZone|string|null $timezone, ?bool $force = null): static
{
if (null === $timezone) {
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, null)->with(DateTimeNormalizer::FORCE_TIMEZONE_KEY, $force);
}
if (\is_string($timezone)) {
try {
$timezone = new \DateTimeZone($timezone);
} catch (\Exception $e) {
throw new InvalidArgumentException(\sprintf('The "%s" timezone is invalid.', $timezone), previous: $e);
}
}
return $this->with(DateTimeNormalizer::TIMEZONE_KEY, $timezone)->with(DateTimeNormalizer::FORCE_TIMEZONE_KEY, $force);
}
public function withCast(?string $cast): static
{
return $this->with(DateTimeNormalizer::CAST_KEY, $cast);
}
}