137 lines
4.5 KiB
PHP
137 lines
4.5 KiB
PHP
<?php
|
|
|
|
///<reference path="Rule/Notnull.php"/>
|
|
|
|
/**
|
|
* Проверка коллекции
|
|
*/
|
|
namespace ctiso\Validator;
|
|
use Exception,
|
|
ctiso\Validator\Rule\AbstractRule,
|
|
ctiso\Collection;
|
|
|
|
class Validator
|
|
{
|
|
protected $chain = array(); // Массив правил
|
|
protected $errorMsg = array(); // Массив ошибок
|
|
protected $type = array(
|
|
'date' => 'ctiso\\Validator\\Rule\\Date',
|
|
'email' => 'ctiso\\Validator\\Rule\\Email',
|
|
'emaillist'=> 'ctiso\\Validator\\Rule\\EmailList',
|
|
'match' => 'ctiso\\Validator\\Rule\\MatchRule',
|
|
'time' => 'ctiso\\Validator\\Rule\\Time',
|
|
'alpha' => 'ctiso\\Validator\\Rule\\Alpha',
|
|
'require' => 'ctiso\\Validator\\Rule\\Notnull',
|
|
'numeric' => 'ctiso\\Validator\\Rule\\Numeric',
|
|
'unique' => 'ctiso\\Validator\\Rule\\Unique',
|
|
'filename' => 'ctiso\\Validator\\Rule\\FileName',
|
|
'count' => 'ctiso\\Validator\\Rule\\Count',
|
|
'isfile' => 'ctiso\\Validator\\Rule\\IsFile',
|
|
'code' => 'ctiso\\Validator\\Rule\\Code'
|
|
);
|
|
|
|
function __construct($rules = array()) {
|
|
$this->addRuleList($rules);
|
|
}
|
|
|
|
function addRuleType($name, $className) {
|
|
$this->type[$name] = $className;
|
|
}
|
|
|
|
/**
|
|
* Добавление списка правил в специальном формате
|
|
* array(array('name' => fieldname, 'validate' => ruletext), ...)
|
|
* fieldname - Имя переменой для проверки
|
|
* ruletext - Описание правила см. формат правила ниже
|
|
*/
|
|
public function addRuleList(array $input)
|
|
{
|
|
// Разбор правила проверки
|
|
// Формат правила 'rule1|rule2,param1=value1|rule3,param1=value1,param2=value2'
|
|
foreach ($input as $value) {
|
|
// Список правил
|
|
if (! isset($value['validate'])) continue;
|
|
$rules = explode("|", $value['validate']);
|
|
|
|
foreach ($rules as $rule) {
|
|
// Список параметров правила
|
|
$rule_param = explode(",", $rule);
|
|
$name = array_shift($rule_param);
|
|
|
|
if (isset($this->type[$name])) {
|
|
$constructor = $this->type[$name];
|
|
$ruleObj = new $constructor($value['name'], false); // Нужны шаблонные сообщения для правил
|
|
if (isset($value['context'])) {
|
|
$ruleObj->setContext($value['context']);
|
|
}
|
|
foreach ($rule_param as $param) {
|
|
// Имя и значение параметра
|
|
list($p_name, $p_value) = explode("=", $param);
|
|
$ruleObj->$p_name = $p_value;
|
|
}
|
|
$this->addRule($ruleObj);
|
|
} else if (!empty($rule)) {
|
|
throw new Exception('Unknown validation rule "' . $rule . "'");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function addRule($rule/*: any*/) {
|
|
if (is_array($rule)) {
|
|
$this->chain = array_merge($this->chain, $rule);
|
|
} else {
|
|
$this->chain[] = $rule;
|
|
}
|
|
}
|
|
|
|
public function skip($rule/*: AbstractRule*/, $container/*: Collection*/) // -> Rule_Abstract
|
|
{
|
|
if ($rule->skipEmpty()) {
|
|
$data = $container->get($rule->field);
|
|
if (!is_array($data)) {
|
|
$value = trim($data);
|
|
return $value == '';
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function validate(Collection $container, $rule = null, $status = null)
|
|
{
|
|
$fields = array();
|
|
if ($rule) {
|
|
$this->chain = $rule;
|
|
}
|
|
$this->errorMsg = array();
|
|
foreach ($this->chain as $rule) {
|
|
//echo $key;
|
|
if (!in_array($rule->field, $fields) && !$this->skip($rule, $container) && !$rule->isValid($container, $status)) {
|
|
$name = $rule->field;
|
|
$this->errorMsg[$name] = $rule->getErrorMsg();
|
|
$fields [] = $name;
|
|
}
|
|
}
|
|
return $this->isValid();
|
|
}
|
|
|
|
public function addError($name, $message)
|
|
{
|
|
$this->errorMsg[$name] = $message;
|
|
}
|
|
|
|
public function isError()
|
|
{
|
|
return !empty($this->errorMsg);
|
|
}
|
|
|
|
public function isValid()
|
|
{
|
|
return empty($this->errorMsg);
|
|
}
|
|
|
|
public function getErrorMsg()
|
|
{
|
|
return $this->errorMsg;
|
|
}
|
|
}
|