1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-05-25 20:28:12 +03:00
ifhub.club/framework/classes/modules/validate/entity/ValidatorType.entity.class.php
2013-08-08 18:00:37 +07:00

102 lines
3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* CTypeValidator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* Валидатор типа данных
* Для типа дата/время используется внешний валидатор DateTimeParser
*
* @package engine.modules.validate
* @since 1.0
*/
class ModuleValidate_EntityValidatorType extends ModuleValidate_EntityValidator {
/**
* Допустимый тип данных.
* Допустимые значения: 'string', 'integer', 'float', 'array', 'date', 'time' и 'datetime'.
*
* @var string
*/
public $type='string';
/**
* Допустимый формат даты, актуально при type = date
*
* @var string
*/
public $dateFormat='dd-MM-yyyy';
/**
* Допустимый формат времени, актуально при type = time
*
* @var string
*/
public $timeFormat='hh:mm';
/**
* Допустимый формат даты со временем, актуально при type = datetime
*
* @var string
*/
public $datetimeFormat='dd-MM-yyyy hh:mm';
/**
* Допускать или нет пустое значение
*
* @var bool
*/
public $allowEmpty=true;
/**
* Запуск валидации
*
* @param mixed $sValue Данные для валидации
*
* @return bool|string
*/
public function validate($sValue) {
if($this->allowEmpty && $this->isEmpty($sValue)) {
return true;
}
require_once(Config::Get('path.framework.libs_vendor.server').'/DateTime/DateTimeParser.php');
if($this->type==='integer') {
$bValid=preg_match('/^[-+]?[0-9]+$/',trim($sValue));
} else if($this->type==='float') {
$bValid=preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($sValue));
} else if($this->type==='date') {
$bValid=DateTimeParser::parse($sValue,$this->dateFormat,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
} else if($this->type==='time') {
$bValid=DateTimeParser::parse($sValue,$this->timeFormat)!==false;
} else if($this->type==='datetime') {
$bValid=DateTimeParser::parse($sValue,$this->datetimeFormat, array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
} else if($this->type==='array') {
$bValid=is_array($sValue);
} else {
return true;
}
if(!$bValid) {
return $this->getMessage($this->Lang_Get('validate_type_error',null,false),'msg',array('type'=>$this->type));
}
return true;
}
}
?>