Validate.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $this->value['MODEl_NAME'] = $options['modelName'] ?? $name;
  48. $this->setRuleContent($options['field']);
  49. return parent::handle($name, $options);
  50. }
  51. /**
  52. * 设置规则内容
  53. * @param array $field
  54. * @return Validate
  55. * @author 等风来
  56. * @email 136327134@qq.com
  57. * @date 2023/4/23
  58. */
  59. protected function setRuleContent(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. $this->value['RULE_PHP'] = implode("\n", $content);
  71. $this->value['MESSAGE_PHP'] = implode("\n", $message);
  72. return $this;
  73. }
  74. /**
  75. * 模板文件配置
  76. * @param string $type
  77. * @return mixed
  78. */
  79. protected function getStub(string $type = '')
  80. {
  81. return __DIR__ . DS . 'stubs' . DS . 'validate' . DS . 'crudValidate.stub';
  82. }
  83. }