mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Better exceptions (component exceptions separate from validation exceptions), date default timezone on bootstrap, Date\Between validator
This commit is contained in:
parent
f6871cbc7d
commit
666c0d5330
|
@ -0,0 +1,4 @@
|
|||
== About
|
||||
|
||||
Respect\Validation aims to be the most awesome validation toolkit ever created
|
||||
.
|
8
library/Respect/Validation/ComponentException.php
Normal file
8
library/Respect/Validation/ComponentException.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
class ComponentException extends \Exception
|
||||
{
|
||||
|
||||
}
|
30
library/Respect/Validation/Date/AbstractDateValidator.php
Normal file
30
library/Respect/Validation/Date/AbstractDateValidator.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
use DateTime;
|
||||
|
||||
abstract class AbstractDateValidator
|
||||
{
|
||||
|
||||
protected $format=DateTime::RFC1036;
|
||||
|
||||
protected function setFormat($format=DateTime::RFC1036)
|
||||
{
|
||||
$this->format = $format;
|
||||
}
|
||||
|
||||
protected function getDateObject($date)
|
||||
{
|
||||
if ($date instanceof DateTime)
|
||||
return $date;
|
||||
else
|
||||
return new DateTime($date);
|
||||
}
|
||||
|
||||
protected function formatDate(DateTime $date)
|
||||
{
|
||||
return $date->format($this->format);
|
||||
}
|
||||
|
||||
}
|
55
library/Respect/Validation/Date/Between.php
Normal file
55
library/Respect/Validation/Date/Between.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\ComponentException;
|
||||
|
||||
class Between extends AbstractDateValidator implements Validatable
|
||||
{
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
|
||||
public function __construct($min, $max, $format=null)
|
||||
{
|
||||
|
||||
$this->min = $this->getDateObject($min);
|
||||
$this->max = $this->getDateObject($max);
|
||||
if ($this->min > $this->max)
|
||||
throw new ComponentException(
|
||||
sprintf('%s cannot be less than %s for validation', $this->formatDate($this->min), $this->formatDate($this->max))
|
||||
);
|
||||
if (!is_null($format))
|
||||
$this->setFormat($format);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$target = $this->getDateObject($input);
|
||||
if (!$this->isValid($target))
|
||||
throw new OutOfBoundsException(
|
||||
sprintf(
|
||||
'%s is not between %s and %s.', $this->formatDate($target), $this->formatDate($this->min), $this->formatDate($this->max)
|
||||
)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid($input)
|
||||
{
|
||||
$target = $this->getDateObject($input);
|
||||
return $target >= $this->min and $target <= $this->max;
|
||||
}
|
||||
|
||||
public function getMessages()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function setMessages(array $messages)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
8
library/Respect/Validation/Date/OutOfBoundsException.php
Normal file
8
library/Respect/Validation/Date/OutOfBoundsException.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
class OutOfBoundsException extends \OutOfBoundsException
|
||||
{
|
||||
|
||||
}
|
|
@ -2,10 +2,7 @@
|
|||
|
||||
namespace Respect\Validation;
|
||||
|
||||
use OutOfRangeException;
|
||||
use LogicException;
|
||||
use ReflectionClass;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Validator implements Validatable
|
||||
{
|
||||
|
@ -28,7 +25,7 @@ class Validator implements Validatable
|
|||
return;
|
||||
}
|
||||
if (is_object($validator))
|
||||
throw new InvalidArgumentException(
|
||||
throw new ComponentException(
|
||||
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators', get_class($validator))
|
||||
);
|
||||
$validatorFqn = explode('\\', get_called_class());
|
||||
|
@ -40,7 +37,7 @@ class Validator implements Validatable
|
|||
'Respect\Validation\Validatable'
|
||||
);
|
||||
if (!$implementedInterface)
|
||||
throw new InvalidArgumentException(
|
||||
throw new ComponentException(
|
||||
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators', $validatorFqn)
|
||||
);
|
||||
if ($validatorClass->hasMethod('__construct')) {
|
||||
|
@ -78,7 +75,7 @@ class Validator implements Validatable
|
|||
} else {
|
||||
$validatorName = $k;
|
||||
if (!empty($v) && !is_array($v))
|
||||
throw new LogicException(
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'Arguments for array-specified validators must be an array, you provided %s', $v
|
||||
)
|
||||
|
@ -153,7 +150,7 @@ class Validator implements Validatable
|
|||
public function setMessages(array $messages)
|
||||
{
|
||||
if (count($this->messages) != count($messages))
|
||||
throw new OutOfRangeException(
|
||||
throw new ComponentException(
|
||||
'You must set exactly the same amount of messages currently present in the validator'
|
||||
);
|
||||
$this->messages = $messages;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
|
||||
date_default_timezone_set('America/Sao_Paulo');
|
||||
set_include_path(get_include_path() . PATH_SEPARATOR . '../library/'. PATH_SEPARATOR . './library/');
|
||||
require_once 'SplClassLoader.php';
|
||||
$respectLoader = new \SplClassLoader();
|
||||
|
|
68
tests/library/Respect/Validation/Date/BetweenTest.php
Normal file
68
tests/library/Respect/Validation/Date/BetweenTest.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class BetweenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidDates
|
||||
*/
|
||||
public function testBetweenValid($input, $min, $max)
|
||||
{
|
||||
$between = new Between($min, $max);
|
||||
$this->assertTrue($between->isValid($input));
|
||||
$this->assertTrue($between->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidDates
|
||||
* @expectedException Respect\Validation\Date\OutOfBoundsException
|
||||
*/
|
||||
public function testBetweenInvalid($input, $min, $max)
|
||||
{
|
||||
|
||||
$between = new Between($min, $max);
|
||||
$this->assertFalse($between->isValid($input));
|
||||
$this->assertFalse($between->validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidConfigs
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testInvalidConfigs($min, $max)
|
||||
{
|
||||
|
||||
$between = new Between($min, $max);
|
||||
}
|
||||
|
||||
public function providerForValidDates()
|
||||
{
|
||||
return array(
|
||||
array('now', 'yesterday', 'tomorrow'),
|
||||
array('now', 'now', 'tomorrow'),
|
||||
array('now', 'yesterday', 'now'),
|
||||
array('now', 'now', 'now'),
|
||||
array(new DateTime(), 'yesterday', 'tomorrow'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidDates()
|
||||
{
|
||||
return array(
|
||||
array('now', 'next month', 'next year'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidConfigs()
|
||||
{
|
||||
|
||||
return array(
|
||||
array('tomorrow', 'yesterday'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -27,7 +27,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testAddNonValidator()
|
||||
{
|
||||
|
@ -35,7 +35,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testAddNonValidator2()
|
||||
{
|
||||
|
@ -75,7 +75,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException LogicException
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testBuildValidatorsInvalid()
|
||||
{
|
||||
|
@ -203,7 +203,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
* @expectedException OutOfRangeException
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testSetInvalidMessages($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue