|
@@ -37,6 +37,8 @@ class GetFromLaravelAPI extends Strategy
|
|
|
|
|
|
$parameters = $this->inferBetterTypesAndExamplesForEloquentUrlParameters($parameters, $endpointData);
|
|
|
|
|
|
+ $parameters = $this->inferBetterTypesAndExamplesForEnumUrlParameters($parameters, $endpointData);
|
|
|
+
|
|
|
$parameters = $this->setTypesAndExamplesForOthers($parameters, $endpointData);
|
|
|
|
|
|
return $parameters;
|
|
@@ -47,18 +49,35 @@ class GetFromLaravelAPI extends Strategy
|
|
|
// If $url is sth like /users/{id}, return "The ID of the user."
|
|
|
// If $url is sth like /anything/{user_id}, return "The ID of the user."
|
|
|
|
|
|
- return collect(["id", "slug"])->flatMap(function ($name) use ($url, $paramName) {
|
|
|
+ $strategies = collect(["id", "slug"])->map(function ($name) {
|
|
|
$friendlyName = $name === 'id' ? "ID" : $name;
|
|
|
|
|
|
- if ($paramName == $name) {
|
|
|
- $thing = $this->getNameOfUrlThing($url, $paramName);
|
|
|
- return ["The $friendlyName of the $thing."];
|
|
|
- } else if (Str::is("*_$name", $paramName)) {
|
|
|
- $thing = str_replace(["_", "-"], " ", str_replace("_$name", '', $paramName));
|
|
|
- return ["The $friendlyName of the $thing."];
|
|
|
+ return function ($url, $paramName) use ($name, $friendlyName) {
|
|
|
+ if ($paramName == $name) {
|
|
|
+ $thing = $this->getNameOfUrlThing($url, $paramName);
|
|
|
+ return "The $friendlyName of the $thing.";
|
|
|
+ } else if (Str::is("*_$name", $paramName)) {
|
|
|
+ $thing = str_replace(["_", "-"], " ", str_replace("_$name", '', $paramName));
|
|
|
+ return "The $friendlyName of the $thing.";
|
|
|
+ }
|
|
|
+ };
|
|
|
+ })->toArray();
|
|
|
+
|
|
|
+ // If $url is sth like /categories/{category}, return "The category."
|
|
|
+ $strategies[] = function ($url, $paramName) {
|
|
|
+ $thing = $this->getNameOfUrlThing($url, $paramName);
|
|
|
+ if ($thing === $paramName) {
|
|
|
+ return "The $thing.";
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ foreach ($strategies as $strategy) {
|
|
|
+ if ($inferred = $strategy($url, $paramName)) {
|
|
|
+ return $inferred;
|
|
|
}
|
|
|
- return [];
|
|
|
- })->first() ?: '';
|
|
|
+ }
|
|
|
+
|
|
|
+ return '';
|
|
|
}
|
|
|
|
|
|
protected function inferBetterTypesAndExamplesForEloquentUrlParameters(array $parameters, ExtractedEndpointData $endpointData): array
|
|
@@ -117,6 +136,22 @@ class GetFromLaravelAPI extends Strategy
|
|
|
return $parameters;
|
|
|
}
|
|
|
|
|
|
+ protected function inferBetterTypesAndExamplesForEnumUrlParameters(array $parameters, ExtractedEndpointData $endpointData): array
|
|
|
+ {
|
|
|
+ $typeHintedEnums = UrlParamsNormalizer::getTypeHintedEnums($endpointData->method);
|
|
|
+ foreach ($typeHintedEnums as $argumentName => $enum) {
|
|
|
+ $parameters[$argumentName]['type'] = $this->normalizeTypeName($enum->getBackingType());
|
|
|
+
|
|
|
+ try {
|
|
|
+ $parameters[$argumentName]['example'] = $enum->getCases()[0]->getBackingValue();
|
|
|
+ } catch (Throwable) {
|
|
|
+ $parameters[$argumentName]['example'] = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $parameters;
|
|
|
+ }
|
|
|
+
|
|
|
protected function setTypesAndExamplesForOthers(array $parameters, ExtractedEndpointData $endpointData): array
|
|
|
{
|
|
|
foreach ($parameters as $name => $parameter) {
|