Skip to content

Commit

Permalink
bug #31744 [Validator] Fix TimezoneValidator default option (ro0NL)
Browse files Browse the repository at this point in the history
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
-------

ee71cdb [Validator] Fix TimezoneValidator default option
  • Loading branch information
nicolas-grekas committed May 31, 2019
2 parents d38c19a + ee71cdb commit 89f423f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 27 deletions.
10 changes: 9 additions & 1 deletion src/Symfony/Component/Validator/Constraints/Timezone.php
Expand Up @@ -43,7 +43,7 @@ class Timezone extends Constraint
/**
* {@inheritdoc}
*/
public function __construct(array $options = null)
public function __construct($options = null)
{
parent::__construct($options);

Expand All @@ -58,4 +58,12 @@ public function __construct(array $options = null)
throw new ConstraintDefinitionException('The option "intlCompatible" can only be used when the PHP intl extension is available.');
}
}

/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'zone';
}
}
22 changes: 0 additions & 22 deletions src/Symfony/Component/Validator/Constraints/TimezoneValidator.php
Expand Up @@ -75,28 +75,6 @@ public function validate($value, Constraint $constraint)
->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) {
Expand Down
Expand Up @@ -23,7 +23,7 @@ public function testValidTimezoneConstraints()
{
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',
Expand Down
Expand Up @@ -267,9 +267,7 @@ public function testGroupedTimezonesWithInvalidCountry()
*/
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);

Expand Down

0 comments on commit 89f423f

Please sign in to comment.