Filter.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace Heanup\Frame;
  3. use Heanup\Frame\Helper\Validate;
  4. /**
  5. * 参数过滤类
  6. */
  7. class Filter
  8. {
  9. protected $_data = array();
  10. public function __construct(Array $data = array())
  11. {
  12. foreach ($data as $name => $value) {
  13. $this->_data[$name] = $value;
  14. }
  15. }
  16. /**
  17. * 过滤字符串
  18. *
  19. * @param string $name
  20. * @param string $default
  21. * @return string
  22. */
  23. public function string($name, $default = null)
  24. {
  25. $result = null;
  26. if (isset($this->_data[$name])) {
  27. $result = $this->_data[$name];
  28. $result = trim($result);
  29. $result = (string)$result;
  30. } elseif (!is_null($default)) {
  31. $result = $default;
  32. }
  33. return $result;
  34. }
  35. /**
  36. * 批量获取字符串
  37. *
  38. * @param string $name
  39. * @param string $default
  40. * @return string
  41. */
  42. public function getBatchString($arr, $isString = TRUE)
  43. {
  44. $result = null;
  45. if (is_array($arr)) {
  46. foreach ($arr as $v) {
  47. if (isset($this->_data[$v])) {
  48. $result[$v] = $isString ? (string)trim($this->_data[$v]) : $this->_data[$v];
  49. }
  50. }
  51. }
  52. return $result;
  53. }
  54. /**
  55. * 获取手机区号
  56. *
  57. * @param string $name
  58. * @param string $default
  59. * @return string
  60. */
  61. public function mobileCode($name = 'mobile_code', $default = '+86')
  62. {
  63. $mobileCode = $this->string($name, $default);
  64. $mobileCode = '+' . ltrim($mobileCode, '+');
  65. return $mobileCode;
  66. }
  67. /**
  68. * 过滤整型
  69. *
  70. * @param string $name
  71. * @param int $default
  72. * @return int
  73. */
  74. public function intval($name, $default = null)
  75. {
  76. $result = null;
  77. if (isset($this->_data[$name])) {
  78. $result = $this->_data[$name];
  79. $result = trim($result);
  80. $result = intval($result);
  81. } elseif (!is_null($default)) {
  82. $result = $default;
  83. }
  84. return $result;
  85. }
  86. /**
  87. * 过滤浮点类型
  88. *
  89. * @param string $name
  90. * @param float $default
  91. * @return float
  92. */
  93. public function float($name, $default = null)
  94. {
  95. $result = null;
  96. if (isset($this->_data[$name])) {
  97. $result = $this->_data[$name];
  98. $result = trim($result);
  99. $result = floatval($result);
  100. } elseif (!is_null($default)) {
  101. $result = $default;
  102. }
  103. return $result;
  104. }
  105. /**
  106. * 验证手机号
  107. *
  108. * @param string $message
  109. * @param int $code
  110. * @param string $name
  111. * @return int|string
  112. * @throws \Exception
  113. */
  114. public function mobile($message = '手机号格式错误', $code = 45050, $name = 'mobile')
  115. {
  116. $result = '';
  117. if (isset($this->_data[$name])) {
  118. $result = $this->_data[$name];
  119. $result = trim($result);
  120. $result = intval($result);
  121. }
  122. if (!$result || Validate::isMobile($result) == false) {
  123. throw new \Heanup\Frame\Exception\Validate($message, $code);
  124. }
  125. return $result;
  126. }
  127. /**
  128. * 获取json数据格式
  129. *
  130. * @param
  131. * $name
  132. * @param
  133. * $default
  134. * @return array
  135. */
  136. public function json($name, $default = null)
  137. {
  138. $result = null;
  139. if (isset($this->_data[$name])) {
  140. $result = $this->_data[$name];
  141. $result = trim($result);
  142. $decoded = json_decode($result,true);
  143. if ($decoded) {
  144. $result = $decoded;
  145. } else {
  146. $result = array();
  147. }
  148. } elseif (!is_null($default)) {
  149. $result = $default;
  150. }
  151. return $result;
  152. }
  153. /**
  154. * 检查字段是否存在,可选检查是否为空
  155. *
  156. * @param string $name
  157. * @param bool $checkEmpty
  158. * @param string $message
  159. * @param int $code
  160. * @throws \Exception
  161. * @return bool
  162. */
  163. public function exists($name, $checkEmpty = false, $message = '', $code = 0)
  164. {
  165. if ($checkEmpty) {
  166. $result = isset($this->_data[$name]) && (is_string($this->_data[$name]) && strlen($this->_data[$name]) > 0);
  167. } else {
  168. $result = isset($this->_data[$name]);
  169. }
  170. if (!$result && $message) {
  171. throw new \Heanup\Frame\Exception\Validate($message, $code);
  172. }
  173. return $result;
  174. }
  175. /**
  176. * 获取数组数据
  177. *
  178. * @param
  179. * $name
  180. * @param array $default
  181. * @return array|mixed
  182. */
  183. public function getArray($name, $default = array())
  184. {
  185. $result = array();
  186. if (isset($this->_data[$name])) {
  187. $result = $this->_data[$name];
  188. } elseif (!is_null($default)) {
  189. $result = $default;
  190. }
  191. return $result;
  192. }
  193. /**
  194. * 参数验证
  195. * @param $data -参数
  196. * @param bool $required -是否必须
  197. * @return array
  198. * @throws Exception
  199. */
  200. public function params($data, $required = false)
  201. {
  202. $res = array();
  203. foreach ($data as $name => $rule) {
  204. if ($required) {
  205. $this->required($name);
  206. }
  207. $res[$name] = $this->$rule($name);
  208. }
  209. return $res;
  210. }
  211. public function required($name)
  212. {
  213. if($this->_data[$name] || strlen($this->_data[$name]) > 0){
  214. return $this->_data[$name];
  215. }else{
  216. throw new Exception('缺少必填参数:' . $name,400);
  217. }
  218. }
  219. public function compare($name, $value, $echo = 'selected')
  220. {
  221. if (isset($this->_data[$name]) && $this->_data[$name] !== '' && $this->_data[$name] == $value) {
  222. echo $echo;
  223. return;
  224. } else {
  225. echo '';
  226. return;
  227. }
  228. }
  229. public function toArray()
  230. {
  231. return $this->_data;
  232. }
  233. }