Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 171e4b9

Browse filesBrowse files
committed
Add TimeZoneValidator
1 parent 47740ce commit 171e4b9
Copy full SHA for 171e4b9

File tree

3 files changed

+266
-0
lines changed
Filter options

3 files changed

+266
-0
lines changed
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Javier Spagnoletti <phansys@gmail.com>
21+
*/
22+
class TimeZone extends Constraint
23+
{
24+
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';
25+
26+
public $value;
27+
28+
public $message = 'This value is not a valid timezone at {{ timezone_group }}.';
29+
30+
protected static $errorNames = array(
31+
self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR',
32+
);
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function __construct($options = null)
38+
{
39+
if (isset($options['value'])) {
40+
$this->value = $options['value'];
41+
} else {
42+
$this->value = \DateTimeZone::ALL;
43+
}
44+
45+
parent::__construct($options);
46+
}
47+
}
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* Validates whether a value is a valid timezone identifier.
20+
*
21+
* @author Javier Spagnoletti <phansys@gmail.com>
22+
*/
23+
class TimeZoneValidator extends ConstraintValidator
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function validate($value, Constraint $constraint)
29+
{
30+
if (!$constraint instanceof TimeZone) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\TimeZone');
32+
}
33+
34+
if (null === $value || '' === $value) {
35+
return;
36+
}
37+
38+
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
39+
throw new UnexpectedTypeException($value, 'string');
40+
}
41+
42+
$value = (string) $value;
43+
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->value);
44+
45+
if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
46+
$this->context->buildViolation($constraint->message)
47+
->setParameter('{{ timezone_group }}', $this->formatValue($value))
48+
->setCode(TimeZone::NO_SUCH_TIMEZONE_ERROR)
49+
->addViolation();
50+
}
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function getDefaultOption()
57+
{
58+
return 'value';
59+
}
60+
}
+159Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\TimeZone;
15+
use Symfony\Component\Validator\Constraints\TimeZoneValidator;
16+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17+
18+
/**
19+
* @author Javier Spagnoletti <phansys@gmail.com>
20+
*/
21+
class TimeZoneValidatorTest extends ConstraintValidatorTestCase
22+
{
23+
protected function createValidator()
24+
{
25+
return new TimeZoneValidator();
26+
}
27+
28+
public function testNullIsValid()
29+
{
30+
$this->validator->validate(null, new TimeZone());
31+
32+
$this->assertNoViolation();
33+
}
34+
35+
public function testEmptyStringIsValid()
36+
{
37+
$this->validator->validate('', new TimeZone());
38+
39+
$this->assertNoViolation();
40+
}
41+
42+
/**
43+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
44+
*/
45+
public function testExpectsStringCompatibleType()
46+
{
47+
$this->validator->validate(new \stdClass(), new TimeZone());
48+
}
49+
50+
/**
51+
* @dataProvider getValidTimeZones
52+
*/
53+
public function testValidTimeZones($timezone)
54+
{
55+
$this->validator->validate($timezone, new TimeZone());
56+
57+
$this->assertNoViolation();
58+
}
59+
60+
public function getValidTimeZones()
61+
{
62+
return array(
63+
array('America/Argentina/Buenos_Aires'),
64+
array('America/Barbados'),
65+
array('Antarctica/Syowa'),
66+
array('Africa/Douala'),
67+
array('Atlantic/Canary'),
68+
array('Asia/Gaza'),
69+
array('Europe/Copenhagen'),
70+
);
71+
}
72+
73+
/**
74+
* @dataProvider getValidGroupedTimeZones
75+
*/
76+
public function testValidGroupedTimeZones($timezone, $what)
77+
{
78+
$constraint = new TimeZone(array(
79+
'value' => $what,
80+
));
81+
82+
$this->validator->validate($timezone, $constraint);
83+
84+
$this->assertNoViolation();
85+
}
86+
87+
public function getValidGroupedTimeZones()
88+
{
89+
return array(
90+
array('America/Argentina/Cordoba', \DateTimeZone::AMERICA),
91+
array('America/Barbados', \DateTimeZone::AMERICA),
92+
array('Africa/Cairo', \DateTimeZone::AFRICA),
93+
array('Atlantic/Cape_Verde', \DateTimeZone::ATLANTIC),
94+
array('Europe/Bratislava', \DateTimeZone::EUROPE),
95+
array('Indian/Christmas', \DateTimeZone::INDIAN),
96+
array('Pacific/Kiritimati', \DateTimeZone::ALL),
97+
array('Pacific/Kiritimati', \DateTimeZone::ALL_WITH_BC),
98+
array('Pacific/Kiritimati', \DateTimeZone::PACIFIC),
99+
array('Arctic/Longyearbyen', \DateTimeZone::ARCTIC),
100+
array('Asia/Beirut', \DateTimeZone::ASIA),
101+
array('Atlantic/Bermuda', \DateTimeZone::ASIA | \DateTimeZone::ATLANTIC),
102+
array('Atlantic/Azores', \DateTimeZone::ATLANTIC | \DateTimeZone::ASIA),
103+
);
104+
}
105+
106+
/**
107+
* @dataProvider getInvalidTimeZones
108+
*/
109+
public function testInvalidTimeZones($timezone)
110+
{
111+
$constraint = new TimeZone(array(
112+
'message' => 'myMessage',
113+
));
114+
115+
$this->validator->validate($timezone, $constraint);
116+
117+
$this->buildViolation('myMessage')
118+
->setParameter('{{ timezone_group }}', '"'.$timezone.'"')
119+
->setCode(TimeZone::NO_SUCH_TIMEZONE_ERROR)
120+
->assertRaised();
121+
}
122+
123+
public function getInvalidTimeZones()
124+
{
125+
return array(
126+
array('Buenos_Aires/Argentina/America'),
127+
array('Mayotte/Indian'),
128+
array('foobar'),
129+
);
130+
}
131+
132+
/**
133+
* @dataProvider getInvalidGroupedTimeZones
134+
*/
135+
public function testInvalidGroupedTimeZones($timezone, $what)
136+
{
137+
$constraint = new TimeZone(array(
138+
'value' => $what,
139+
'message' => 'myMessage',
140+
));
141+
142+
$this->validator->validate($timezone, $constraint);
143+
144+
$this->buildViolation('myMessage')
145+
->setParameter('{{ timezone_group }}', '"'.$timezone.'"')
146+
->setCode(TimeZone::NO_SUCH_TIMEZONE_ERROR)
147+
->assertRaised();
148+
}
149+
150+
public function getInvalidGroupedTimeZones()
151+
{
152+
return array(
153+
array('Antarctica/McMurdo', \DateTimeZone::AMERICA),
154+
array('America/Barbados', \DateTimeZone::ANTARCTICA),
155+
array('Europe/Kiev', \DateTimeZone::ARCTIC),
156+
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN),
157+
);
158+
}
159+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.