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

Skip to content

[Validator] Add TimezoneValidator #22262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Renamed "timezone" group to "zone"
  • Loading branch information
phansys committed Nov 29, 2018
commit 49e4b51e62749f217b6b80a31c15fdc1e251e21f
10 changes: 5 additions & 5 deletions 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 $timezone = \DateTimeZone::ALL;
public $zone = \DateTimeZone::ALL;

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 All @@ -39,13 +39,13 @@ class Timezone extends Constraint
*/
public function __construct($options = null)
{
if (isset($options['timezone'])) {
$this->timezone = $options['timezone'];
if (isset($options['zone'])) {
Copy link
Contributor

@ro0NL ro0NL Jul 14, 2017

Choose a reason for hiding this comment

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

What about \DateTimeZone::ALL as a default, or, if countryCode is given default to \DateTimeZone::PER_COUNTRY instead. So we dont have to expect null :)

$this->zone = $options['zone'];
}

if (isset($options['countryCode'])) {
if (\DateTimeZone::PER_COUNTRY !== $this->timezone) {
throw new ConstraintDefinitionException('The option "countryCode" can only be used when "timezone" option has `\DateTimeZone::PER_COUNTRY` as value');
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');
}

$this->countryCode = $options['countryCode'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public function validate($value, Constraint $constraint)
}

$value = (string) $value;
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->timezone, $constraint->countryCode);
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode);

if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->timezone, $constraint->countryCode))
->setParameter('{{ extra_info }}', $this->formatExtraInfo($constraint->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.

This is a weird parameter, and binds localized text to this constraint validator... 👎 for that.

What about specialized params instead like zone_id / zone_name or so?

->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->addViolation();
}
Expand All @@ -55,28 +55,28 @@ public function validate($value, Constraint $constraint)
*/
public function getDefaultOption()
{
return 'timezone';
return 'zone';
}

/**
* Format the extra info which is appended to validation message based on
* constraint options
*
* @param int $timezone
* @param int $zone
* @param string|null $countryCode
*
* @return string
*/
protected function formatExtraInfo($timezone, $countryCode = null)
protected function formatExtraInfo($zone, $countryCode = null)
{
if (!$timezone) {
if (!$zone) {
return '';
}
$r = new \ReflectionClass('\DateTimeZone');
$consts = $r->getConstants();

if (!$value = array_search($timezone, $consts, true)) {
$value = $timezone;
if (!$value = array_search($zone, $consts, true)) {
$value = $zone;
}

$value = ' for "'.$value.'" zone';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public function testValidTimezoneConstraints()

$constraint = new Timezone(array(
'message' => 'myMessage',
'timezone' => \DateTimeZone::PER_COUNTRY,
'zone' => \DateTimeZone::PER_COUNTRY,
'countryCode' => 'AR',
));

$constraint = new Timezone(array(
'message' => 'myMessage',
'timezone' => \DateTimeZone::ALL,
'zone' => \DateTimeZone::ALL,
));
}

Expand All @@ -42,7 +42,7 @@ public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone()
{
$constraint = new Timezone(array(
'message' => 'myMessage',
'timezone' => \DateTimeZone::ALL,
'zone' => \DateTimeZone::ALL,
'countryCode' => 'AR',
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getValidTimezones()
public function testValidGroupedTimezones($timezone, $what)
{
$constraint = new Timezone(array(
'timezone' => $what,
'zone' => $what,
));

$this->validator->validate($timezone, $constraint);
Expand Down Expand Up @@ -135,7 +135,7 @@ public function getInvalidTimezones()
public function testInvalidGroupedTimezones($timezone, $what, $extraInfo)
{
$constraint = new Timezone(array(
'timezone' => $what,
'zone' => $what,
'message' => 'myMessage',
));

Expand Down Expand Up @@ -164,7 +164,7 @@ public function getInvalidGroupedTimezones()
public function testValidGroupedTimezonesByCountry($timezone, $what, $country)
{
$constraint = new Timezone(array(
'timezone' => $what,
'zone' => $what,
'countryCode' => $country,
));

Expand Down Expand Up @@ -199,7 +199,7 @@ public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country,
{
$constraint = new Timezone(array(
'message' => 'myMessage',
'timezone' => $what,
'zone' => $what,
'countryCode' => $country,
));

Expand Down