Commit 02dd8b1a authored by romanb's avatar romanb

Validator refactoring. 2 new validators: past & future

parent e647cdbb
......@@ -77,7 +77,7 @@ class Doctrine_Validator extends Doctrine_Object
foreach ($data as $key => $value) {
if ($value === self::$_null) {
$value = null;
} elseif ($value instanceof Doctrine_Record) {
} else if ($value instanceof Doctrine_Record) {
$value = $value->getIncremented();
}
......@@ -191,7 +191,7 @@ class Doctrine_Validator extends Doctrine_Object
*
* @param $portableType portable doctrine type
* @return string
*/
*//*
public static function phpType($portableType)
{
switch ($portableType) {
......@@ -208,7 +208,7 @@ class Doctrine_Validator extends Doctrine_Object
default:
return $portableType;
}
}
}*/
/**
* returns whether or not the given variable is
* valid type
......@@ -217,6 +217,7 @@ class Doctrine_Validator extends Doctrine_Object
* @param string $type
* @return boolean
*/
/*
public static function isValidType($var, $type)
{
if ($type == 'boolean') {
......@@ -242,13 +243,57 @@ class Doctrine_Validator extends Doctrine_Object
return true;
break;
}
}*/
/**
* returns whether or not the given variable is
* valid type
*
* @param mixed $var
* @param string $type
* @return boolean
*/
public static function isValidType($var, $type)
{
if ($var === null) {
return true;
} else if (is_object($var)) {
return $type == 'object';
}
switch ($type) {
case 'float':
case 'double':
return (String)$var == strval(floatval($var));
case 'integer':
return (String)$var == strval(intval($var));
case 'string':
return is_string($var) || is_int($var) || is_float($var);
case 'array':
return is_array($var);
case 'object':
return is_object($var);
case 'boolean':
return is_bool($var);
case 'timestamp':
// todo: validate the timestamp is in YYYY-MM-DD HH:MM:SS format
return true;
case 'date':
$validator = self::getValidator('date');
return $validator->validate($var);
default:
return false;
}
}
/**
* returns the type of loosely typed variable
*
* @param mixed $var
* @return string
*/
*//*
public static function gettype($var)
{
$type = gettype($var);
......@@ -265,5 +310,5 @@ class Doctrine_Validator extends Doctrine_Object
default:
return $type;
}
}
}*/
}
......@@ -40,7 +40,7 @@ class Doctrine_Validator_Date
*/
public function validate($value)
{
if (empty($value)) {
if ($value === null) {
return true;
}
$e = explode('-', $value);
......
......@@ -41,7 +41,7 @@ class Doctrine_Validator_Email
*/
public function validate($value)
{
if (empty($value)) {
if ($value === null) {
return true;
}
if (isset($this->args)) {
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_Future
*
* @package Doctrine
* @category Object Relational Mapping
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.org>
*/
class Doctrine_Validator_Future
{
/**
* checks if the given value is a valid date in the future.
*
* @param mixed $value
* @return boolean
*/
public function validate($value)
{
if ($value === null) {
return true;
}
$e = explode('-', $value);
if (count($e) !== 3) {
return false;
}
if (is_array($this->args) && isset($this->args['timezone'])) {
switch (strtolower($this->args['timezone'])) {
case 'gmt':
$now = gmdate("U") - date("Z");
break;
default:
$now = getdate();
break;
}
} else {
$now = getdate();
}
if ($now['year'] > $e[0]) {
return false;
} else if ($now['year'] == $e[0]) {
if ($now['mon'] > $e[1]) {
return false;
} else if ($now['mon'] == $e[1]) {
return $now['mday'] < $e[2];
} else {
return true;
}
} else {
return true;
}
}
}
\ No newline at end of file
<?php
/*
* $Id: Date.php 2367 2007-09-02 20:00:27Z zYne $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_Past
*
* @package Doctrine
* @category Object Relational Mapping
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Roman Borschel <roman@code-factory.org>
*/
class Doctrine_Validator_Past
{
/**
* checks if the given value is a valid date in the past.
*
* @param mixed $value
* @return boolean
*/
public function validate($value)
{
if ($value === null) {
return true;
}
$e = explode('-', $value);
if (count($e) !== 3) {
return false;
}
if (is_array($this->args) && isset($this->args['timezone'])) {
switch (strtolower($this->args['timezone'])) {
case 'gmt':
$now = gmdate("U") - date("Z");
break;
default:
$now = getdate();
break;
}
} else {
$now = getdate();
}
if ($now['year'] < $e[0]) {
return false;
} else if ($now['year'] == $e[0]) {
if ($now['mon'] < $e[1]) {
return false;
} else if ($now['mon'] == $e[1]) {
return $now['mday'] > $e[2];
} else {
return true;
}
} else {
return true;
}
}
}
\ No newline at end of file
......@@ -2,14 +2,14 @@
class ValidatorTest extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn('mymixed', 'string', 100);
$this->hasColumn('mystring', 'string', 100, 'notnull|unique');
$this->hasColumn('mystring', 'string', 100, array('notnull', 'unique'));
$this->hasColumn('myarray', 'array', 1000);
$this->hasColumn('myobject', 'object', 1000);
$this->hasColumn('myinteger', 'integer', 11);
$this->hasColumn('myrange', 'integer', 11, array('range' => array(4,123)));
$this->hasColumn('myregexp', 'string', 5, array('regexp' => '/^[0-9]+$/'));
$this->hasColumn('myemail', 'string', 100, 'email');
$this->hasColumn('myemail2', 'string', 100, 'email|notblank');
$this->hasColumn('myemail', 'string', 100, array('email'));
$this->hasColumn('myemail2', 'string', 100, array('email', 'notblank'));
}
}
<?php
class ValidatorTest_DateModel extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn('birthday', 'date', null, array('past'));
$this->hasColumn('death', 'date', null, array('future'));
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_FutureTestCase
*
* @package Doctrine
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Validator_Future_TestCase extends Doctrine_UnitTestCase
{
public function prepareTables()
{
$this->tables[] = 'ValidatorTest_DateModel';
parent::prepareTables();
}
public function prepareData()
{
}
public function testValidFutureDates()
{
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
// one year ahead
$user1 = new ValidatorTest_DateModel();
$user1->death = date('Y-m-d', time() + 365 * 24 * 60 * 60);
$this->assertTrue($user1->trySave());
// one month ahead
$user1 = new ValidatorTest_DateModel();
$user1->death = date('Y-m-d', time() + 30 * 24 * 60 * 60);
$this->assertTrue($user1->trySave());
// one day ahead
$user1->death = date('Y-m-d', time() + 24 * 60 * 60);
$this->assertTrue($user1->trySave());
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE);
}
public function testInvalidFutureDates()
{
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$user1 = new ValidatorTest_DateModel();
$user1->death = date('Y-m-d', 42);
$this->assertFalse($user1->trySave());
$user1->death = date('Y-m-d', time());
$this->assertFalse($user1->trySave());
$user1->death = date('Y-m-d', time() + 60);
$this->assertFalse($user1->trySave());
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE);
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
/**
* Doctrine_Validator_FutureTestCase
*
* @package Doctrine
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Validator_Past_TestCase extends Doctrine_UnitTestCase
{
public function prepareTables()
{
$this->tables[] = 'ValidatorTest_DateModel';
parent::prepareTables();
}
public function prepareData()
{
}
public function testInvalidPastDates()
{
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
// one year ahead
$user1 = new ValidatorTest_DateModel();
$user1->birthday = date('Y-m-d', time() + 365 * 24 * 60 * 60);
$this->assertFalse($user1->trySave());
// one month ahead
$user1 = new ValidatorTest_DateModel();
$user1->birthday = date('Y-m-d', time() + 30 * 24 * 60 * 60);
$this->assertFalse($user1->trySave());
// one day ahead
$user1->birthday = date('Y-m-d', time() + 24 * 60 * 60);
$this->assertFalse($user1->trySave());
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE);
}
public function testValidPastDates()
{
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
$user1 = new ValidatorTest_DateModel();
$user1->birthday = date('Y-m-d', 42);
$this->assertTrue($user1->trySave());
$user1->birthday = date('Y-m-d', mktime(0,0,0,6,3,1981));
$this->assertTrue($user1->trySave());
$user1->birthday = date('Y-m-d', mktime(0,0,0,3,9,1983));
$this->assertTrue($user1->trySave());
$this->manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_NONE);
}
}
<?php
ini_set('max_execution_time', 900);
ini_set("date.timezone", "GMT+0");
function parseOptions($array) {
$currentName='';
......@@ -223,6 +224,8 @@ $plugins = new GroupTest('Plugin tests: View, Validator, Hook');
//$utility->addTestCase(new Doctrine_PessimisticLocking_TestCase());
$plugins->addTestCase(new Doctrine_View_TestCase());
$plugins->addTestCase(new Doctrine_Validator_TestCase());
$plugins->addTestCase(new Doctrine_Validator_Future_TestCase());
$plugins->addTestCase(new Doctrine_Validator_Past_TestCase());
$plugins->addTestCase(new Doctrine_Hook_TestCase());
$plugins->addTestCase(new Doctrine_I18n_TestCase());
$test->addTestCase($plugins);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment