Validate.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\crud;
  14. /**
  15. * Class Validate
  16. * @author 等风来
  17. * @email 136327134@qq.com
  18. * @date 2023/3/29
  19. * @package crmeb\services\crud
  20. */
  21. class Validate extends Make
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $name = 'validate';
  27. /**
  28. * @return string
  29. * @author 等风来
  30. * @email 136327134@qq.com
  31. * @date 2023/4/4
  32. */
  33. protected function setBaseDir(): string
  34. {
  35. return 'app' . DS . 'adminapi' . DS . 'validate' . DS . 'crud';
  36. }
  37. /**
  38. * @param string $name
  39. * @param array $options
  40. * @return Validate
  41. * @author 等风来
  42. * @email 136327134@qq.com
  43. * @date 2023/4/23
  44. */
  45. public function handle(string $name, array $options = [])
  46. {
  47. [$rule, $message] = $this->getRuleContent($options['field']);
  48. $this->value['rule-php'] = $rule;
  49. $this->value['message-php'] = $message;
  50. return parent::handle($name, $options); // TODO: Change the autogenerated stub
  51. }
  52. /**
  53. * @param array $field
  54. * @return array
  55. * @author 等风来
  56. * @email 136327134@qq.com
  57. * @date 2023/4/23
  58. */
  59. protected function getRuleContent(array $field)
  60. {
  61. $content = [];
  62. $message = [];
  63. foreach ($field as $item) {
  64. $item['name'] = addslashes($item['name']);
  65. if ($item['required']) {
  66. $content[] = $this->tab(2) . '\'' . $item['field'] . '\'=>\'require\'';
  67. $message[] = $this->tab(2) . '\'' . $item['field'] . '.require\'=>\'' . $item['name'] . '必须填写\'';
  68. }
  69. }
  70. return [implode("\n", $content), implode("\n", $message)];
  71. }
  72. /**
  73. * 模板文件配置
  74. * @param string $type
  75. * @return mixed
  76. */
  77. protected function getStub(string $type = '')
  78. {
  79. return __DIR__ . DS . 'stubs' . DS . 'validate' . DS . 'crudValidate.stub';
  80. }
  81. }