Правило для файлов + Рекурсивное создание папки

This commit is contained in:
Фёдор Подлеснов 2016-07-27 15:02:04 +03:00
parent 405192f96a
commit 233e90ce72
5 changed files with 72 additions and 11 deletions

View file

@ -280,7 +280,7 @@ class Path
{
$path_dst = pathinfo($dst, PATHINFO_DIRNAME);
if (! file_exists($path_dst)) {
mkdir($path_dst);
mkdir($path_dst, 0700, true);
}
}

View file

@ -6,7 +6,7 @@ abstract class Rule_Abstract
protected $errorMsg;
protected $ctx;
public function __construct($field, $errorMsg)
public function __construct($field, $errorMsg = false)
{
$this->field = $field;
$this->errorMsg = $errorMsg;
@ -31,7 +31,11 @@ abstract class Rule_Abstract
public function isValid(Collection $container, $status = null)
{
//
return true;
}
function skipEmpty() {
return true;
}
public function setContext($ctx)

View 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;
}
}

View file

@ -4,6 +4,10 @@ require_once 'abstract.php';
class Rule_Notnull extends Rule_Abstract
{
function skipEmpty() {
return false;
}
public function getErrorMsg()
{
return "Поле не должно быть пустым";

View file

@ -1,4 +1,7 @@
<?php
require_once "core/validator/rule/all.php";
/**
* Проверка коллекции
*/
@ -7,6 +10,10 @@ class Validator
protected $chain = array(); // Массив правил
protected $errorMsg = array(); // Массив ошибок
function __construct($rules = array()) {
$this->addRuleList($rules);
}
/**
* Добавление списка правил в специальном формате
* array(array('name' => fieldname, 'validate' => ruletext), ...)
@ -15,8 +22,6 @@ class Validator
*/
public function addRuleList(array $input)
{
require_once "core/validator/rule/all.php";
$type = array(
'date' => 'Rule_Date',
'email' => 'Rule_Email',
@ -28,6 +33,7 @@ class Validator
'numeric' => 'Rule_Numeric',
'unique' => 'Rule_Unique',
'count' => 'Rule_Count',
'isfile' => 'Rule_IsFile',
'code' => 'Rule_Code'
);
@ -54,6 +60,8 @@ class Validator
$rule->$name = $value;
}
$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) {
return false;
} else {
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)
@ -88,7 +95,7 @@ class Validator
}
$this->errorMsg = array();
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;
$this->errorMsg[$name] = $rule->getErrorMsg();
}