瀏覽代碼

Allows users to exclude a parameter from having an example
- Includes 'No-example' in the doc-block
- Useful for optional params in the request, or those that conflict with other params (ie only one of two are allowed)
- Implement with a simple `strpos()` search

Marnu Lombard 5 年之前
父節點
當前提交
1df54a8c8d
共有 1 個文件被更改,包括 19 次插入0 次删除
  1. 19 0
      src/Tools/Generator.php

+ 19 - 0
src/Tools/Generator.php

@@ -130,6 +130,9 @@ class Generator
             ->filter(function ($tag) {
                 return $tag instanceof Tag && $tag->getName() === 'bodyParam';
             })
+            ->filter(function (Tag $tag) {
+                return !$this->shouldExcludeExample($tag);
+            })
             ->mapWithKeys(function ($tag) {
                 preg_match('/(.+?)\s+(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content);
                 if (empty($content)) {
@@ -205,6 +208,9 @@ class Generator
             ->filter(function ($tag) {
                 return $tag instanceof Tag && $tag->getName() === 'queryParam';
             })
+            ->filter(function (Tag $tag) {
+                return !$this->shouldExcludeExample($tag);
+            })
             ->mapWithKeys(function ($tag) {
                 preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content);
                 if (empty($content)) {
@@ -367,6 +373,19 @@ class Generator
         return [$description, $example];
     }
 
+    /**
+     * Allows users to specify that we shouldn't generate an example for the parameter
+     * by writing 'No-example'
+     *
+     * @param Tag $tag
+     *
+     * @return bool Whether no example should be generated
+     */
+    private function shouldExcludeExample(Tag $tag)
+    {
+        return strpos($tag->getContent(), ' No-example') !== false;
+    }
+
     /**
      * Cast a value from a string to a specified type.
      *