| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- namespace PhpLife\Frame;
- use PhpLife\Frame\Helper\Validate;
- /**
- * 参数过滤类
- */
- class Filter
- {
- protected $_data = array();
- public function __construct(Array $data = array())
- {
- foreach ($data as $name => $value) {
- $this->_data[$name] = $value;
- }
- }
- /**
- * 过滤字符串
- *
- * @param string $name
- * @param string $default
- * @return string
- */
- public function string($name, $default = null)
- {
- $result = null;
- if (isset($this->_data[$name])) {
- $result = $this->_data[$name];
- $result = trim($result);
- $result = (string)$result;
- } elseif (!is_null($default)) {
- $result = $default;
- }
- return $result;
- }
- /**
- * 批量获取字符串
- *
- * @param string $name
- * @param string $default
- * @return string
- */
- public function getBatchString($arr, $isString = TRUE)
- {
- $result = null;
- if (is_array($arr)) {
- foreach ($arr as $v) {
- if (isset($this->_data[$v])) {
- $result[$v] = $isString ? (string)trim($this->_data[$v]) : $this->_data[$v];
- }
- }
- }
- return $result;
- }
- /**
- * 获取手机区号
- *
- * @param string $name
- * @param string $default
- * @return string
- */
- public function mobileCode($name = 'mobile_code', $default = '+86')
- {
- $mobileCode = $this->string($name, $default);
- $mobileCode = '+' . ltrim($mobileCode, '+');
- return $mobileCode;
- }
- /**
- * 过滤整型
- *
- * @param string $name
- * @param int $default
- * @return int
- */
- public function intval($name, $default = null)
- {
- $result = null;
- if (isset($this->_data[$name])) {
- $result = $this->_data[$name];
- $result = trim($result);
- $result = intval($result);
- } elseif (!is_null($default)) {
- $result = $default;
- }
- return $result;
- }
- /**
- * 过滤浮点类型
- *
- * @param string $name
- * @param float $default
- * @return float
- */
- public function float($name, $default = null)
- {
- $result = null;
- if (isset($this->_data[$name])) {
- $result = $this->_data[$name];
- $result = trim($result);
- $result = floatval($result);
- } elseif (!is_null($default)) {
- $result = $default;
- }
- return $result;
- }
- /**
- * 验证手机号
- *
- * @param string $message
- * @param int $code
- * @param string $name
- * @return int|string
- * @throws \Exception
- */
- public function mobile($message = '手机号格式错误', $code = 45050, $name = 'mobile')
- {
- $result = '';
- if (isset($this->_data[$name])) {
- $result = $this->_data[$name];
- $result = trim($result);
- $result = intval($result);
- }
- if (!$result || Validate::isMobile($result) == false) {
- throw new \PhpLife\Frame\Exception\Validate($message, $code);
- }
- return $result;
- }
- /**
- * 获取json数据格式
- *
- * @param
- * $name
- * @param
- * $default
- * @return array
- */
- public function json($name, $default = null)
- {
- $result = null;
- if (isset($this->_data[$name])) {
- $result = $this->_data[$name];
- $result = trim($result);
- $decoded = json_decode($result,true);
- if ($decoded) {
- $result = $decoded;
- } else {
- $result = array();
- }
- } elseif (!is_null($default)) {
- $result = $default;
- }
- return $result;
- }
- /**
- * 检查字段是否存在,可选检查是否为空
- *
- * @param string $name
- * @param bool $checkEmpty
- * @param string $message
- * @param int $code
- * @throws \Exception
- * @return bool
- */
- public function exists($name, $checkEmpty = false, $message = '', $code = 0)
- {
- if ($checkEmpty) {
- $result = isset($this->_data[$name]) && (is_string($this->_data[$name]) && strlen($this->_data[$name]) > 0);
- } else {
- $result = isset($this->_data[$name]);
- }
- if (!$result && $message) {
- throw new \PhpLife\Frame\Exception\Validate($message, $code);
- }
- return $result;
- }
- /**
- * 获取数组数据
- *
- * @param
- * $name
- * @param array $default
- * @return array|mixed
- */
- public function getArray($name, $default = array())
- {
- $result = array();
- if (isset($this->_data[$name])) {
- $result = $this->_data[$name];
- } elseif (!is_null($default)) {
- $result = $default;
- }
- return $result;
- }
- /**
- * 参数验证
- * @param $data -参数
- * @param bool $required -是否必须
- * @return array
- * @throws Exception
- */
- public function params($data, $required = false)
- {
- $res = array();
- foreach ($data as $name => $rule) {
- if ($required) {
- $this->required($name);
- }
- $res[$name] = $this->$rule($name);
- }
- return $res;
- }
- public function required($name)
- {
- if($this->_data[$name] || strlen($this->_data[$name]) > 0){
- return $this->_data[$name];
- }else{
- throw new Exception('缺少必填参数:' . $name,400);
- }
- }
- public function compare($name, $value, $echo = 'selected')
- {
- if (isset($this->_data[$name]) && $this->_data[$name] !== '' && $this->_data[$name] == $value) {
- echo $echo;
- return;
- } else {
- echo '';
- return;
- }
- }
- public function toArray()
- {
- return $this->_data;
- }
- }
|