OrderVirtualFieldValidate.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\validate\api;
  12. use think\Validate;
  13. class OrderVirtualFieldValidate extends Validate
  14. {
  15. protected $failException = true;
  16. protected $rule = [];
  17. public function load(array $extend, array $value)
  18. {
  19. $extend = array_combine(array_column($extend, 'title'), $extend);
  20. $rule = [];
  21. foreach ($extend as $title => $val) {
  22. $item = [];
  23. if ($val['key'] === 'image') {
  24. if ($val['require']) {
  25. $item[] = 'isRequireImage';
  26. } else {
  27. $item[] = 'isImage';
  28. }
  29. } else {
  30. if ($val['require']) {
  31. $item[] = 'require';
  32. }
  33. $item[] = 'is' . ucfirst($val['key']);
  34. }
  35. $rule[$title.' '] = implode('|', $item);
  36. }
  37. $this->rule = $rule;
  38. $data = [];
  39. foreach ($value as $v) {
  40. $data[(string)$v['title'].' '] = $v['value'] ?? '';
  41. }
  42. $this->check($data);
  43. return $data;
  44. }
  45. public function isMobile($val)
  46. {
  47. return $this->regex($val, 'mobile');
  48. }
  49. public function isDate($val)
  50. {
  51. return $this->dateFormat($val, 'Y-m-d');
  52. }
  53. public function isTime($val)
  54. {
  55. return $this->dateFormat($val, 'H:i');
  56. }
  57. public function isRequireImage($val)
  58. {
  59. if (!count($val)) return false;
  60. foreach ($val as $v) {
  61. if (!is_string($v)) return false;
  62. }
  63. return true;
  64. }
  65. public function isImage($val)
  66. {
  67. if (!count($val)) return true;
  68. return $this->isRequireImage($val);
  69. }
  70. public function isEmail($val)
  71. {
  72. return $this->filter($val, FILTER_VALIDATE_EMAIL);
  73. }
  74. public function isNumber($val)
  75. {
  76. return ctype_digit((string)$val);
  77. }
  78. public function isText($val)
  79. {
  80. return (bool)trim((string)$val);
  81. }
  82. public function isIdCard($val)
  83. {
  84. return $this->regex($val, 'idCard');
  85. }
  86. }