Quellcode durchsuchen

Properly parse examples for array parameters in @-param

shalvah vor 5 Jahren
Ursprung
Commit
b89c35755f

+ 8 - 1
src/Extracting/ParamHelpers.php

@@ -83,6 +83,13 @@ trait ParamHelpers
      */
     protected function castToType($value, string $type)
     {
+        if ($type === 'array' && is_string($value)) {
+            $value = trim($value);
+            if ($value[0] == '[' && $value[strlen($value) - 1] == ']') {
+                return json_decode($value, true);
+            }
+        }
+
         $casts = [
             'integer' => 'intval',
             'int' => 'intval',
@@ -152,7 +159,7 @@ trait ParamHelpers
      *
      * @return array The description and included example.
      */
-    protected function parseParamDescription(string $description, string $type)
+    protected function parseExampleFromParamDescription(string $description, string $type)
     {
         $example = null;
         if (preg_match('/(.*)\bExample:\s*([\s\S]+)\s*/', $description, $content)) {

+ 1 - 1
src/Extracting/Strategies/BodyParameters/GetFromBodyParamTag.php

@@ -89,7 +89,7 @@ class GetFromBodyParamTag extends Strategy
                 }
 
                 $type = $this->normalizeParameterType($type);
-                [$description, $example] = $this->parseParamDescription($description, $type);
+                [$description, $example] = $this->parseExampleFromParamDescription($description, $type);
                 $value = is_null($example) && ! $this->shouldExcludeExample($tag->getContent())
                     ? $this->generateDummyValue($type)
                     : $example;

+ 1 - 1
src/Extracting/Strategies/QueryParameters/GetFromQueryParamTag.php

@@ -88,7 +88,7 @@ class GetFromQueryParamTag extends Strategy
                     $required = trim($required) == 'required' ? true : false;
                 }
 
-                [$description, $value] = $this->parseParamDescription($description, 'string');
+                [$description, $value] = $this->parseExampleFromParamDescription($description, 'string');
                 if (is_null($value) && ! $this->shouldExcludeExample($tag->getContent())) {
                     $value = Str::contains($description, ['number', 'count', 'page'])
                         ? $this->generateDummyValue('integer')

+ 1 - 1
src/Extracting/Strategies/UrlParameters/GetFromUrlParamTag.php

@@ -83,7 +83,7 @@ class GetFromUrlParamTag extends Strategy
                     $required = trim($required) == 'required' ? true : false;
                 }
 
-                [$description, $value] = $this->parseParamDescription($description, 'string');
+                [$description, $value] = $this->parseExampleFromParamDescription($description, 'string');
                 if (is_null($value) && ! $this->shouldExcludeExample($tag->getContent())) {
                     $value = Str::contains($description, ['number', 'count', 'page'])
                         ? $this->generateDummyValue('integer')