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 TimezoneValidator
  • Loading branch information
phansys committed Nov 29, 2018
commit 14689f7095a2884a444695afade0f7ed6d0dac71
17 changes: 5 additions & 12 deletions src/Symfony/Component/Validator/Constraints/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ class Timezone extends Constraint
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';
const NO_SUCH_TIMEZONE_IN_ZONE_ERROR = 'b57767b1-36c0-40ac-a3d7-629420c775b8';
const NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR = 'c4a22222-dc92-4fc0-abb0-d95b268c7d0b';

public $zone;
public $zone = \DateTimeZone::ALL;
public $countryCode;
public $message = 'This value is not a valid timezone{{ zone_message }}{{ country_code_message }}.';
public $message = 'This value is not a valid timezone.';

protected static $errorNames = array(
self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR',
Copy link
Contributor

Choose a reason for hiding this comment

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

missing few codes here

Expand All @@ -41,16 +40,10 @@ class Timezone extends Constraint
*/
public function __construct(array $options = null)
{
$this->zone = $options['zone'] ?? null;

if (isset($options['countryCode'])) {
if (\DateTimeZone::PER_COUNTRY !== $this->zone) {
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value');
}
parent::__construct($options);

$this->countryCode = $options['countryCode'];
if ($this->countryCode && \DateTimeZone::PER_COUNTRY !== $this->zone) {
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "zone" option has `\DateTimeZone::PER_COUNTRY` as value');
}

parent::__construct($options);
}
}
55 changes: 18 additions & 37 deletions src/Symfony/Component/Validator/Constraints/TimezoneValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,32 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

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

// @see: https://bugs.php.net/bug.php?id=75928
if ($constraint->countryCode) {
$timezoneIds = \DateTimeZone::listIdentifiers($zone, $constraint->countryCode);
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode);
} else {
$timezoneIds = \DateTimeZone::listIdentifiers($zone);
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone);
}

if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
if ($timezoneIds && !\in_array($value, $timezoneIds, true)) {
if ($constraint->countryCode) {
$code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR;
} elseif (null !== $constraint->zone) {
} elseif (\DateTimeZone::ALL !== $constraint->zone) {
$code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR;
} else {
$code = Timezone::NO_SUCH_TIMEZONE_ERROR;
}

$violation = $this->context->buildViolation($constraint->message);

foreach ($this->generateValidationMessage($constraint->zone, $constraint->countryCode) as $placeholder => $message) {
$violation->setParameter($placeholder, $message);
}

$violation
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode($code)
->addViolation()
;
->addViolation();
}
}

Expand All @@ -80,35 +73,23 @@ public function getDefaultOption()
}

/**
* Generates the replace parameters which are used in validation message.
*
* @param int|null $zone
* @param string|null $countryCode
*
* @return array
* {@inheritdoc}
*/
private function generateValidationMessage(int $zone = null, string $countryCode = null): array
protected function formatValue($value, $format = 0)
{
$values = array(
'{{ country_code_message }}' => '',
'{{ zone_message }}' => '',
);

if (null !== $zone) {
if (\DateTimeZone::PER_COUNTRY !== $zone) {
$value = parent::formatValue($value, $format);
if ($value) {
if (\DateTimeZone::PER_COUNTRY !== $value) {
$r = new \ReflectionClass(\DateTimeZone::class);
$consts = $r->getConstants();
if ($zoneFound = array_search($zone, $consts, true)) {
$values['{{ zone_message }}'] = ' at "'.$zoneFound.'" zone';
} else {
$values['{{ zone_message }}'] = ' at zone with identifier '.$zone;
if ($zoneFound = array_search($value, $consts, true)) {
return $zoneFound;
}
}
if ($countryCode) {
$values['{{ country_code_message }}'] = ' for ISO 3166-1 country code "'.$countryCode.'"';

return $value;
}
}

return $values;
return $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>This value should be a multiple of {{ compared_value }}.</target>
</trans-unit>
<trans-unit id="85">
<source>This value is not a valid timezone.</source>
<target>This value is not a valid timezone.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Este valor debería ser un múltiplo de {{ compared_value }}.</target>
</trans-unit>
<trans-unit id="85">
<source>This value is not a valid timezone.</source>
<target>Este valor no es una zona horaria válida.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getValidGroupedTimezones(): iterable
/**
* @dataProvider getInvalidTimezones
*/
public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMessage, string $countryCodeMessage)
public function testInvalidTimezonesWithoutZone(string $timezone)
{
$constraint = new Timezone(array(
'message' => 'myMessage',
Expand All @@ -115,25 +115,24 @@ public function testInvalidTimezonesWithoutZone(string $timezone, string $zoneMe
$this->validator->validate($timezone, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ zone_message }}', $zoneMessage)
->setParameter('{{ country_code_message }}', $countryCodeMessage)
->setParameter('{{ value }}', '"'.$timezone.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->assertRaised();
}

public function getInvalidTimezones(): iterable
{
return array(
array('Buenos_Aires/Argentina/America', '', ''),
array('Mayotte/Indian', '', ''),
array('foobar', '', ''),
array('Buenos_Aires/Argentina/America'),
array('Mayotte/Indian'),
array('foobar'),
);
}

/**
* @dataProvider getInvalidGroupedTimezones
*/
public function testInvalidGroupedTimezones(string $timezone, int $what, string $zoneMessage, string $countryCodeMessage)
public function testInvalidGroupedTimezones(string $timezone, int $what)
{
$constraint = new Timezone(array(
'zone' => $what,
Expand All @@ -143,20 +142,19 @@ public function testInvalidGroupedTimezones(string $timezone, int $what, string
$this->validator->validate($timezone, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ zone_message }}', $zoneMessage)
->setParameter('{{ country_code_message }}', $countryCodeMessage)
->setParameter('{{ value }}', '"'.$timezone.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR)
->assertRaised();
}

public function getInvalidGroupedTimezones(): iterable
{
return array(
array('Antarctica/McMurdo', \DateTimeZone::AMERICA, ' at "AMERICA" zone', ''),
array('America/Barbados', \DateTimeZone::ANTARCTICA, ' at "ANTARCTICA" zone', ''),
array('Europe/Kiev', \DateTimeZone::ARCTIC, ' at "ARCTIC" zone', ''),
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN, ' at "INDIAN" zone', ''),
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA, ' at zone with identifier 260', ''),
array('Antarctica/McMurdo', \DateTimeZone::AMERICA),
array('America/Barbados', \DateTimeZone::ANTARCTICA),
array('Europe/Kiev', \DateTimeZone::ARCTIC),
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN),
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN | \DateTimeZone::ANTARCTICA),
);
}

Expand Down Expand Up @@ -197,7 +195,7 @@ public function getValidGroupedTimezonesByCountry(): iterable
/**
* @dataProvider getInvalidGroupedTimezonesByCountry
*/
public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country, string $zoneMessage, string $countryCodeMessage)
public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what, string $country)
{
$constraint = new Timezone(array(
'message' => 'myMessage',
Expand All @@ -208,17 +206,16 @@ public function testInvalidGroupedTimezonesByCountry(string $timezone, int $what
$this->validator->validate($timezone, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ zone_message }}', $zoneMessage)
->setParameter('{{ country_code_message }}', $countryCodeMessage)
->setParameter('{{ value }}', '"'.$timezone.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR)
->assertRaised();
}

public function getInvalidGroupedTimezonesByCountry(): iterable
{
return array(
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR', '', ' for ISO 3166-1 country code "FR"'),
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT', '', ' for ISO 3166-1 country code "PT"'),
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR'),
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT'),
);
}

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

$this->buildViolation('myMessage')
->setParameter('{{ zone_message }}', '')
->setParameter('{{ country_code_message }}', '')
->setParameter('{{ value }}', '"'.$timezone.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->assertRaised();
}
Expand Down