Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
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 * A helper providing autocompletion for available DateTimeNormalizer options.
21 *
22 * @author Mathias Arlaud <[email protected]>
23 */
24 final class DateTimeNormalizerContextBuilder implements ContextBuilderInterface
25 {
26 use ContextBuilderTrait;
27
28 /**
29 * Configures the format of the date.
30 *
31 * @see https://secure.php.net/manual/en/datetime.format.php
32 */
33 public function withFormat(?string $format): static
34 {
35 return $this->with(DateTimeNormalizer::FORMAT_KEY, $format);
36 }
37
38 /**
39 * Configures the timezone of the date.
40 *
41 * It could be either a \DateTimeZone or a string
42 * that will be used to construct the \DateTimeZone
43 *
44 * @see https://secure.php.net/manual/en/class.datetimezone.php
45 *
46 * @param ?bool $force Whether to enforce the timezone during denormalization
47 *
48 * @throws InvalidArgumentException
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 * @param 'int'|'float'|null $cast
69 */
70 public function withCast(?string $cast): static
71 {
72 return $this->with(DateTimeNormalizer::CAST_KEY, $cast);
73 }
74 }