Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e1831c4

Browse files
committed
Update TimezoneValidator
1 parent 0491875 commit e1831c4

File tree

5 files changed

+46
-68
lines changed

5 files changed

+46
-68
lines changed

src/Symfony/Component/Validator/Constraints/Timezone.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ class Timezone extends Constraint
2525
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';
2626
const NO_SUCH_TIMEZONE_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8';
2727
const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b';
28-
29-
public $zone;
28+
public $zone = \DateTimeZone::ALL;
3029
public $countryCode;
31-
public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.';
30+
public $message = 'This value is not a valid timezone.';
3231

3332
protected static $errorNames = array(
3433
self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR',
@@ -41,16 +40,10 @@ class Timezone extends Constraint
4140
*/
4241
public function __construct(array $options = null)
4342
{
44-
$this->zone = $options['zone'] ?? null;
45-
46-
if (isset($options['countryCode'])) {
47-
if (\DateTimeZone::PER_COUNTRY !== $this->zone) {
48-
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value');
49-
}
43+
parent::__construct($options);
5044

51-
$this->countryCode = $options['countryCode'];
45+
if ($this->countryCode && \DateTimeZone::PER_COUNTRY !== $this->zone) {
46+
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value');
5247
}
53-
54-
parent::__construct($options);
5548
}
5649
}

src/Symfony/Component/Validator/Constraints/TimezoneValidator.php

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,27 @@ public function validate($value, Constraint $constraint)
4040
}
4141

4242
$value = (string) $value;
43-
$zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL;
4443

4544
// @see: https://bugs.php.net/bug.php?id=75928
4645
if ($constraint->countryCode) {
47-
$timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode);
46+
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode);
4847
} else {
49-
$timezoneIds = \DateTimeZone::listIdentifiers($zone);
48+
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone);
5049
}
5150

5251
if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
5352
if ($constraint->countryCode) {
5453
$code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR;
55-
} elseif (null !== $constraint->zone) {
54+
} elseif (\DateTimeZone::ALL !== $constraint->zone) {
5655
$code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR;
5756
} else {
5857
$code = Timezone::NO_SUCH_TIMEZONE_ERROR;
5958
}
6059

61-
$violation = $this->context->buildViolation($constraint->message);
62-
63-
foreach ($this->generateValidationMessage($constraint->zone, $constraint->countryCode) as $placeholder => $message) {
64-
$violation->setParameter($placeholder, $message);
65-
}
66-
67-
$violation
60+
$this->context->buildViolation($constraint->message)
61+
->setParameter('{{ value }}', $this->formatValue($value))
6862
->setCode($code)
69-
->addViolation()
70-
;
63+
->addViolation();
7164
}
7265
}
7366

@@ -80,35 +73,23 @@ public function getDefaultOption()
8073
}
8174

8275
/**
83-
* Generates the replace parameters which are used in validation message.
84-
*
85-
* @param int|null $zone
86-
* @param string|null $countryCode
87-
*
88-
* @return array
76+
* {@inheritdoc}
8977
*/
90-
private function generateValidationMessage(int $zone = null, string $countryCode = null): array
78+
protected function formatValue($value, $format = 0)
9179
{
92-
$values = array(
93-
'{{ country_code_message }}' => '',
94-
'{{ zone_message }}' => '',
95-
);
96-
97-
if (null !== $zone) {
98-
if (\DateTimeZone::PER_COUNTRY !== $zone) {
80+
$value = parent::formatValue($value, $format);
81+
if ($value) {
82+
if (\DateTimeZone::PER_COUNTRY !== $value) {
9983
$r = new \ReflectionClass(\DateTimeZone::class);
10084
$consts = $r->getConstants();
101-
if ($zoneFound = array_search($zone, $consts, true)) {
102-
$values['{{ zone_message }}'] = ' at "'.$zoneFound.'" zone';
103-
} else {
104-
$values['{{ zone_message }}'] = ' at zone with identifier '.$zone;
85+
if ($zoneFound = array_search($value, $consts, true)) {
86+
return $zoneFound;
10587
}
106-
}
107-
if ($countryCode) {
108-
$values['{{ country_code_message }}'] = ' for ISO 3166-1 country code "'.$countryCode.'"';
88+
89+
return $value;
10990
}
11091
}
11192

112-
return $values;
93+
return $value;
11394
}
11495
}

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@
326326
<source>This value should be a multiple of {{ compared_value }}.</source>
327327
<target>This value should be a multiple of {{ compared_value }}.</target>
328328
</trans-unit>
329+
<trans-unit id="85">
330+
<source>This value is not a valid timezone.</source>
331+
<target>This value is not a valid timezone.</target>
332+
</trans-unit>
329333
</body>
330334
</file>
331335
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.es.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@
326326
<source>This value should be a multiple of {{ compared_value }}.</source>
327327
<target>Este valor debería ser un múltiplo de {{ compared_value }}.</target>
328328
</trans-unit>
329+
<trans-unit id="85">
330+
<source>This value is not a valid timezone.</source>
331+
<target>Este valor no es una zona horaria válida.</target>
332+
</trans-unit>
329333
</body>
330334
</file>
331335
</xliff>

src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function getValidGroupedTimezones(): iterable
106106
/**
107107
* @dataProvider getInvalidTimezones
108108
*/
109-
public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMessage, string $countryCodeMessage)
109+
public function testInvalidTimezonesWithoutZone(string $timezone)
110110
{
111111
$constraint = new Timezone(array(
112112
'message' => 'myMessage',
@@ -115,25 +115,24 @@ public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMe
115115
$this->validator->validate($timezone, $constraint);
116116

117117
$this->buildViolation('myMessage')
118-
->setParameter('{{ zone_message }}', $zoneMessage)
119-
->setParameter('{{ country_code_message }}', $countryCodeMessage)
118+
->setParameter('{{ value }}', '"'.$timezone.'"')
120119
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
121120
->assertRaised();
122121
}
123122

124123
public function getInvalidTimezones(): iterable
125124
{
126125
return array(
127-
array('Buenos_Aires/Argentina/America', '', ''),
128-
array('Mayotte/Indian', '', ''),
129-
array('foobar', '', ''),
126+
array('Buenos_Aires/Argentina/America'),
127+
array('Mayotte/Indian'),
128+
array('foobar'),
130129
);
131130
}
132131

133132
/**
134133
* @dataProvider getInvalidGroupedTimezones
135134
*/
136-
public function testInvalidGroupedTimezones(string $timezone, int $what, string $zoneMessage, string $countryCodeMessage)
135+
public function testInvalidGroupedTimezones(string $timezone, int $what)
137136
{
138137
$constraint = new Timezone(array(
139138
'zone' => $what,
@@ -143,20 +142,19 @@ public function testInvalidGroupedTimezones(string $timezone, int $what, string
143142
$this->validator->validate($timezone, $constraint);
144143

145144
$this->buildViolation('myMessage')
146-
->setParameter('{{ zone_message }}', $zoneMessage)
147-
->setParameter('{{ country_code_message }}', $countryCodeMessage)
145+
->setParameter('{{ value }}', '"'.$timezone.'"')
148146
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR)
149147
->assertRaised();
150148
}
151149

152150
public function getInvalidGroupedTimezones(): iterable
153151
{
154152
return array(
155-
array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' at "AMERICA" zone', ''),
156-
array('America/Barbados', \DateTimeZone::ANTARCTICA, ' at "ANTARCTICA" zone', ''),
157-
array('Europe/Kiev', \DateTimeZone::ARCTIC, ' at "ARCTIC" zone', ''),
158-
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' at "INDIAN" zone', ''),
159-
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' at zone with identifier 260', ''),
153+
array('Antarctica/McMurdo', \DateTimeZone::AMERICA),
154+
array('America/Barbados', \DateTimeZone::ANTARCTICA),
155+
array('Europe/Kiev', \DateTimeZone::ARCTIC),
156+
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN),
157+
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA),
160158
);
161159
}
162160

@@ -197,7 +195,7 @@ public function getValidGroupedTimezonesByCountry(): iterable
197195
/**
198196
* @dataProvider getInvalidGroupedTimezonesByCountry
199197
*/
200-
public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country, string $zoneMessage, string $countryCodeMessage)
198+
public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country)
201199
{
202200
$constraint = new Timezone(array(
203201
'message' => 'myMessage',
@@ -208,17 +206,16 @@ public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what
208206
$this->validator->validate($timezone, $constraint);
209207

210208
$this->buildViolation('myMessage')
211-
->setParameter('{{ zone_message }}', $zoneMessage)
212-
->setParameter('{{ country_code_message }}', $countryCodeMessage)
209+
->setParameter('{{ value }}', '"'.$timezone.'"')
213210
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR)
214211
->assertRaised();
215212
}
216213

217214
public function getInvalidGroupedTimezonesByCountry(): iterable
218215
{
219216
return array(
220-
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', '', ' for ISO 3166-1 country code "FR"'),
221-
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', '', ' for ISO 3166-1 country code "PT"'),
217+
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR'),
218+
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT'),
222219
);
223220
}
224221

@@ -248,8 +245,7 @@ public function testDeprecatedTimezonesAreInvaildWithoutBC(string $timezone)
248245
$this->validator->validate($timezone, $constraint);
249246

250247
$this->buildViolation('myMessage')
251-
->setParameter('{{ zone_message }}', '')
252-
->setParameter('{{ country_code_message }}', '')
248+
->setParameter('{{ value }}', '"'.$timezone.'"')
253249
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
254250
->assertRaised();
255251
}

0 commit comments

Comments
 (0)