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

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update way of handling zone option
  • Loading branch information
phansys committed Nov 29, 2018
commit dfa4f984416d9c0e0eb2f4a9b930ffc5301970fe
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/Constraints/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Timezone extends Constraint
{
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';

public $zone = \DateTimeZone::ALL;
public $zone;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newlines between public props can be removed

public $countryCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function validate($value, Constraint $constraint)
}

$value = (string) $value;
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode);
$zone = null !== $constraint->zone ? $constraint->zone : \DateTimeZone::ALL;
$timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listIdentifiers actually allows zones like \DateTimeZone::AMERICA | \DateTimeZone::EUROPE.. thats cool 👍


if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
$this->context->buildViolation($constraint->message)
Expand All @@ -62,13 +63,16 @@ public function getDefaultOption()
* Format the extra info which is appended to validation message based on
* constraint options.
*
* @param int $zone
* @param int|null $zone
* @param string|null $countryCode
*
* @return string
*/
private function formatExtraInfo($zone, $countryCode = null)
{
if (null === $zone) {
return '';
}
if ($countryCode) {
$value = ' for ISO 3166-1 country code "'.$countryCode.'"';
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getValidGroupedTimezones()
/**
* @dataProvider getInvalidTimezones
*/
public function testInvalidTimezones($timezone, $extraInfo)
public function testInvalidTimezonesWithoutZone($timezone, $extraInfo)
{
$constraint = new Timezone(array(
'message' => 'myMessage',
Expand All @@ -115,17 +115,17 @@ public function testInvalidTimezones($timezone, $extraInfo)
$this->validator->validate($timezone, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ extra_info }}', '"'.$extraInfo.'"')
->setParameter('{{ extra_info }}', $extraInfo)
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->assertRaised();
}

public function getInvalidTimezones()
{
return array(
array('Buenos_Aires/Argentina/America', ' for "ALL" zone'),
array('Mayotte/Indian', ' for "ALL" zone'),
array('foobar', ' for "ALL" zone'),
array('Buenos_Aires/Argentina/America', ''),
array('Mayotte/Indian', ''),
array('foobar', ''),
);
}

Expand Down