-
-
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 #23648 [Form] Add input + regions options to TimezoneType (ro…
…0NL) This PR was merged into the 3.4 branch. Discussion ---------- [Form] Add input + regions options to TimezoneType | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#8223 I want to use `\DateTimeZone` as a model format, with only european timezones in the form available. Hence the new options. Commits ------- 183307b [Form] Add input + regions options to TimezoneType
- Loading branch information
Showing
7 changed files
with
272 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
82 changes: 82 additions & 0 deletions
82
...Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeZoneToStringTransformer.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,82 @@ | ||
<?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 DateTimeZone object. | ||
* | ||
* @author Roland Franssen <franssen.roland@gmai.com> | ||
*/ | ||
class DateTimeZoneToStringTransformer implements DataTransformerInterface | ||
{ | ||
private $multiple; | ||
|
||
public function __construct($multiple = false) | ||
{ | ||
$this->multiple = $multiple; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function transform($dateTimeZone) | ||
{ | ||
if (null === $dateTimeZone) { | ||
return; | ||
} | ||
|
||
if ($this->multiple) { | ||
if (!is_array($dateTimeZone)) { | ||
throw new TransformationFailedException('Expected an array.'); | ||
} | ||
|
||
return array_map(array(new self(), 'transform'), $dateTimeZone); | ||
} | ||
|
||
if (!$dateTimeZone instanceof \DateTimeZone) { | ||
throw new TransformationFailedException('Expected a \DateTimeZone.'); | ||
} | ||
|
||
return $dateTimeZone->getName(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (null === $value) { | ||
return; | ||
} | ||
|
||
if ($this->multiple) { | ||
if (!is_array($value)) { | ||
throw new TransformationFailedException('Expected an array.'); | ||
} | ||
|
||
return array_map(array(new self(), 'reverseTransform'), $value); | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw new TransformationFailedException('Expected a string.'); | ||
} | ||
|
||
try { | ||
return new \DateTimeZone($value); | ||
} catch (\Exception $e) { | ||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
...mponent/Form/Tests/Extension/Core/DataTransformer/DateTimeZoneToStringTransformerTest.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,56 @@ | ||
<?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 Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeZoneToStringTransformer; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DateTimeZoneToStringTransformerTest extends TestCase | ||
{ | ||
public function testSingle() | ||
{ | ||
$transformer = new DateTimeZoneToStringTransformer(); | ||
|
||
$this->assertNull($transformer->transform(null)); | ||
$this->assertNull($transformer->reverseTransform(null)); | ||
|
||
$this->assertSame('Europe/Amsterdam', $transformer->transform(new \DateTimeZone('Europe/Amsterdam'))); | ||
$this->assertEquals(new \DateTimeZone('Europe/Amsterdam'), $transformer->reverseTransform('Europe/Amsterdam')); | ||
} | ||
|
||
public function testMultiple() | ||
{ | ||
$transformer = new DateTimeZoneToStringTransformer(true); | ||
|
||
$this->assertNull($transformer->transform(null)); | ||
$this->assertNull($transformer->reverseTransform(null)); | ||
|
||
$this->assertSame(array('Europe/Amsterdam'), $transformer->transform(array(new \DateTimeZone('Europe/Amsterdam')))); | ||
$this->assertEquals(array(new \DateTimeZone('Europe/Amsterdam')), $transformer->reverseTransform(array('Europe/Amsterdam'))); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testInvalidTimezone() | ||
{ | ||
(new DateTimeZoneToStringTransformer())->transform(1); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testUnknownTimezone() | ||
{ | ||
(new DateTimeZoneToStringTransformer(true))->reverseTransform(array('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