From 3e069d14f20577a385530cd4892553a89f9924b1 Mon Sep 17 00:00:00 2001 From: hide_d Date: Sat, 7 Apr 2018 14:24:20 +0900 Subject: [PATCH] =?UTF-8?q?Util::getReq=20=ED=95=A8=EC=88=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80.=20=EC=A0=95=EC=A0=81=20=EB=B6=84=EC=84=9D=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=EB=A5=BC=20=EC=9A=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20=EC=9C=84=ED=95=9C=20Validator::rule=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=B6=94=EA=B0=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sammo/Util.php | 54 +++++++++++++++++++++++++++++++++++++++++ src/sammo/Validator.php | 21 +++++++++++++--- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/sammo/Util.php b/src/sammo/Util.php index 7a66f257..8189f238 100644 --- a/src/sammo/Util.php +++ b/src/sammo/Util.php @@ -3,6 +3,60 @@ namespace sammo; class Util extends \utilphp\util { + /** + * $_GET, $_POST에서 값을 가져오는 함수. Util::array_get($_POST[$name])을 축약 가능. + * 타입이 복잡해질 경우 이 함수를 통하지 않고 json으로 요청할 것을 권장. + * + * @param string $name 가져오고자 하는 key 이름. + * @param string $type 가져오고자 하는 type. [string, int, float, bool, array, array_string, array_int] + * @return int|float|string|null + * @throws \InvalidArgumentException + */ + public static function getReq(string $name, string $type = 'string'){ + if(isset($_GET[$name])){ + $value = $_GET[$name]; + } + else if(isset($_POST[$name])){ + $value = $_POST[$name]; + } + else{ + return null; + } + + if(is_array($value)){ + if($type === 'array_int'){ + return array_map('intval', $value); + } + + if($type === 'array_string'){ + return array_map(function($item){ + return (string)$item; + }, $value); + } + + if($type === 'array'){ + return $value; + } + + throw new \InvalidArgumentException('지원할 수 없는 type 지정. array 가 붙은 type이어야 합니다'); + } + + if($type === 'bool'){ + return !!$value; + } + if($type === 'int'){ + return (int)$value; + } + if($type === 'float'){ + return (float)$value; + } + if($type === 'string'){ + return (string)$value; + } + + throw new \InvalidArgumentException('올바르지 않은 type 지정'); + } + public static function hashPassword($salt, $password) { return hash('sha512', $salt.$password.$salt); diff --git a/src/sammo/Validator.php b/src/sammo/Validator.php index 800cc8d0..ebf3e8c4 100644 --- a/src/sammo/Validator.php +++ b/src/sammo/Validator.php @@ -1,9 +1,6 @@ |string, null|int|string|array $option=null) - */ class Validator extends \Valitron\Validator { protected static $_lang = 'ko'; @@ -17,4 +14,22 @@ class Validator extends \Valitron\Validator $errors = join(', ', $errors); return $errors; } + + /** + * Convenience method to add a single validation rule + * Phan의 오동작으로 PhanUndeclaredFunctionInCallable가 발생하는것을 억제하기 위해서 별도 제작. + * $rule에 함수를 넣는 대신 상속한 클래스에 추가하는 방식을 사용할 것. + * + * @suppress PhanUndeclaredFunctionInCallable + * + * @param string $rule + * @param array|string $fields + * @return $this + * @throws \InvalidArgumentException + */ + public function rule($rule, $fields){ + $params = array_slice(func_get_args(), 2); + + return parent::rule($rule, $fields, $params); + } }