OutputEndpointData.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Knuckles\Camel\Output;
  3. use Illuminate\Http\UploadedFile;
  4. use Illuminate\Routing\Route;
  5. use Knuckles\Camel\BaseDTO;
  6. use Knuckles\Camel\Extraction\Metadata;
  7. use Knuckles\Camel\Extraction\ResponseCollection;
  8. use Knuckles\Camel\Extraction\ResponseField;
  9. use Knuckles\Scribe\Extracting\Extractor;
  10. use Knuckles\Scribe\Tools\Utils as u;
  11. class OutputEndpointData extends BaseDTO
  12. {
  13. /**
  14. * @var array<string>
  15. */
  16. public array $httpMethods;
  17. public string $uri;
  18. public Metadata $metadata;
  19. /**
  20. * @var array<string,string>
  21. */
  22. public array $headers = [];
  23. /**
  24. * @var array<string,\Knuckles\Camel\Output\Parameter>
  25. */
  26. public array $urlParameters = [];
  27. /**
  28. * @var array<string,mixed>
  29. */
  30. public array $cleanUrlParameters = [];
  31. /**
  32. * @var array<string,\Knuckles\Camel\Output\Parameter>
  33. */
  34. public array $queryParameters = [];
  35. /**
  36. * @var array<string,mixed>
  37. */
  38. public array $cleanQueryParameters = [];
  39. /**
  40. * @var array<string, \Knuckles\Camel\Output\Parameter>
  41. */
  42. public array $bodyParameters = [];
  43. /**
  44. * @var array<string,mixed>
  45. */
  46. public array $cleanBodyParameters = [];
  47. /**
  48. * @var array<string,\Illuminate\Http\UploadedFile>
  49. */
  50. public array $fileParameters = [];
  51. public ResponseCollection $responses;
  52. /**
  53. * @var array<string,\Knuckles\Camel\Extraction\ResponseField>
  54. */
  55. public array $responseFields = [];
  56. /**
  57. * @var array<string, array>
  58. */
  59. public array $nestedBodyParameters = [];
  60. public ?string $boundUri;
  61. public function __construct(array $parameters = [])
  62. {
  63. // spatie/dto currently doesn't auto-cast nested DTOs like that
  64. $parameters['responses'] = new ResponseCollection($parameters['responses']);
  65. $parameters['bodyParameters'] = array_map(function ($param) {
  66. return new Parameter($param);
  67. }, $parameters['bodyParameters']);
  68. $parameters['queryParameters'] = array_map(function ($param) {
  69. return new Parameter($param);
  70. }, $parameters['queryParameters']);
  71. $parameters['urlParameters'] = array_map(function ($param) {
  72. return new Parameter($param);
  73. }, $parameters['urlParameters']);
  74. $parameters['responseFields'] = array_map(function ($param) {
  75. return new ResponseField($param);
  76. }, $parameters['responseFields']);
  77. parent::__construct($parameters);
  78. $this->nestedBodyParameters = Extractor::nestArrayAndObjectFields($this->bodyParameters);
  79. $this->cleanBodyParameters = Extractor::cleanParams($this->bodyParameters);
  80. $this->cleanQueryParameters = Extractor::cleanParams($this->queryParameters);
  81. $this->cleanUrlParameters = Extractor::cleanParams($this->urlParameters);
  82. $this->boundUri = u::getUrlWithBoundParameters($this->uri, $this->cleanUrlParameters);
  83. [$files, $regularParameters] = collect($this->cleanBodyParameters)
  84. ->partition(
  85. function ($example) {
  86. // We'll only check two levels: a file, or an array of files
  87. return is_array($example) && isset($example[0])
  88. ? $example[0] instanceof UploadedFile
  89. : $example instanceof UploadedFile;
  90. }
  91. );
  92. if (count($files)) {
  93. $this->headers['Content-Type'] = 'multipart/form-data';
  94. }
  95. $this->fileParameters = $files->toArray();
  96. $this->cleanBodyParameters = $regularParameters->toArray();
  97. }
  98. /**
  99. * @param Route $route
  100. *
  101. * @return array<string>
  102. */
  103. public static function getMethods(Route $route): array
  104. {
  105. $methods = $route->methods();
  106. // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
  107. // but not if there's only one method (means it was intentional)
  108. if (count($methods) === 1) {
  109. return $methods;
  110. }
  111. return array_diff($methods, ['HEAD']);
  112. }
  113. public static function fromExtractedEndpointArray(array $endpoint): OutputEndpointData
  114. {
  115. return new self($endpoint);
  116. }
  117. public function endpointId(): string
  118. {
  119. return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':'], '-', $this->uri);
  120. }
  121. public function hasResponses(): bool
  122. {
  123. return count($this->responses) > 0;
  124. }
  125. public function hasFiles(): bool
  126. {
  127. return count($this->fileParameters) > 0;
  128. }
  129. public function isGet(): bool
  130. {
  131. return in_array('GET', $this->httpMethods);
  132. }
  133. public function hasHeadersOrQueryOrBodyParams(): bool
  134. {
  135. return !empty($this->headers)
  136. || !empty($this->cleanQueryParameters)
  137. || !empty($this->cleanBodyParameters);
  138. }
  139. }