Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DateTimeNormalizerContextBuilder.php

class DateTimeNormalizerContextBuilder

A helper providing autocompletion for available DateTimeNormalizer options.

@author Mathias Arlaud <mathias.arlaud@gmail.com>

Hierarchy

  • class \Symfony\Component\Serializer\Context\Normalizer\DateTimeNormalizerContextBuilder implements \Symfony\Component\Serializer\Context\ContextBuilderInterface uses \Symfony\Component\Serializer\Context\ContextBuilderTrait

Expanded class hierarchy of DateTimeNormalizerContextBuilder

File

vendor/symfony/serializer/Context/Normalizer/DateTimeNormalizerContextBuilder.php, line 24

Namespace

Symfony\Component\Serializer\Context\Normalizer
View source
final class DateTimeNormalizerContextBuilder implements ContextBuilderInterface {
    use ContextBuilderTrait;
    
    /**
     * Configures the format of the date.
     *
     * @see https://secure.php.net/manual/en/datetime.format.php
     */
    public function withFormat(?string $format) : static {
        return $this->with(DateTimeNormalizer::FORMAT_KEY, $format);
    }
    
    /**
     * Configures the timezone of the date.
     *
     * It could be either a \DateTimeZone or a string
     * that will be used to construct the \DateTimeZone
     *
     * @see https://secure.php.net/manual/en/class.datetimezone.php
     *
     * @throws InvalidArgumentException
     */
    public function withTimezone(\DateTimeZone|string|null $timezone) : static {
        if (null === $timezone) {
            return $this->with(DateTimeNormalizer::TIMEZONE_KEY, null);
        }
        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);
    }
    
    /**
     * @param 'int'|'float'|null $cast
     */
    public function withCast(?string $cast) : static {
        return $this->with(DateTimeNormalizer::CAST_KEY, $cast);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ContextBuilderTrait::$context private property
ContextBuilderTrait::toArray public function
ContextBuilderTrait::with protected function
ContextBuilderTrait::withContext public function
DateTimeNormalizerContextBuilder::withCast public function
DateTimeNormalizerContextBuilder::withFormat public function Configures the format of the date.
DateTimeNormalizerContextBuilder::withTimezone public function Configures the timezone of the date.
RSS feed
Powered by Drupal