Регистр файлов
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);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue