bug #31744 [Validator] Fix TimezoneValidator default option (ro0NL)

This PR was squashed before being merged into the 4.3 branch (closes #31744).

Discussion
----------

[Validator] Fix TimezoneValidator default option

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #...   <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too).
 - Features and deprecations must be submitted against branch 4.4.
 - Legacy code removals go to the master branch.
-->

Commits
-------

ee71cdbae4 [Validator] Fix TimezoneValidator default option
This commit is contained in:
Nicolas Grekas 2019-05-31 09:43:12 +02:00
commit 89f423fc35
4 changed files with 11 additions and 27 deletions

View File

@ -43,7 +43,7 @@ class Timezone extends Constraint
/**
* {@inheritdoc}
*/
public function __construct(array $options = null)
public function __construct($options = null)
{
parent::__construct($options);
@ -58,4 +58,12 @@ class Timezone extends Constraint
throw new ConstraintDefinitionException('The option "intlCompatible" can only be used when the PHP intl extension is available.');
}
}
/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'zone';
}
}

View File

@ -75,28 +75,6 @@ class TimezoneValidator extends ConstraintValidator
->addViolation();
}
/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'zone';
}
/**
* {@inheritdoc}
*/
protected function formatValue($value, $format = 0)
{
$value = parent::formatValue($value, $format);
if (!$value || \DateTimeZone::PER_COUNTRY === $value) {
return $value;
}
return array_search($value, (new \ReflectionClass(\DateTimeZone::class))->getConstants(), true) ?: $value;
}
private static function getPhpTimezones(int $zone, string $countryCode = null): array
{
if (null !== $countryCode) {

View File

@ -23,7 +23,7 @@ class TimezoneTest extends TestCase
{
new Timezone();
new Timezone(['zone' => \DateTimeZone::ALL]);
new Timezone(['zone' => \DateTimeZone::ALL_WITH_BC]);
new Timezone(\DateTimeZone::ALL_WITH_BC);
new Timezone([
'zone' => \DateTimeZone::PER_COUNTRY,
'countryCode' => 'AR',

View File

@ -267,9 +267,7 @@ class TimezoneValidatorTest extends ConstraintValidatorTestCase
*/
public function testDeprecatedTimezonesAreValidWithBC(string $timezone)
{
$constraint = new Timezone([
'zone' => \DateTimeZone::ALL_WITH_BC,
]);
$constraint = new Timezone(\DateTimeZone::ALL_WITH_BC);
$this->validator->validate($timezone, $constraint);