|
2 | 2 |
|
3 | 3 | namespace App\Helpers\Swagger; |
4 | 4 |
|
| 5 | +use App\Helpers\MetaFormats\AnnotationConversion\Utils; |
| 6 | + |
5 | 7 | /** |
6 | 8 | * A data structure for endpoint signatures that can produce annotations parsable by a swagger generator. |
7 | 9 | */ |
8 | 10 | class AnnotationData |
9 | 11 | { |
10 | 12 | public HttpMethods $httpMethod; |
11 | 13 |
|
| 14 | + public string $className; |
| 15 | + public string $methodName; |
12 | 16 | public array $pathParams; |
13 | 17 | public array $queryParams; |
14 | 18 | public array $bodyParams; |
15 | 19 | public ?string $endpointDescription; |
16 | 20 |
|
17 | 21 | public function __construct( |
| 22 | + string $className, |
| 23 | + string $methodName, |
18 | 24 | HttpMethods $httpMethod, |
19 | 25 | array $pathParams, |
20 | 26 | array $queryParams, |
21 | 27 | array $bodyParams, |
22 | | - string $endpointDescription = null, |
| 28 | + ?string $endpointDescription = null, |
23 | 29 | ) { |
| 30 | + $this->className = $className; |
| 31 | + $this->methodName = $methodName; |
24 | 32 | $this->httpMethod = $httpMethod; |
25 | 33 | $this->pathParams = $pathParams; |
26 | 34 | $this->queryParams = $queryParams; |
@@ -68,16 +76,31 @@ private function getBodyAnnotation(): string | null |
68 | 76 | return $head . $body->toString() . "))"; |
69 | 77 | } |
70 | 78 |
|
71 | | - /** |
72 | | - * Converts the extracted annotation data to a string parsable by the Swagger-PHP library. |
73 | | - * @param string $route The route of the handler this set of data represents. |
74 | | - * @return string Returns the transpiled annotations on a single line. |
75 | | - */ |
| 79 | + /** |
| 80 | + * Constructs an operation ID used to identify the endpoint. |
| 81 | + * The operation ID is composed of the presenter class name and the endpoint method name with the 'action' prefix. |
| 82 | + * @return string Returns the operation ID. |
| 83 | + */ |
| 84 | + private function constructOperationId() |
| 85 | + { |
| 86 | + // remove the namespace prefix of the class and make the first letter lowercase |
| 87 | + $className = lcfirst(Utils::shortenClass($this->className)); |
| 88 | + // remove the 'action' prefix |
| 89 | + $endpoint = substr($this->methodName, strlen("action")); |
| 90 | + return $className . $endpoint; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Converts the extracted annotation data to a string parsable by the Swagger-PHP library. |
| 95 | + * @param string $route The route of the handler this set of data represents. |
| 96 | + * @return string Returns the transpiled annotations on a single line. |
| 97 | + */ |
76 | 98 | public function toSwaggerAnnotations(string $route) |
77 | 99 | { |
78 | 100 | $httpMethodAnnotation = $this->getHttpMethodAnnotation(); |
79 | 101 | $body = new ParenthesesBuilder(); |
80 | 102 | $body->addKeyValue("path", $route); |
| 103 | + $body->addKeyValue("operationId", $this->constructOperationId()); |
81 | 104 |
|
82 | 105 | // add the endpoint description when provided |
83 | 106 | if ($this->endpointDescription !== null) { |
|
0 commit comments