-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #31195 [Form] Add intltimezone input to TimezoneType (ro0NL)
This PR was squashed before being merged into the 4.3-dev branch (closes #31195). Discussion ---------- [Form] Add intltimezone input to TimezoneType | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #22302 (ref #28836) | License | MIT | Doc PR | symfony/symfony-docs#11463 cc @rvanlaak Commits ------- e169dfb [Form] Add intltimezone input to TimezoneType
- Loading branch information
Showing
4 changed files
with
206 additions
and
4 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...Symfony/Component/Form/Extension/Core/DataTransformer/IntlTimeZoneToStringTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?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; | ||
} | ||
|
||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...mponent/Form/Tests/Extension/Core/DataTransformer/IntlTimeZoneToStringTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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\Tests\Extension\Core\DataTransformer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\Extension\Core\DataTransformer\IntlTimeZoneToStringTransformer; | ||
|
||
/** | ||
* @requires extension intl | ||
*/ | ||
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')])); | ||
$this->assertEquals([\IntlTimeZone::createTimeZone('Europe/Amsterdam')], $transformer->reverseTransform(['Europe/Amsterdam'])); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testInvalidTimezone() | ||
{ | ||
(new IntlTimeZoneToStringTransformer())->transform(1); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testUnknownTimezone() | ||
{ | ||
(new IntlTimeZoneToStringTransformer(true))->reverseTransform(['Foo/Bar']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters