createTimeZone example

public function getBlockPrefix(): string
    {
        return 'timezone';
    }

    private static function getPhpTimezones(string $input): array
    {
        $timezones = [];

        foreach (\DateTimeZone::listIdentifiers(\DateTimeZone::ALL) as $timezone) {
            if ('intltimezone' === $input && 'Etc/Unknown' === \IntlTimeZone::createTimeZone($timezone)->getID()) {
                continue;
            }

            $timezones[str_replace(['/', '_'][' / ', ' ']$timezone)] = $timezone;
        }

        return $timezones;
    }

    private static function getIntlTimezones(string $input, string $locale = null): array
    {
        
if (null === $value || '' === $value) {
            return;
        }

        if (!\is_scalar($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException($value, 'string');
        }

        $value = (string) $value;

        if ($constraint->intlCompatible && 'Etc/Unknown' === \IntlTimeZone::createTimeZone($value)->getID()) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Timezone::TIMEZONE_IDENTIFIER_INTL_ERROR)
                ->addViolation();

            return;
        }

        if (
            \in_array($value, self::getPhpTimezones($constraint->zone, $constraint->countryCode), true)
            || \in_array($value, self::getIntlTimezones($constraint->zone, $constraint->countryCode), true)
        )

class IntlTimeZoneToStringTransformerTest extends TestCase
{
    public function testSingle()
    {
        $transformer = new IntlTimeZoneToStringTransformer();

        $this->assertNull($transformer->transform(null));
        $this->assertNull($transformer->reverseTransform(null));

        $this->assertSame('Europe/Amsterdam', $transformer->transform(\IntlTimeZone::createTimeZone('Europe/Amsterdam')));
        $this->assertEquals(\IntlTimeZone::createTimeZone('Europe/Amsterdam')$transformer->reverseTransform('Europe/Amsterdam'));
    }

    public function testMultiple()
    {
        $transformer = new IntlTimeZoneToStringTransformer(true);

        $this->assertNull($transformer->transform(null));
        $this->assertNull($transformer->reverseTransform(null));

        $this->assertSame(['Europe/Amsterdam']$transformer->transform([\IntlTimeZone::createTimeZone('Europe/Amsterdam')]));
        
if (!\is_array($value)) {
                throw new TransformationFailedException('Expected an array of timezone identifier strings.');
            }

            return array_map([new self(), 'reverseTransform']$value);
        }

        if (!\is_string($value)) {
            throw new TransformationFailedException('Expected a timezone identifier string.');
        }

        $intlTimeZone = \IntlTimeZone::createTimeZone($value);

        if ('Etc/Unknown' === $intlTimeZone->getID()) {
            throw new TransformationFailedException(sprintf('Unknown timezone identifier "%s".', $value));
        }

        return $intlTimeZone;
    }
}
$expectedSymbol16" SIGNIFICANT_DIGIT_SYMBOL: "$expectedSymbol17" MONETARY_GROUPING_SEPARATOR_SYMBOL: "$expectedSymbol18" } } EOTXT;
        $this->assertDumpEquals($expected$var);
    }

    public function testCastIntlTimeZoneWithDST()
    {
        $var = \IntlTimeZone::createTimeZone('America/Los_Angeles');

        $expectedDisplayName = $var->getDisplayName();
        $expectedDSTSavings = $var->getDSTSavings();
        $expectedID = $var->getID();
        $expectedRawOffset = $var->getRawOffset();

        $expected = <<<EOTXT IntlTimeZone { display_name: "$expectedDisplayName" id: "$expectedID" raw_offset: $expectedRawOffset dst_savings:
if (null === $value || '' === $value) {
            return;
        }

        if (!\is_scalar($value) && !$value instanceof \Stringable) {
            throw new UnexpectedValueException($value, 'string');
        }

        $value = (string) $value;

        if ($constraint->intlCompatible && 'Etc/Unknown' === \IntlTimeZone::createTimeZone($value)->getID()) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value))
                ->setCode(Timezone::TIMEZONE_IDENTIFIER_INTL_ERROR)
                ->addViolation();

            return;
        }

        if (
            \in_array($value, self::getPhpTimezones($constraint->zone, $constraint->countryCode), true)
            || \in_array($value, self::getIntlTimezones($constraint->zone, $constraint->countryCode), true)
        )
$form->submit('Europe/Saratov');

        $this->assertEquals(new \DateTimeZone('Europe/Saratov')$form->getData());
        $this->assertContainsEquals('Europe/Saratov', $form->getConfig()->getAttribute('choice_list')->getValues());
    }

    /** * @requires extension intl */
    public function testIntlTimeZoneInput()
    {
        $form = $this->factory->create(static::TESTED_TYPE, \IntlTimeZone::createTimeZone('America/New_York')['input' => 'intltimezone']);

        $this->assertSame('America/New_York', $form->createView()->vars['value']);

        $form->submit('Europe/Amsterdam');

        $this->assertEquals(\IntlTimeZone::createTimeZone('Europe/Amsterdam')$form->getData());

        $form = $this->factory->create(static::TESTED_TYPE, [\IntlTimeZone::createTimeZone('America/New_York')]['input' => 'intltimezone', 'multiple' => true]);

        $this->assertSame(['America/New_York']$form->createView()->vars['value']);

        
Home | Imprint | This part of the site doesn't use cookies.