Validate

验证器类: EasySwoole\Validate\Validate

例子

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 18-10-11
  6. * Time: 上午10:26
  7. */
  8. require_once 'vendor/autoload.php';
  9. $data = [
  10. 'name' => 'blank',
  11. 'age' => 25
  12. ];
  13. $valitor = new \EasySwoole\Validate\Validate();
  14. $valitor->addColumn('name', '名字不为空')->required('名字不为空')->lengthMin(10,'最小长度不小于10位');
  15. $bool = $valitor->validate($data);
  16. var_dump($valitor->getError()->getErrorRuleMsg()?:$valitor->getError()->getColumnErrorMsg());
  17. /* 结果:
  18. string(26) "最小长度不小于10位"
  19. */

如何在控制器使用验证例子(demo)

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: root
  5. * Date: 18-11-14
  6. * Time: 下午2:33
  7. */
  8. namespace App\HttpController\Validate;
  9. use App\HttpController\Base;
  10. use EasySwoole\Http\Message\Status;
  11. use EasySwoole\Validate\Validate;
  12. class Index extends Base
  13. {
  14. function index() {
  15. $validate = new Validate();
  16. $validate->addColumn('name')->required('姓名必填');
  17. $validate->addColumn('age')->required('年龄必填')->between(20, 30, '年轻只能在20岁到30岁之前');
  18. if ($this->validate($validate)) {
  19. $this->writeJson(Status::CODE_OK, null, 'success');
  20. } else {
  21. $this->writeJson(Status::CODE_BAD_REQUEST, $validate->getError()->__toString(), 'fail');
  22. }
  23. }
  24. }

启动服务。

访问http://localhost:9501/validate,响应结果如下:

  1. {"code":400,"result":"name姓名必填","msg":"fail"}

访问http://localhost:9501/validate?name=blank,响应结果如下:

  1. {"code":400,"result":"age年龄必填","msg":"fail"}

访问http://localhost:9501/validate?name=blank&age=12,响应结果如下:

  1. {"code":400,"result":"age年龄只能在20岁到30岁之前","msg":"fail"}

访问http://localhost:9501/validate?name=blank&age=22,响应结果如下:

  1. {"code":400,"result":"age年龄只能在20岁到30岁之前","msg":"fail"}

方法列表

获取Error:

  1. function getError():?EasySwoole\Validate\Error

给字段添加规则:

  • string name 字段key
  • string errorMsg 错误信息
    • string alias 别名
  1. public function addColumn(string $name,?string $errorMsg = null,?string $alias = null):EasySwoole\Validate\Rule

返回一个Rule对象可以添加自定义规则。

数据验证:

  • array data 数据
  1. function validate(array $data)

验证规则类

目前验证器支持的规则如下

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yf
  5. * Date: 2018/7/6
  6. * Time: 上午12:41
  7. */
  8. namespace EasySwoole\Validate;
  9. /**
  10. * 校验规则
  11. * 请以首字母排序校验方法以便后期维护
  12. * Class Rule
  13. * @package EasySwoole\Validate
  14. */
  15. class Rule
  16. {
  17. protected $ruleMap = [];
  18. function getRuleMap(): array
  19. {
  20. return $this->ruleMap;
  21. }
  22. /**
  23. * 给定的URL是否可以成功通讯
  24. * @param null|string $msg
  25. * @return $this
  26. */
  27. function activeUrl($msg = null)
  28. {
  29. $this->ruleMap['activeUrl'] = [
  30. 'arg' => null,
  31. 'msg' => $msg
  32. ];
  33. return $this;
  34. }
  35. /**
  36. * 给定的参数是否是字母 即[a-zA-Z]
  37. * @param null|string $msg
  38. * @return $this
  39. */
  40. function alpha($msg = null)
  41. {
  42. $this->ruleMap['alpha'] = [
  43. 'arg' => null,
  44. 'msg' => $msg
  45. ];
  46. return $this;
  47. }
  48. function alphaNum($msg = null)
  49. {
  50. $this->ruleMap['alphaNum'] = [
  51. 'arg' => null,
  52. 'msg' => $msg
  53. ];
  54. return $this;
  55. }
  56. function alphaDash($msg = null)
  57. {
  58. $this->ruleMap['alphaDash'] = [
  59. 'arg' => null,
  60. 'msg' => $msg
  61. ];
  62. return $this;
  63. }
  64. /**
  65. * 给定的参数是否在 $min $max 之间
  66. * @param integer $min 最小值 不包含该值
  67. * @param integer $max 最大值 不包含该值
  68. * @param null|string $msg
  69. * @return $this
  70. */
  71. function between($min, $max, $msg = null)
  72. {
  73. $this->ruleMap['between'] = [
  74. 'msg' => $msg,
  75. 'arg' => [
  76. $min, $max
  77. ]
  78. ];
  79. return $this;
  80. }
  81. /**
  82. * 给定参数是否为布尔值
  83. * @param null|string $msg
  84. * @return $this
  85. */
  86. function bool($msg = null)
  87. {
  88. $this->ruleMap['bool'] = [
  89. 'msg' => $msg,
  90. 'arg' => null
  91. ];
  92. return $this;
  93. }
  94. /**
  95. * 给定参数是否为小数格式
  96. * @param null|integer $precision 规定小数点位数 null 为不规定
  97. * @param null $msg
  98. * @return $this
  99. */
  100. function decimal(?int $precision = null, $msg = null)
  101. {
  102. $this->ruleMap['decimal'] = [
  103. 'msg' => $msg,
  104. 'arg' => $precision
  105. ];
  106. return $this;
  107. }
  108. /**
  109. * 给定参数是否在某日期之前
  110. * @param null|string $date
  111. * @param null|string $msg
  112. * @return $this
  113. */
  114. function dateBefore(?string $date = null, $msg = null)
  115. {
  116. $this->ruleMap['dateBefore'] = [
  117. 'msg' => $msg,
  118. 'arg' => $date
  119. ];
  120. return $this;
  121. }
  122. /**
  123. * 给定参数是否在某日期之后
  124. * @param null|string $date
  125. * @param null|string $msg
  126. * @return $this
  127. */
  128. function dateAfter(?string $date = null, $msg = null)
  129. {
  130. $this->ruleMap['dateAfter'] = [
  131. 'msg' => $msg,
  132. 'arg' => $date
  133. ];
  134. return $this;
  135. }
  136. /**
  137. * 验证值是否相等
  138. * @param $compare
  139. * @param null|string $msg
  140. * @return $this
  141. */
  142. function equal($compare, $msg = null)
  143. {
  144. $this->ruleMap['equal'] = [
  145. 'msg' => $msg,
  146. 'arg' => $compare
  147. ];
  148. return $this;
  149. }
  150. /**
  151. * 验证值是否一个浮点数
  152. * @param null|string $msg
  153. * @return $this
  154. */
  155. function float($msg = null)
  156. {
  157. $this->ruleMap['float'] = [
  158. 'arg' => null,
  159. 'msg' => $msg
  160. ];
  161. return $this;
  162. }
  163. /**
  164. * 调用自定义的闭包验证
  165. * @param callable $func
  166. * @param null|string $msg
  167. * @return $this
  168. */
  169. function func(callable $func, $msg = null)
  170. {
  171. $this->ruleMap['func'] = [
  172. 'arg' => $func,
  173. 'msg' => $msg
  174. ];
  175. return $this;
  176. }
  177. /**
  178. * 值是否在数组中
  179. * @param array $array
  180. * @param bool $isStrict
  181. * @param null|string $msg
  182. * @return $this
  183. */
  184. function inArray(array $array, $isStrict = false, $msg = null)
  185. {
  186. $this->ruleMap['inArray'] = [
  187. 'arg' => [ $array, $isStrict ],
  188. 'msg' => $msg
  189. ];
  190. return $this;
  191. }
  192. /**
  193. * 是否一个整数值
  194. * @param null|string $msg
  195. * @return $this
  196. */
  197. function integer($msg = null)
  198. {
  199. $this->ruleMap['integer'] = [
  200. 'arg' => null,
  201. 'msg' => $msg
  202. ];
  203. return $this;
  204. }
  205. /**
  206. * 是否一个有效的IP
  207. * @param array $array
  208. * @param null $msg
  209. * @return $this
  210. */
  211. function isIp($msg = null)
  212. {
  213. $this->ruleMap['isIp'] = [
  214. 'arg' => null,
  215. 'msg' => $msg
  216. ];
  217. return $this;
  218. }
  219. /**
  220. * 是否不为空
  221. * @param null $msg
  222. * @return $this
  223. */
  224. function notEmpty($msg = null)
  225. {
  226. $this->ruleMap['notEmpty'] = [
  227. 'arg' => null,
  228. 'msg' => $msg
  229. ];
  230. return $this;
  231. }
  232. /**
  233. * 是否一个数字值
  234. * @param null $msg
  235. * @return $this
  236. */
  237. function numeric($msg = null)
  238. {
  239. $this->ruleMap['numeric'] = [
  240. 'arg' => null,
  241. 'msg' => $msg
  242. ];
  243. return $this;
  244. }
  245. /**
  246. * 不在数组中
  247. * @param array $array
  248. * @param bool $isStrict
  249. * @param null $msg
  250. * @return $this
  251. */
  252. function notInArray(array $array, $isStrict = false, $msg = null)
  253. {
  254. $this->ruleMap['notInArray'] = [
  255. 'arg' => [ $array, $isStrict ],
  256. 'msg' => $msg
  257. ];
  258. return $this;
  259. }
  260. /**
  261. * 验证数组或字符串的长度
  262. * @param int $len
  263. * @param null $msg
  264. * @return $this
  265. */
  266. function length(int $len, $msg = null)
  267. {
  268. $this->ruleMap['length'] = [
  269. 'msg' => $msg,
  270. 'arg' => $len
  271. ];
  272. return $this;
  273. }
  274. /**
  275. * 验证数组或字符串的长度是否超出
  276. * @param int $lengthMax
  277. * @param null $msg
  278. * @return $this
  279. */
  280. function lengthMax(int $lengthMax, $msg = null)
  281. {
  282. $this->ruleMap['lengthMax'] = [
  283. 'msg' => $msg,
  284. 'arg' => $lengthMax
  285. ];
  286. return $this;
  287. }
  288. /**
  289. * 验证数组或字符串的长度是否达到
  290. * @param int $lengthMin
  291. * @param null $msg
  292. * @return $this
  293. */
  294. function lengthMin(int $lengthMin, $msg = null)
  295. {
  296. $this->ruleMap['lengthMin'] = [
  297. 'msg' => $msg,
  298. 'arg' => $lengthMin
  299. ];
  300. return $this;
  301. }
  302. /**
  303. * 验证数组或字符串的长度是否在一个范围内
  304. * @param null $msg
  305. * @return $this
  306. */
  307. function betweenLen(int $min, int $max, $msg = null)
  308. {
  309. $this->ruleMap['betweenLen'] = [
  310. 'msg' => $msg,
  311. 'arg' => [
  312. $min,
  313. $max
  314. ]
  315. ];
  316. return $this;
  317. }
  318. /**
  319. * 验证值不大于(相等视为不通过)
  320. * @param int $max
  321. * @param null|string $msg
  322. * @return Rule
  323. */
  324. function max(int $max, ?string $msg = null): Rule
  325. {
  326. $this->ruleMap['max'] = [
  327. 'arg' => $max,
  328. 'msg' => $msg
  329. ];
  330. return $this;
  331. }
  332. /**
  333. * 验证值不小于(相等视为不通过)
  334. * @param int $min
  335. * @param null|string $msg
  336. * @return Rule
  337. */
  338. function min(int $min, ?string $msg = null): Rule
  339. {
  340. $this->ruleMap['min'] = [
  341. 'arg' => $min,
  342. 'msg' => $msg
  343. ];
  344. return $this;
  345. }
  346. /**
  347. * 验证值是合法的金额
  348. * 100 | 100.1 | 100.01
  349. * @param integer|null $precision 小数点位数
  350. * @param string|null $msg
  351. * @return Rule
  352. */
  353. function money(?int $precision = null, string $msg = null): Rule
  354. {
  355. $this->ruleMap['money'] = [
  356. 'arg' => $precision,
  357. 'msg' => $msg
  358. ];
  359. return $this;
  360. }
  361. /**
  362. * 设置值为可选参数
  363. * @return $this
  364. */
  365. function optional()
  366. {
  367. $this->ruleMap['optional'] = [
  368. 'arg' => null,
  369. 'msg' => null
  370. ];
  371. return $this;
  372. }
  373. /**
  374. * 正则表达式验证
  375. * @param $reg
  376. * @param null $msg
  377. * @return $this
  378. */
  379. function regex($reg, $msg = null)
  380. {
  381. $this->ruleMap['regex'] = [
  382. 'arg' => $reg,
  383. 'msg' => $msg
  384. ];
  385. return $this;
  386. }
  387. /**
  388. * 必须存在值
  389. * @param null $msg
  390. * @return $this
  391. */
  392. function required($msg = null)
  393. {
  394. $this->ruleMap['required'] = [
  395. 'arg' => null,
  396. 'msg' => $msg
  397. ];
  398. return $this;
  399. }
  400. /**
  401. * 值是一个合法的时间戳
  402. * @param null $msg
  403. * @return $this
  404. */
  405. function timestamp($msg = null)
  406. {
  407. $this->ruleMap['timestamp'] = [
  408. 'arg' => null,
  409. 'msg' => $msg
  410. ];
  411. return $this;
  412. }
  413. /**
  414. * 时间戳在某指定日期之前
  415. * @param string $date 传入任意可被strtotime解析的字符串
  416. * @param null $msg
  417. * @return $this
  418. */
  419. function timestampBeforeDate($date, $msg = null)
  420. {
  421. $this->ruleMap['timestampBeforeDate'] = [
  422. 'arg' => $date,
  423. 'msg' => $msg
  424. ];
  425. return $this;
  426. }
  427. /**
  428. * 时间戳在某指定日期之后
  429. * @param string $date 传入任意可被strtotime解析的字符串
  430. * @param null $msg
  431. * @return $this
  432. */
  433. function timestampAfterDate($date, $msg = null)
  434. {
  435. $this->ruleMap['timestampAfterDate'] = [
  436. 'arg' => $date,
  437. 'msg' => $msg
  438. ];
  439. return $this;
  440. }
  441. /**
  442. * 指定时间戳在某时间戳之前
  443. * @param string|integer $beforeTimestamp 在该时间戳之前
  444. * @param null $msg
  445. * @return $this
  446. */
  447. function timestampBefore($beforeTimestamp, $msg = null)
  448. {
  449. $this->ruleMap['timestampBefore'] = [
  450. 'arg' => $beforeTimestamp,
  451. 'msg' => $msg
  452. ];
  453. return $this;
  454. }
  455. /**
  456. * 指定时间戳在某时间戳之后
  457. * @param string|integer $afterTimestamp 在该时间戳之后
  458. * @param null $msg
  459. * @return $this
  460. */
  461. function timestampAfter($afterTimestamp, $msg = null)
  462. {
  463. $this->ruleMap['timestampAfter'] = [
  464. 'arg' => $afterTimestamp,
  465. 'msg' => $msg
  466. ];
  467. return $this;
  468. }
  469. /**
  470. * 值是一个合法的链接
  471. * @param null $msg
  472. * @return $this
  473. */
  474. function url($msg = null)
  475. {
  476. $this->ruleMap['url'] = [
  477. 'arg' => null,
  478. 'msg' => $msg
  479. ];
  480. return $this;
  481. }
  482. /**
  483. * 值是一个合法的链接
  484. * @param null $msg
  485. * @return $this
  486. */
  487. function allDigital($msg = null)
  488. {
  489. $this->ruleMap['allDigital'] = [
  490. 'arg' => null,
  491. 'msg' => $msg
  492. ];
  493. return $this;
  494. }
  495. }