Регистр файлов
This commit is contained in:
parent
4fd0187ea6
commit
c8958cbee0
83 changed files with 25 additions and 53 deletions
124
src/Validator/Validator.php
Normal file
124
src/Validator/Validator.php
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка коллекции
|
||||
*/
|
||||
class Validator
|
||||
{
|
||||
protected $chain = array(); // Массив правил
|
||||
protected $errorMsg = array(); // Массив ошибок
|
||||
|
||||
function __construct($rules = array()) {
|
||||
$this->addRuleList($rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавление списка правил в специальном формате
|
||||
* array(array('name' => fieldname, 'validate' => ruletext), ...)
|
||||
* fieldname - Имя переменой для проверки
|
||||
* ruletext - Описание правила см. формат правила ниже
|
||||
*/
|
||||
public function addRuleList(array $input)
|
||||
{
|
||||
$type = array(
|
||||
'date' => 'Validator_Rule_Date',
|
||||
'email' => 'Validator_Rule_Email',
|
||||
'emaillist'=> 'Validator_Rule_EmailList',
|
||||
'match' => 'Validator_Rule_Match',
|
||||
'time' => 'Validator_Rule_Time',
|
||||
'alpha' => 'Validator_Rule_Alpha',
|
||||
'require' => 'Validator_Rule_Notnull',
|
||||
'numeric' => 'Validator_Rule_Numeric',
|
||||
'unique' => 'Validator_Rule_Unique',
|
||||
'count' => 'Validator_Rule_Count',
|
||||
'isfile' => 'Validator_Rule_IsFile',
|
||||
'code' => 'Validator_Rule_Code'
|
||||
);
|
||||
|
||||
// Разбор правила проверки
|
||||
// Формат правила 'rule1|rule2,param1=value1|rule3,param1=value1,param2=value2'
|
||||
foreach ($input as $value) {
|
||||
// Список правил
|
||||
if (!isset($value['validate']) || $value['validate'] == '') continue;
|
||||
|
||||
$rules = explode("|", $value['validate']);
|
||||
foreach ($rules as $rule) {
|
||||
// Список параметров правила
|
||||
$rule_param = explode(",", $rule);
|
||||
$name = array_shift($rule_param);
|
||||
|
||||
if (isset($type[$name])) {
|
||||
$constructor = $type[$name]; // "Rule_" . ucfirst($name)
|
||||
$rule = new $constructor($value['name'], false); // Нужны шаблонные сообщения для правил
|
||||
if (isset($value['context'])) {
|
||||
$rule->setContext($value['context']);
|
||||
}
|
||||
foreach ($rule_param as $param) {
|
||||
// Имя и значение параметра
|
||||
list($name, $value) = explode("=", $param);
|
||||
$rule->$name = $value;
|
||||
}
|
||||
$this->addRule($rule);
|
||||
} else {
|
||||
throw new Exception('Unknown validation rule "' . $rule . "'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addRule(&$rule)
|
||||
{
|
||||
if (is_array($rule)) {
|
||||
$this->chain = array_merge($this->chain, $rule);
|
||||
} else {
|
||||
$this->chain[] = $rule;
|
||||
}
|
||||
}
|
||||
|
||||
public function skip($rule, $container) // -> 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)
|
||||
{
|
||||
if ($rule) {
|
||||
$this->chain = $rule;
|
||||
}
|
||||
$this->errorMsg = array();
|
||||
foreach ($this->chain as $key => $rule) {
|
||||
if (!$this->skip($rule, $container) && !$rule->isValid($container, $status)) {
|
||||
$name = $rule->field;
|
||||
$this->errorMsg[$name] = $rule->getErrorMsg();
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue