-
-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4b87fb7
Add `TimeZoneValidator`
phansys 6cceda0
Add support for `\DateTimeZone::PER_COUNTRY`
phansys 6124ef6
Update constraint message
phansys 49e4b51
Renamed "timezone" group to "zone"
phansys 288a413
Apply CS fixes from `fabbot`
phansys 7545275
Update method visibility at `TimezoneValidator`
phansys de78802
Update validation messages
phansys dfa4f98
Update way of handling `zone` option
phansys 6be950a
Add specific errors to be used when `zone` or `countryCode` are provided
phansys 6d23035
Add support for deprecated timezones
phansys 13bc629
Remove surplus quotes in validation message
phansys 0491875
Update validation message
phansys 14689f7
Update `TimezoneValidator`
phansys c3dce5c
Add simple assertion at `TimezoneTest::testValidTimezoneConstraints()`
phansys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | ||
* | ||
* @author Javier Spagnoletti <phansys@gmail.com> | ||
*/ | ||
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 = \DateTimeZone::ALL; | ||
public $countryCode; | ||
public $message = 'This value is not a valid timezone.'; | ||
|
||
protected static $errorNames = array( | ||
self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR', | ||
self::NO_SUCH_TIMEZONE_IN_ZONE_ERROR => 'NO_SUCH_TIMEZONE_IN_ZONE_ERROR', | ||
self::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR => 'NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR', | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(array $options = null) | ||
{ | ||
parent::__construct($options); | ||
|
||
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'); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
src/Symfony/Component/Validator/Constraints/TimezoneValidator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
/** | ||
* Validates whether a value is a valid timezone identifier. | ||
* | ||
* @author Javier Spagnoletti <phansys@gmail.com> | ||
*/ | ||
class TimezoneValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value, Constraint $constraint) | ||
{ | ||
if (!$constraint instanceof Timezone) { | ||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Timezone'); | ||
} | ||
|
||
if (null === $value || '' === $value) { | ||
return; | ||
} | ||
|
||
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { | ||
throw new UnexpectedTypeException($value, 'string'); | ||
} | ||
|
||
$value = (string) $value; | ||
|
||
// @see: https://bugs.php.net/bug.php?id=75928 | ||
if ($constraint->countryCode) { | ||
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode); | ||
} else { | ||
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->zone); | ||
} | ||
|
||
if ($timezoneIds && !\in_array($value, $timezoneIds, true)) { | ||
if ($constraint->countryCode) { | ||
$code = Timezone::NO_SUCH_TIMEZONE_IN_COUNTRY_ERROR; | ||
} elseif (\DateTimeZone::ALL !== $constraint->zone) { | ||
$code = Timezone::NO_SUCH_TIMEZONE_IN_ZONE_ERROR; | ||
} else { | ||
$code = Timezone::NO_SUCH_TIMEZONE_ERROR; | ||
} | ||
|
||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode($code) | ||
->addViolation(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultOption() | ||
{ | ||
return 'zone'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function formatValue($value, $format = 0) | ||
{ | ||
$value = parent::formatValue($value, $format); | ||
if ($value) { | ||
if (\DateTimeZone::PER_COUNTRY !== $value) { | ||
$r = new \ReflectionClass(\DateTimeZone::class); | ||
$consts = $r->getConstants(); | ||
if ($zoneFound = array_search($value, $consts, true)) { | ||
return $zoneFound; | ||
} | ||
|
||
return $value; | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Constraints; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Validator\Constraints\Timezone; | ||
|
||
/** | ||
* @author Javier Spagnoletti <phansys@gmail.com> | ||
*/ | ||
class TimezoneTest extends TestCase | ||
{ | ||
public function testValidTimezoneConstraints() | ||
{ | ||
$constraint = new Timezone(); | ||
|
||
$constraint = new Timezone(array( | ||
'message' => 'myMessage', | ||
'zone' => \DateTimeZone::PER_COUNTRY, | ||
'countryCode' => 'AR', | ||
)); | ||
|
||
$constraint = new Timezone(array( | ||
'message' => 'myMessage', | ||
'zone' => \DateTimeZone::ALL, | ||
)); | ||
|
||
// Make an assertion in order to avoid this test to be marked as risky | ||
$this->assertInstanceOf(Timezone::class, $constraint); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException | ||
*/ | ||
public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone() | ||
{ | ||
$constraint = new Timezone(array( | ||
'message' => 'myMessage', | ||
'zone' => \DateTimeZone::ALL, | ||
'countryCode' => 'AR', | ||
)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException | ||
*/ | ||
public function testExceptionForGroupedTimezonesByCountryWithoutTimezone() | ||
{ | ||
$constraint = new Timezone(array( | ||
'message' => 'myMessage', | ||
'countryCode' => 'AR', | ||
)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
missing few codes here