Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
86.96% covered (warning)
86.96%
20 / 23
IntlTimeZoneToStringTransformer
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
12.32
86.96% covered (warning)
86.96%
20 / 23
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 transform
0.00% covered (danger)
0.00%
0 / 1
5.03
88.89% covered (warning)
88.89%
8 / 9
 reverseTransform
0.00% covered (danger)
0.00%
0 / 1
6.17
83.33% covered (warning)
83.33%
10 / 12
<?php
/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Symfony\Component\Form\Extension\Core\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
/**
 * Transforms between a timezone identifier string and a IntlTimeZone object.
 *
 * @author Roland Franssen <franssen.roland@gmail.com>
 */
class IntlTimeZoneToStringTransformer implements DataTransformerInterface
{
    private $multiple;
    public function __construct(bool $multiple = false)
    {
        $this->multiple = $multiple;
    }
    /**
     * {@inheritdoc}
     */
    public function transform($intlTimeZone)
    {
        if (null === $intlTimeZone) {
            return null;
        }
        if ($this->multiple) {
            if (!\is_array($intlTimeZone)) {
                throw new TransformationFailedException('Expected an array of \IntlTimeZone objects.');
            }
            return array_map([new self(), 'transform'], $intlTimeZone);
        }
        if (!$intlTimeZone instanceof \IntlTimeZone) {
            throw new TransformationFailedException('Expected a \IntlTimeZone object.');
        }
        return $intlTimeZone->getID();
    }
    /**
     * {@inheritdoc}
     */
    public function reverseTransform($value)
    {
        if (null === $value) {
            return;
        }
        if ($this->multiple) {
            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;
    }
}