Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Timezone.php
@@ -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 src/Symfony/Component/Validator/Constraints/TimezoneValidator.php
@@ -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();
}
}
}
Expand Up @@ -306,6 +306,10 @@
<source>The host could not be resolved.</source>
<target>The host could not be resolved.</target>
</trans-unit>
<trans-unit id="80">
Copy link
Member

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.

<source>"{{ value }}" is not a valid timezone.</source>
<target>"{{ value }}" is not a valid timezone.</target>
</trans-unit>
</body>
</file>
</xliff>
Expand Up @@ -306,6 +306,10 @@
<source>The host could not be resolved.</source>
<target>Le nom de domaine n'a pas pu être résolu.</target>
</trans-unit>
<trans-unit id="80">
<source>"{{ value }}" is not a valid timezone.</source>
<target>"{{ value }}" n'est pas un fuseau horaire valide.</target>
</trans-unit>
</body>
</file>
</xliff>
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing carriage return before namespace keyword and license header section.

Copy link
Member

Choose a reason for hiding this comment

The 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'),
);
}
}