Регистр файлов
This commit is contained in:
parent
4fd0187ea6
commit
c8958cbee0
83 changed files with 25 additions and 53 deletions
45
src/Validator/Rule/Abstract.php
Normal file
45
src/Validator/Rule/Abstract.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
abstract class Validator_Rule_Abstract
|
||||
{
|
||||
public $field;
|
||||
protected $errorMsg;
|
||||
protected $ctx;
|
||||
|
||||
public function __construct($field, $errorMsg = false)
|
||||
{
|
||||
$this->field = $field;
|
||||
$this->errorMsg = $errorMsg;
|
||||
}
|
||||
|
||||
public function setName($field)
|
||||
{
|
||||
$this->field = $field;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setErrorMsg($errorMsg)
|
||||
{
|
||||
$this->errorMsg = $errorMsg;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return $this->errorMsg;
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
function skipEmpty() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setContext($ctx)
|
||||
{
|
||||
$this->ctx = $ctx;
|
||||
}
|
||||
}
|
||||
18
src/Validator/Rule/Alpha.php
Normal file
18
src/Validator/Rule/Alpha.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка на число
|
||||
*/
|
||||
class Validator_Rule_Alpha extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Поле должно содержать только буквы";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return ctype_alpha($container->get($this->field));
|
||||
}
|
||||
}
|
||||
|
||||
57
src/Validator/Rule/Code.php
Normal file
57
src/Validator/Rule/Code.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата электронной почты
|
||||
*/
|
||||
class Validator_Rule_Code extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Неправильно указан персональный код";
|
||||
}
|
||||
|
||||
function checkCode($code) {
|
||||
foreach($code as $c) {
|
||||
if (empty($c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
if ($status == 'update') return true;
|
||||
$name = $this->field;
|
||||
|
||||
if (is_array($_POST[$name . '_code_genre'])) {
|
||||
for($n = 0; $n < count($_POST[$name . '_code_genre']); $n++) {
|
||||
$code = array(
|
||||
$_POST[$name . '_code_genre'][$n],
|
||||
$_POST[$name . '_code_f'][$n],
|
||||
$_POST[$name . '_code_i'][$n],
|
||||
$_POST[$name . '_code_o'][$n],
|
||||
$_POST[$name . '_code_year'][$n],
|
||||
$_POST[$name . '_code_month'][$n],
|
||||
$_POST[$name . '_code_day'][$n]
|
||||
);
|
||||
if (!$this->checkCode($code)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
$code = array(
|
||||
$_POST[$name . '_code_genre'],
|
||||
$_POST[$name . '_code_f'],
|
||||
$_POST[$name . '_code_i'],
|
||||
$_POST[$name . '_code_o'],
|
||||
$_POST[$name . '_code_year'],
|
||||
$_POST[$name . '_code_month'],
|
||||
$_POST[$name . '_code_day']
|
||||
);
|
||||
|
||||
return $this->checkCode($code);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/Validator/Rule/Count.php
Normal file
32
src/Validator/Rule/Count.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата даты
|
||||
*/
|
||||
class Validator_Rule_Count extends Rule_Abstract
|
||||
{
|
||||
public $size = 1;
|
||||
public $max = false;
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Количество записей должно быть не менне {$this->size} и не более {$this->max}";
|
||||
}
|
||||
|
||||
function not_empty($s) {
|
||||
return $s != "";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
|
||||
if (!$this->max) {
|
||||
$this->max = $this->size;
|
||||
}
|
||||
$count = count(array_filter(array_map('trim',
|
||||
explode(";", $container->get($this->field))), array($this, 'not_empty')));
|
||||
|
||||
return $count >= $this->size && $count <= $this->max;
|
||||
}
|
||||
}
|
||||
|
||||
22
src/Validator/Rule/Date.php
Normal file
22
src/Validator/Rule/Date.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата даты
|
||||
*/
|
||||
class Validator_Rule_Date extends Rule_Abstract
|
||||
{
|
||||
private $split = "\\/";
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Неверный формат даты";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$pattern = "/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/";
|
||||
return (preg_match($pattern, $container->get($this->field), $matches)
|
||||
&& checkdate($matches[2], $matches[1], $matches[3]));
|
||||
}
|
||||
}
|
||||
|
||||
28
src/Validator/Rule/Email.php
Normal file
28
src/Validator/Rule/Email.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата электронной почты
|
||||
*/
|
||||
class Validator_Rule_Email extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Неверный формат электронной почты";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\|\{\}~\']+';
|
||||
$doIsValid = '(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.?)+';
|
||||
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
|
||||
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
|
||||
|
||||
$emails = explode(",", $container->get($this->field));
|
||||
foreach ($emails as $email) {
|
||||
// if (! preg_match("/^$user@($doIsValid|(\[($ipv4|$ipv6)\]))$/", $email)) return false;
|
||||
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
26
src/Validator/Rule/EmailList.php
Normal file
26
src/Validator/Rule/EmailList.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата электронной почты
|
||||
*/
|
||||
class Validator_Rule_EmailList extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Неверный формат электронной почты";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null) {
|
||||
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\|\{\}~\']+';
|
||||
$doIsValid = '(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.?)+';
|
||||
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
|
||||
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
|
||||
|
||||
$emails = $container->get($this->field);
|
||||
foreach ($emails as $email) {
|
||||
// if (! preg_match("/^$user@($doIsValid|(\[($ipv4|$ipv6)\]))$/", $email)) return false;
|
||||
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
49
src/Validator/Rule/IsFile.php
Normal file
49
src/Validator/Rule/IsFile.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата времени
|
||||
*/
|
||||
class Validator_Rule_IsFile extends Rule_Abstract
|
||||
{
|
||||
private $type = array();
|
||||
private $maxsize = 1024;
|
||||
|
||||
function skipEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
function setSize($size) {
|
||||
$this->maxsize = $size;
|
||||
}
|
||||
|
||||
function setType(array $type) {
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
if (!isset($_FILES[$this->field]) || $_FILES[$this->field]['error'] == UPLOAD_ERR_NO_FILE) {
|
||||
$this->setErrorMsg('Файл не загружен');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($_FILES[$this->field]['error'] == UPLOAD_ERR_INI_SIZE) {
|
||||
$this->setErrorMsg('Превышен размер файла');
|
||||
return false;
|
||||
}
|
||||
|
||||
$tmp = $_FILES[$this->field];
|
||||
if (!in_array($tmp['type'], $this->type)) {
|
||||
$this->setErrorMsg('Неверный формат файла');
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($tmp['size'] > $this->maxsize*1024) {
|
||||
$this->setErrorMsg('Неверный размер файла');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
26
src/Validator/Rule/Match.php
Normal file
26
src/Validator/Rule/Match.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка на равентство двух полей
|
||||
*/
|
||||
class Validator_Rule_Match extends Rule_Abstract
|
||||
{
|
||||
public $same;
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Поля не совпадают";
|
||||
}
|
||||
|
||||
/* public function __construct($field, $refField, $errorMsg)
|
||||
{
|
||||
$this->field = $field;
|
||||
$this->refField = $refField;
|
||||
$this->errorMsg = $errorMsg;
|
||||
}
|
||||
*/
|
||||
|
||||
public function isValid(Collection $container, $status = null) {
|
||||
return (strcmp($container->get($this->field), $container->get($this->same)) == 0);
|
||||
}
|
||||
}
|
||||
27
src/Validator/Rule/Notnull.php
Normal file
27
src/Validator/Rule/Notnull.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
class Validator_Rule_Notnull extends Rule_Abstract
|
||||
{
|
||||
function skipEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Поле не должно быть пустым";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$data = $container->get($this->field);
|
||||
if (is_array($data)) {
|
||||
foreach($data as $c) {
|
||||
if (trim($c) != '') return true;
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
$value = trim($data);
|
||||
return $value != '';
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/Validator/Rule/Numeric.php
Normal file
17
src/Validator/Rule/Numeric.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка на число
|
||||
*/
|
||||
class Validator_Rule_Numeric extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Значение поля должно быть числом";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return (is_numeric($container->get($this->field)));
|
||||
}
|
||||
}
|
||||
33
src/Validator/Rule/Time.php
Normal file
33
src/Validator/Rule/Time.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата времени
|
||||
*/
|
||||
class Validator_Rule_Time extends Rule_Abstract
|
||||
{
|
||||
private $split = ":";
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Неверный формат времени";
|
||||
}
|
||||
|
||||
static function checktime($hour, $minute)
|
||||
{
|
||||
if ($hour > -1 && $hour < 24 && $minute > -1 && $minute < 60) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$tmp = explode($this->split, $container->get($this->field), 2);
|
||||
if ($tmp) {
|
||||
if (self::checktime ($tmp[0], $tmp[1])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
18
src/Validator/Rule/Unique.php
Normal file
18
src/Validator/Rule/Unique.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Проверка формата времени
|
||||
*/
|
||||
class Validator_Rule_Unique extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return $this->ctx->getMessage();
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return $this->ctx->isUnique($container->get($this->field), $status);
|
||||
}
|
||||
}
|
||||
|
||||
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