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
[Validator] - Added Timezone Constraint Validator #13270
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,26 @@ | ||
<?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; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"PROPERTY", "METHOD", "ANNOTATION"}) | ||
* | ||
* @author Mickaël Andrieu <andrieu.travail@gmail.com> | ||
* | ||
*/ | ||
class Timezone extends Constraint | ||
{ | ||
public $message = '"{{ value }}" is not a valid timezone.'; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Validator/Constraints/TimezoneValidator.php
This file contains 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,47 @@ | ||
<?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; | ||
|
||
/** | ||
* @author Mickaël Andrieu <andrieu.travail@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'); | ||
} | ||
|
||
if (!in_array($value, \DateTimeZone::listIdentifiers(), true)) { | ||
$this->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->addViolation(); | ||
} | ||
} | ||
} |
This file contains 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 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
94 changes: 94 additions & 0 deletions
94
src/Symfony/Component/Validator/Tests/Constraints/TimezoneValidatorTest.php
This file contains 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,94 @@ | ||
<?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; | ||
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. Missing carriage return before 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. and missing the license block |
||
|
||
use Symfony\Component\Validator\Constraints\Timezone; | ||
use Symfony\Component\Validator\Constraints\TimezoneValidator; | ||
use Symfony\Component\Validator\Validation; | ||
|
||
/** | ||
* @author Mickaël Andrieu <andrieu.travail@gmail.com> | ||
*/ | ||
class TimezoneValidatorTest extends AbstractConstraintValidatorTest | ||
{ | ||
protected function getApiVersion() | ||
{ | ||
return Validation::API_VERSION_2_5; | ||
} | ||
|
||
protected function createValidator() | ||
{ | ||
return new TimezoneValidator(); | ||
} | ||
|
||
public function testEuropeParisIsValid() | ||
{ | ||
$this->validator->validate('Europe/Paris', new Timezone()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testAmericaIndianaIndianapolisIsValid() | ||
{ | ||
$this->validator->validate('America/Indiana/Indianapolis', new Timezone()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testNullIsValid() | ||
{ | ||
$this->validator->validate(null, new Timezone()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
public function testEmptyStringIsValid() | ||
{ | ||
$this->validator->validate('', new Timezone()); | ||
|
||
$this->assertNoViolation(); | ||
} | ||
|
||
/** | ||
* @dataProvider getInvalidValues | ||
*/ | ||
public function testInvalidValues($value, $valueAsString) | ||
{ | ||
$constraint = new Timezone(array( | ||
'message' => 'myMessage', | ||
)); | ||
|
||
$this->validator->validate($value, $constraint); | ||
|
||
$this->buildViolation('myMessage') | ||
->setParameter('{{ value }}', $valueAsString) | ||
->assertRaised(); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException | ||
*/ | ||
public function testExpectsStringCompatibleType() | ||
{ | ||
$this->validator->validate(new \stdClass(), new Timezone()); | ||
} | ||
|
||
public function getInvalidValues() | ||
{ | ||
return array( | ||
array('foobar', '"foobar"'), | ||
array(0, '0'), | ||
array(false, 'false'), | ||
array(1234, '1234'), | ||
); | ||
} | ||
} |
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.
The id will need to become
81
.