10000 [Validator] Add `TimezoneValidator` by phansys · Pull Request #22262 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for \DateTimeZone::PER_COUNTRY
  • Loading branch information
phansys committed Nov 29, 2018
commit 6cceda0ac17270cb1e97831a2f63353d14e1d58e
17 changes: 14 additions & 3 deletions src/Symfony/Component/Validator/Constraints/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;

/**
* @Annotation
Expand All @@ -23,7 +24,9 @@ class Timezone extends Constraint
{
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';

public $value = \DateTimeZone::ALL;
public $timezone = \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;

public $message = 'This value is not a valid timezone at {{ timezone_group }}.';

Expand All @@ -36,8 +39,16 @@ class Timezone extends Constraint
*/
public function __construct($options = null)
{
if (isset($options['value'])) {
$this->value = $options['value'];
if (isset($options['timezone'])) {
$this->timezone = $options['timezone'];
}

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

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

parent::__construct($options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function validate($value, Constraint $constraint)
}

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

if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
$this->context->buildViolation($constraint->message)
Expand All @@ -55,6 +55,6 @@ public function validate($value, Constraint $constraint)
*/
public function getDefaultOption()
{
return 'value';
return 'timezone';
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/TimezoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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',
'timezone' => \DateTimeZone::PER_COUNTRY,
'countryCode' => 'AR',
));

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

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExceptionForGroupedTimezonesByCountryWithWrongTimezone()
{
$constraint = new Timezone(array(
'message' => 'myMessage',
'timezone' => \DateTimeZone::ALL,
'countryCode' => 'AR',
));
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testExceptionForGroupedTimezonesByCountryWithoutTimezone()
{
$constraint = new Timezone(array(
'message' => 'myMessage',
'countryCode' => 'AR',
));
}
}
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(
'value' => $what,
'timezone' => $what,
));

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

Expand All @@ -156,4 +156,65 @@ public function getInvalidGroupedTimezones()
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN),
);
}

/**
* @dataProvider getValidGroupedTimezonesByCountry
*/
public function testValidGroupedTimezonesByCountry($timezone, $what, $country)
{
$constraint = new Timezone(array(
'timezone' => $what,
'countryCode' => $country,
));

$this->validator->validate($timezone, $constraint);

$this->assertNoViolation();
}

public function getValidGroupedTimezonesByCountry()
{
return array(
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'AR'),
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'BB'),
array('Africa/Cairo', \DateTimeZone::PER_COUNTRY, 'EG'),
array('Atlantic/Cape_Verde', \DateTimeZone::PER_COUNTRY, 'CV'),
array('Europe/Bratislava', \DateTimeZone::PER_COUNTRY, 'SK'),
array('Indian/Christmas', \DateTimeZone::PER_COUNTRY, 'CX'),
array('Pacific/Kiritimati', \DateTimeZone::PER_COUNTRY, 'KI'),
array('Pacific/Kiritimati', \DateTimeZone::PER_COUNTRY, 'KI'),
array('Pacific/Kiritimati', \DateTimeZone::PER_COUNTRY, 'KI'),
array('Arctic/Longyearbyen', \DateTimeZone::PER_COUNTRY, 'SJ'),
array('Asia/Beirut', \DateTimeZone::PER_COUNTRY, 'LB'),
array('Atlantic/Bermuda', \DateTimeZone::PER_COUNTRY, 'BM'),
array('Atlantic/Azores', \DateTimeZone::PER_COUNTRY, 'PT'),
);
}

/**
* @dataProvider getInvalidGroupedTimezonesByCountry
*/
public function testInvalidGroupedTimezonesByCountry($timezone, $what, $country)
{
$constraint = new Timezone(array(
'message' => 'myMessage',
'timezone' => $what,
'countryCode' => $country,
));

$this->validator->validate($timezone, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ timezone_group }}', '"'.$timezone.'"')
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->assertRaised();
}

public function getInvalidGroupedTimezonesByCountry()
{
return array(
array('America/Argentina/Cordoba', \DateTimeZone::PER_COUNTRY, 'FR'),
array('America/Barbados', \DateTimeZone::PER_COUNTRY, 'PT'),
);
}
}
0