Правило для файлов + Рекурсивное создание папки
This commit is contained in:
parent
405192f96a
commit
233e90ce72
5 changed files with 72 additions and 11 deletions
|
|
@ -280,7 +280,7 @@ class Path
|
||||||
{
|
{
|
||||||
$path_dst = pathinfo($dst, PATHINFO_DIRNAME);
|
$path_dst = pathinfo($dst, PATHINFO_DIRNAME);
|
||||||
if (! file_exists($path_dst)) {
|
if (! file_exists($path_dst)) {
|
||||||
mkdir($path_dst);
|
mkdir($path_dst, 0700, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ abstract class Rule_Abstract
|
||||||
protected $errorMsg;
|
protected $errorMsg;
|
||||||
protected $ctx;
|
protected $ctx;
|
||||||
|
|
||||||
public function __construct($field, $errorMsg)
|
public function __construct($field, $errorMsg = false)
|
||||||
{
|
{
|
||||||
$this->field = $field;
|
$this->field = $field;
|
||||||
$this->errorMsg = $errorMsg;
|
$this->errorMsg = $errorMsg;
|
||||||
|
|
@ -31,7 +31,11 @@ abstract class Rule_Abstract
|
||||||
|
|
||||||
public function isValid(Collection $container, $status = null)
|
public function isValid(Collection $container, $status = null)
|
||||||
{
|
{
|
||||||
//
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function skipEmpty() {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setContext($ctx)
|
public function setContext($ctx)
|
||||||
|
|
|
||||||
46
core/validator/rule/isfile.php
Normal file
46
core/validator/rule/isfile.php
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once 'abstract.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Проверка формата времени
|
||||||
|
*/
|
||||||
|
class 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -4,6 +4,10 @@ require_once 'abstract.php';
|
||||||
|
|
||||||
class Rule_Notnull extends Rule_Abstract
|
class Rule_Notnull extends Rule_Abstract
|
||||||
{
|
{
|
||||||
|
function skipEmpty() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function getErrorMsg()
|
public function getErrorMsg()
|
||||||
{
|
{
|
||||||
return "Поле не должно быть пустым";
|
return "Поле не должно быть пустым";
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once "core/validator/rule/all.php";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Проверка коллекции
|
* Проверка коллекции
|
||||||
*/
|
*/
|
||||||
|
|
@ -7,6 +10,10 @@ class Validator
|
||||||
protected $chain = array(); // Массив правил
|
protected $chain = array(); // Массив правил
|
||||||
protected $errorMsg = array(); // Массив ошибок
|
protected $errorMsg = array(); // Массив ошибок
|
||||||
|
|
||||||
|
function __construct($rules = array()) {
|
||||||
|
$this->addRuleList($rules);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Добавление списка правил в специальном формате
|
* Добавление списка правил в специальном формате
|
||||||
* array(array('name' => fieldname, 'validate' => ruletext), ...)
|
* array(array('name' => fieldname, 'validate' => ruletext), ...)
|
||||||
|
|
@ -15,8 +22,6 @@ class Validator
|
||||||
*/
|
*/
|
||||||
public function addRuleList(array $input)
|
public function addRuleList(array $input)
|
||||||
{
|
{
|
||||||
require_once "core/validator/rule/all.php";
|
|
||||||
|
|
||||||
$type = array(
|
$type = array(
|
||||||
'date' => 'Rule_Date',
|
'date' => 'Rule_Date',
|
||||||
'email' => 'Rule_Email',
|
'email' => 'Rule_Email',
|
||||||
|
|
@ -28,6 +33,7 @@ class Validator
|
||||||
'numeric' => 'Rule_Numeric',
|
'numeric' => 'Rule_Numeric',
|
||||||
'unique' => 'Rule_Unique',
|
'unique' => 'Rule_Unique',
|
||||||
'count' => 'Rule_Count',
|
'count' => 'Rule_Count',
|
||||||
|
'isfile' => 'Rule_IsFile',
|
||||||
'code' => 'Rule_Code'
|
'code' => 'Rule_Code'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
@ -54,6 +60,8 @@ class Validator
|
||||||
$rule->$name = $value;
|
$rule->$name = $value;
|
||||||
}
|
}
|
||||||
$this->addRule($rule);
|
$this->addRule($rule);
|
||||||
|
} else {
|
||||||
|
throw new Exception('Unknown validation rule ' . $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -68,17 +76,16 @@ class Validator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Skip($rule, $container) // -> Rule_Abstract
|
public function skip($rule, $container) // -> Rule_Abstract
|
||||||
{
|
{
|
||||||
if ($rule instanceof Rule_Notnull) {
|
if ($rule->skipEmpty()) {
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
$data = $container->get($rule->field);
|
$data = $container->get($rule->field);
|
||||||
if (!is_array($data)) {
|
if (!is_array($data)) {
|
||||||
$value = trim($data);
|
$value = trim($data);
|
||||||
return $value == '';
|
return $value == '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function validate(Collection $container, $rule = null, $status = null)
|
public function validate(Collection $container, $rule = null, $status = null)
|
||||||
|
|
@ -88,7 +95,7 @@ class Validator
|
||||||
}
|
}
|
||||||
$this->errorMsg = array();
|
$this->errorMsg = array();
|
||||||
foreach ($this->chain as $key => $rule) {
|
foreach ($this->chain as $key => $rule) {
|
||||||
if (!$this->Skip($rule, $container) && !$rule->isValid($container, $status)) {
|
if (!$this->skip($rule, $container) && !$rule->isValid($container, $status)) {
|
||||||
$name = $rule->field;
|
$name = $rule->field;
|
||||||
$this->errorMsg[$name] = $rule->getErrorMsg();
|
$this->errorMsg[$name] = $rule->getErrorMsg();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue