From 233e90ce72d233eb7cfb5c47a3eca226aaf89ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=91=D0=B4=D0=BE=D1=80=20=D0=9F=D0=BE=D0=B4=D0=BB?= =?UTF-8?q?=D0=B5=D1=81=D0=BD=D0=BE=D0=B2?= Date: Wed, 27 Jul 2016 15:02:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D0=BE=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20+?= =?UTF-8?q?=20=D0=A0=D0=B5=D0=BA=D1=83=D1=80=D1=81=D0=B8=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D0=B5=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BF=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/path.php | 2 +- core/validator/rule/abstract.php | 8 ++++-- core/validator/rule/isfile.php | 46 ++++++++++++++++++++++++++++++++ core/validator/rule/notnull.php | 4 +++ core/validator/validator.php | 23 ++++++++++------ 5 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 core/validator/rule/isfile.php diff --git a/core/path.php b/core/path.php index 1830e9b..70e83c0 100644 --- a/core/path.php +++ b/core/path.php @@ -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); } } diff --git a/core/validator/rule/abstract.php b/core/validator/rule/abstract.php index ed4f5c8..06189ec 100644 --- a/core/validator/rule/abstract.php +++ b/core/validator/rule/abstract.php @@ -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) diff --git a/core/validator/rule/isfile.php b/core/validator/rule/isfile.php new file mode 100644 index 0000000..37a03b9 --- /dev/null +++ b/core/validator/rule/isfile.php @@ -0,0 +1,46 @@ +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; + } +} + diff --git a/core/validator/rule/notnull.php b/core/validator/rule/notnull.php index 947fea7..8a2170e 100644 --- a/core/validator/rule/notnull.php +++ b/core/validator/rule/notnull.php @@ -4,6 +4,10 @@ require_once 'abstract.php'; class Rule_Notnull extends Rule_Abstract { + function skipEmpty() { + return false; + } + public function getErrorMsg() { return "Поле не должно быть пустым"; diff --git a/core/validator/validator.php b/core/validator/validator.php index efdb618..10a30d6 100644 --- a/core/validator/validator.php +++ b/core/validator/validator.php @@ -1,4 +1,7 @@ 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(); }