[Validator] Fix TimezoneValidator default option

This commit is contained in:
Roland Franssen 2019-05-30 18:46:20 +02:00 committed by Nicolas Grekas
parent 3392186e7f
commit ee71cdbae4
4 changed files with 11 additions and 27 deletions

View File

@ -43,7 +43,7 @@ class Timezone extends Constraint
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct(array $options = null) public function __construct($options = null)
{ {
parent::__construct($options); 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.'); 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(); ->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 private static function getPhpTimezones(int $zone, string $countryCode = null): array
{ {
if (null !== $countryCode) { if (null !== $countryCode) {

View File

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

View File

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