Request.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Develop\Support\Generate\Create;
  4. use Catch\CatchAdmin;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Facades\File;
  7. use Illuminate\Support\Str;
  8. class Request extends Creator
  9. {
  10. /**
  11. * @var array
  12. */
  13. protected array $structures;
  14. /**
  15. * @var array|string[]
  16. */
  17. protected array $replace = ['{namespace}', '{request}', '{rule}'];
  18. /**
  19. * @param string $controller
  20. */
  21. public function __construct(public readonly string $controller)
  22. {
  23. }
  24. /**
  25. * @return string
  26. */
  27. public function getFile(): string
  28. {
  29. return CatchAdmin::getModuleRequestPath($this->module).$this->getRequestName().$this->ext;
  30. }
  31. /**
  32. * get content
  33. *
  34. * @return string|bool
  35. */
  36. public function getContent(): string|bool
  37. {
  38. $rule = $this->getRulesString();
  39. if (! $rule) {
  40. return false;
  41. }
  42. return Str::of(
  43. File::get(dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'request.stub')
  44. )->replace($this->replace, [$this->getNamespace(), $this->getRequestName(), $rule])->toString();
  45. }
  46. /**
  47. * get namespace
  48. *
  49. * @return string
  50. */
  51. protected function getNamespace(): string
  52. {
  53. return Str::of(CatchAdmin::getModuleRequestNamespace($this->module))->rtrim('\\')->append(';')->toString();
  54. }
  55. /**
  56. * get request name
  57. *
  58. * @return ?string
  59. */
  60. public function getRequestName(): ?string
  61. {
  62. if ($this->getRules()) {
  63. return Str::of($this->controller)->remove('Controller')->append('Request')->ucfirst()->toString();
  64. }
  65. return null;
  66. }
  67. /**
  68. * get rule
  69. *
  70. * @return string|bool
  71. */
  72. public function getRulesString(): string|bool
  73. {
  74. $rules = $this->getRules();
  75. if (! count($rules)) {
  76. return false;
  77. }
  78. $rule = Str::of('');
  79. foreach ($rules as $field => $validates) {
  80. $rule = $rule->append("'{$field}'")
  81. ->append(' => ')
  82. ->append('\'')
  83. ->append(Arr::join($validates, '|'))
  84. ->append('\',')
  85. ->newLine();
  86. }
  87. return $rule->toString();
  88. }
  89. /**
  90. * get rules
  91. *
  92. * @return array
  93. */
  94. protected function getRules(): array
  95. {
  96. $rules = [];
  97. foreach ($this->structures as $structure) {
  98. if ($structure['field'] && count($structure['validates'])) {
  99. $rules[$structure['field']] = $structure['validates'];
  100. }
  101. }
  102. return $rules;
  103. }
  104. /**
  105. * set structures
  106. *
  107. * @param array $structures
  108. * @return $this
  109. */
  110. public function setStructures(array $structures): static
  111. {
  112. $this->structures = $structures;
  113. return $this;
  114. }
  115. }