OutputEndpointData.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Knuckles\Camel\Output;
  3. use Illuminate\Http\UploadedFile;
  4. use Illuminate\Routing\Route;
  5. use Illuminate\Support\Str;
  6. use Knuckles\Camel\BaseDTO;
  7. use Knuckles\Camel\Extraction\Metadata;
  8. use Knuckles\Camel\Extraction\ResponseCollection;
  9. use Knuckles\Camel\Extraction\ResponseField;
  10. use Knuckles\Scribe\Extracting\Extractor;
  11. use Knuckles\Scribe\Tools\Utils as u;
  12. class OutputEndpointData extends BaseDTO
  13. {
  14. /**
  15. * @var array<string>
  16. */
  17. public array $httpMethods;
  18. public string $uri;
  19. public Metadata $metadata;
  20. /**
  21. * @var array<string,string>
  22. */
  23. public array $headers = [];
  24. /**
  25. * @var array<string,\Knuckles\Camel\Output\Parameter>
  26. */
  27. public array $urlParameters = [];
  28. /**
  29. * @var array<string,mixed>
  30. */
  31. public array $cleanUrlParameters = [];
  32. /**
  33. * @var array<string,\Knuckles\Camel\Output\Parameter>
  34. */
  35. public array $queryParameters = [];
  36. /**
  37. * @var array<string,mixed>
  38. */
  39. public array $cleanQueryParameters = [];
  40. /**
  41. * @var array<string, \Knuckles\Camel\Output\Parameter>
  42. */
  43. public array $bodyParameters = [];
  44. /**
  45. * @var array<string,mixed>
  46. */
  47. public array $cleanBodyParameters = [];
  48. /**
  49. * @var array<string,\Illuminate\Http\UploadedFile>
  50. */
  51. public array $fileParameters = [];
  52. public ResponseCollection $responses;
  53. /**
  54. * @var array<string,\Knuckles\Camel\Extraction\ResponseField>
  55. */
  56. public array $responseFields = [];
  57. /**
  58. * The same as bodyParameters, but organized in a hierarchy.
  59. * So, top-level items first, with a __fields property containing their children, and so on.
  60. * Useful so we can easily render and nest on the frontend.
  61. * @var array<string, array>
  62. */
  63. public array $nestedBodyParameters = [];
  64. /**
  65. * @var array<string, array>
  66. */
  67. public array $nestedResponseFields = [];
  68. public ?string $boundUri;
  69. public function __construct(array $parameters = [])
  70. {
  71. // spatie/dto currently doesn't auto-cast nested DTOs like that
  72. $parameters['responses'] = new ResponseCollection($parameters['responses']);
  73. $parameters['bodyParameters'] = array_map(function ($param) {
  74. return new Parameter($param);
  75. }, $parameters['bodyParameters']);
  76. $parameters['queryParameters'] = array_map(function ($param) {
  77. return new Parameter($param);
  78. }, $parameters['queryParameters']);
  79. $parameters['urlParameters'] = array_map(function ($param) {
  80. return new Parameter($param);
  81. }, $parameters['urlParameters']);
  82. $parameters['responseFields'] = array_map(function ($param) {
  83. return new ResponseField($param);
  84. }, $parameters['responseFields']);
  85. parent::__construct($parameters);
  86. $this->cleanBodyParameters = Extractor::cleanParams($this->bodyParameters);
  87. $this->cleanQueryParameters = Extractor::cleanParams($this->queryParameters);
  88. $this->cleanUrlParameters = Extractor::cleanParams($this->urlParameters);
  89. $this->nestedBodyParameters = Extractor::nestArrayAndObjectFields($this->bodyParameters, $this->cleanBodyParameters);
  90. $this->nestedResponseFields = Extractor::nestArrayAndObjectFields($this->responseFields);
  91. $this->boundUri = u::getUrlWithBoundParameters($this->uri, $this->cleanUrlParameters);
  92. [$files, $regularParameters] = static::getFileParameters($this->cleanBodyParameters);
  93. if (count($files)) {
  94. $this->headers['Content-Type'] = 'multipart/form-data';
  95. }
  96. $this->fileParameters = $files;
  97. $this->cleanBodyParameters = $regularParameters;
  98. }
  99. /**
  100. * @param Route $route
  101. *
  102. * @return array<string>
  103. */
  104. public static function getMethods(Route $route): array
  105. {
  106. $methods = $route->methods();
  107. // Laravel adds an automatic "HEAD" endpoint for each GET request, so we'll strip that out,
  108. // but not if there's only one method (means it was intentional)
  109. if (count($methods) === 1) {
  110. return $methods;
  111. }
  112. return array_diff($methods, ['HEAD']);
  113. }
  114. public static function fromExtractedEndpointArray(array $endpoint): OutputEndpointData
  115. {
  116. return new self($endpoint);
  117. }
  118. public function endpointId(): string
  119. {
  120. return $this->httpMethods[0] . str_replace(['/', '?', '{', '}', ':', '\\', '+', '|'], '-', $this->uri);
  121. }
  122. public function name(): string
  123. {
  124. return $this->metadata->title ?: ($this->httpMethods[0] . " " . $this->uri);
  125. }
  126. public function fullSlug(): string
  127. {
  128. $groupSlug = Str::slug($this->metadata->groupName);
  129. $endpointId = $this->endpointId();
  130. return "$groupSlug-$endpointId";
  131. }
  132. public function hasResponses(): bool
  133. {
  134. return count($this->responses) > 0;
  135. }
  136. public function hasFiles(): bool
  137. {
  138. return count($this->fileParameters) > 0;
  139. }
  140. public function isArrayBody(): bool
  141. {
  142. return count($this->nestedBodyParameters) === 1
  143. && array_keys($this->nestedBodyParameters)[0] === "[]";
  144. }
  145. public function isGet(): bool
  146. {
  147. return in_array('GET', $this->httpMethods);
  148. }
  149. public function hasHeadersOrQueryOrBodyParams(): bool
  150. {
  151. return !empty($this->headers)
  152. || !empty($this->cleanQueryParameters)
  153. || !empty($this->cleanBodyParameters);
  154. }
  155. public static function getFileParameters(array $parameters): array
  156. {
  157. $files = [];
  158. $regularParameters = [];
  159. foreach ($parameters as $name => $example) {
  160. if ($example instanceof UploadedFile) {
  161. $files[$name] = $example;
  162. } else if (is_array($example) && !empty($example)) {
  163. [$subFiles, $subRegulars] = static::getFileParameters($example);
  164. foreach ($subFiles as $subName => $subExample) {
  165. $files[$name][$subName] = $subExample;
  166. }
  167. foreach ($subRegulars as $subName => $subExample) {
  168. $regularParameters[$name][$subName] = $subExample;
  169. }
  170. } else {
  171. $regularParameters[$name] = $example;
  172. }
  173. }
  174. return [$files, $regularParameters];
  175. }
  176. }