-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
4b87fb7
6cceda0
6124ef6
49e4b51
288a413
7545275
de78802
dfa4f98
6be950a
6d23035
13bc629
0491875
14689f7
c3dce5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
public $countryCode; | ||
|
||
|
@@ -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'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about |
||
$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']; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) | ||
->addViolation(); | ||
} | ||
|
@@ -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'; | ||
|
There was a problem hiding this comment.
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