Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
Next Next commit
Add TimeZoneValidator
  • Loading branch information
phansys committed Nov 29, 2018
commit 4b87fb778d7aecbb7ef8e682da7df9f7f32d0c94
45 changes: 45 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Timezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 Javier Spagnoletti <[email protected]>
*/
class Timezone extends Constraint
{
const NO_SUCH_TIMEZONE_ERROR = '45de6628-3479-46d6-a210-00ad584f530a';

public $value = \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 $message = 'This value is not a valid timezone at {{ timezone_group }}.';

protected static $errorNames = array(
self::NO_SUCH_TIMEZONE_ERROR => 'NO_SUCH_TIMEZONE_ERROR',
Copy link
Contributor

Choose a reason for hiding this comment

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

missing few codes here

);

/**
* {@inheritdoc}
*/
public function __construct($options = null)
{
if (isset($options['value'])) {
$this->value = $options['value'];
}

parent::__construct($options);
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Component/Validator/Constraints/TimezoneValidator.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 <[email protected]>
*
* 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 <[email protected]>
*/
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;
$timezoneIds = \DateTimeZone::listIdentifiers($constraint->value);

if ($timezoneIds && !in_array($value, $timezoneIds, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ timezone_group }}', $this->formatValue($value))
->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR)
->addViolation();
}
}

/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'value';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 Symfony\Component\Validator\Constraints\Timezone;
use Symfony\Component\Validator\Constraints\TimezoneValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @author Javier Spagnoletti <[email protected]>
*/
class TimezoneValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
{
return new TimezoneValidator();
}

public function testNullIsValid()
{
$this->validator->validate(null, new Timezone());

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new Timezone());

$this->assertNoViolation();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new Timezone());
}

/**
* @dataProvider getValidTimezones
*/
public function testValidTimezones($timezone)
{
$this->validator->validate($timezone, new Timezone());

$this->assertNoViolation();
}

public function getValidTimezones()
{
return array(
array('America/Argentina/Buenos_Aires'),
array('America/Barbados'),
array('Antarctica/Syowa'),
array('Africa/Douala'),
array('Atlantic/Canary'),
array('Asia/Gaza'),
array('Europe/Copenhagen'),
);
}

/**
* @dataProvider getValidGroupedTimezones
*/
public function testValidGroupedTimezones($timezone, $what)
{
$constraint = new Timezone(array(
'value' => $what,
));

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

$this->assertNoViolation();
}

public function getValidGroupedTimezones()
{
return array(
array('America/Argentina/Cordoba', \DateTimeZone::AMERICA),
array('America/Barbados', \DateTimeZone::AMERICA),
array('Africa/Cairo', \DateTimeZone::AFRICA),
array('Atlantic/Cape_Verde', \DateTimeZone::ATLANTIC),
array('Europe/Bratislava', \DateTimeZone::EUROPE),
array('Indian/Christmas', \DateTimeZone::INDIAN),
array('Pacific/Kiritimati', \DateTimeZone::ALL),
array('Pacific/Kiritimati', \DateTimeZone::ALL_WITH_BC),
array('Pacific/Kiritimati', \DateTimeZone::PACIFIC),
array('Arctic/Longyearbyen', \DateTimeZone::ARCTIC),
array('Asia/Beirut', \DateTimeZone::ASIA),
array('Atlantic/Bermuda', \DateTimeZone::ASIA | \DateTimeZone::ATLANTIC),
array('Atlantic/Azores', \DateTimeZone::ATLANTIC | \DateTimeZone::ASIA),
);
}

/**
* @dataProvider getInvalidTimezones
*/
public function testInvalidTimezones($timezone)
{
$constraint = new Timezone(array(
'message' => 'myMessage',
));

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

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

public function getInvalidTimezones()
{
return array(
array('Buenos_Aires/Argentina/America'),
array('Mayotte/Indian'),
array('foobar'),
);
}

/**
* @dataProvider getInvalidGroupedTimezones
*/
public function testInvalidGroupedTimezones($timezone, $what)
{
$constraint = new Timezone(array(
'value' => $what,
'message' => 'myMessage',
));

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

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

public function getInvalidGroupedTimezones()
{
return array(
array('Antarctica/McMurdo', \DateTimeZone::AMERICA),
array('America/Barbados', \DateTimeZone::ANTARCTICA),
array('Europe/Kiev', \DateTimeZone::ARCTIC),
array('Asia/Ho_Chi_Minh', \DateTimeZone::INDIAN),
);
}
}