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

add Validatore DateCompare #1

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
176 changes: 176 additions & 0 deletions library/Zle/Validate/DateCompare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

/**
* Zend Library Extension
*
* PHP version 5
*
* @category Zle
* @package Zle_Validate
* @author Fabio Napoleoni <f.napoleoni@gmail.com>
* @license http://framework.zend.com/license/new-bsd New BSD License
* @link http://framework.zend.com/
*/

/**
* Zle_Validate_DateCompare
*
* Examples of use:
* $element->addValidator(new My_Validate_DateCompare('startdate')); //exact match
* $element->addValidator(new My_Validate_DateCompare('startdate','enddate')); //between dates
* $element->addValidator(new My_Validate_DateCompare('startdate',true)); //not later
* $element->addValidator(new My_Validate_DateCompare('startdate',false)); //not earlier
*
* @category Zle
* @package Zle_Validate
* @author Andrea Giannantonio <a.giannantonio@gmail.com>
* @license http://framework.zend.com/license/new-bsd New BSD License
* @link http://framework.zend.com/
*/
class Zle_Validate_DateCompare extends Zend_Validate_Abstract
{
/**
* Error codes
* @const string
*/
const NOT_SAME = 'notSame';
const MISSING_TOKEN = 'missingToken';
const NOT_LATER = 'notLater';
const NOT_EARLIER = 'notEarlier';
const NOT_BETWEEN = 'notBetween';

/**
* Error messages
* @var array
*/
protected $_messageTemplates = array(
self::NOT_SAME => "The date '%token%' does not match the given '%value%'",
self::NOT_BETWEEN => "The date '%value%' is not in the valid range '%token%' - '%compare%'",
self::NOT_LATER => "The date '%token%' is not later than '%value%'",
self::NOT_EARLIER => "The date '%token%' is not earlier than '%value%'",
self::MISSING_TOKEN => "No date was provided to match against '%token%'",
);

/** @var array */
protected $_messageVariables = array(
'token' => '_tokenString',
'compare' => '_compareString'
);

/**
* Original token against which to validate
* @var string
*/
protected $_tokenString;
protected $_compareString;
protected $_token;
protected $_compare;

/**
* Sets validator options
*
* @param mixed $token
* @param mixed $compare
*/
public function __construct($token = null, $compare = true)
{
if (null !== $token) {
$this->setToken($token);
$this->setCompare($compare);
}
}

/**
* Set token against which to compare
*
* @param mixed $token
* @return Zend_Validate_Identical
*/
public function setToken($token)
{
$this->_tokenString = (string)$token;
$this->_token = $token;
return $this;
}

/**
* Retrieve token
*
* @return string
*/
public function getToken()
{
return $this->_token;
}

/**
* Set compare against which to compare
*
* @param mixed $compare
* @return Zend_Validate_Identical
*/
public function setCompare($compare)
{
$this->_compareString = (string)$compare;
$this->_compare = $compare;
return $this;
}

/**
* Retrieve compare
*
* @return string
*/
public function getCompare()
{
return $this->_compare;
}

/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if a token has been set and the provided value
* matches that token.
*
* @param mixed $value
* @return boolean
*/
public function isValid($value)
{
$this->_setValue((string)$value);
$token = $this->getToken();

if ($token === null) {
$this->_error(self::MISSING_TOKEN);
return false;
}
$date1 = new Zend_Date($value);
$date2 = new Zend_Date($token);
if ($this->getCompare() === true) {

if ($date1->compare($date2) < 0 || $date1->equals($date2)) {

$this->_error(self::NOT_LATER);
return false;
}
} else if ($this->getCompare() === false) {
if ($date1->compare($date2) > 0 || $date1->equals($date2)) {
$this->_error(self::NOT_EARLIER);
return false;
}
} else if ($this->getCompare() === null) {
if (!$date1->equals($date2)) {
$this->_error(self::NOT_SAME);
return false;
}
} else {
$date3 = new Zend_Date($this->getCompare());
if ($date1->compare($date2) < 0 || $date1->compare($date3) > 0) {
$this->_error(self::NOT_BETWEEN);
return false;
}
}

return true;
}
}
65 changes: 65 additions & 0 deletions tests/Zle/Validate/DateCompareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* Zend Library Extension
*
* PHP version 5
*
* @category Zle
* @package Zle_Test
* @author Fabio Napoleoni <f.napoleoni@gmail.com>
* @license http://framework.zend.com/license/new-bsd New BSD License
* @link http://framework.zend.com/
*/

/**
* DateCompareTest
*
* @category Zle
* @package Zle_Test
* @author Andrea Giannantonio <a.giannantonio@gmail.com>
* @license http://framework.zend.com/license/new-bsd New BSD License
* @link http://framework.zend.com/
*/
class DateCompareTest extends PHPUnit_Framework_TestCase
{
public function dataForTest()
{
return array(
array('2011-06-05', '2011-06-01', '2011-06-10', true), //between dates
array('2011-06-10', '2011-06-01', '2011-06-10', true), //between dates
array('2011-06-01', '2011-06-01', '2011-06-10', true), //between dates
array('2011-06-15', '2011-06-01', '2011-06-10', false), //between dates
array('2011-05-30', '2011-06-01', '2011-06-10', false), //between dates
array('2011-06-01', '2011-06-01', null, true), //exact match
array('2011-06-15', '2011-06-01', null, false), //exact match
array('2011-05-30', '2011-06-01', null, false), //exact match
array('2011-06-02', '2011-06-01', true, true), //not later
array('2011-06-01', '2011-06-01', true, false), //not later
array('2011-05-30', '2011-06-01', true, false), //not later
array('2011-05-30', '2011-06-01', false, true), //not earlier
array('2011-06-01', '2011-06-01', false, false), //not earlier
array('2011-06-10', '2011-06-01', false, false), //not earlier
);
}

/**
* @dataProvider dataForTest
* @param $value
* @param $token
* @param $compare
* @param $expected
* @return void
*/
public function testDateCompare($value, $token, $compare, $expected)
{
/** @var $validate Zle_Validate_DateCompare */
$validate = new Zle_Validate_DateCompare($token, $compare);
$this->assertEquals(
$expected,
$validate->isValid($value),
"value: $value -- token: $token -- compare: $compare"
);
}

}