Browse Source

Allow multiple required_if or required_unless rules

Marcel Pociot 8 years ago
parent
commit
181e4adea6

+ 16 - 2
src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

@@ -188,6 +188,20 @@ abstract class AbstractGenerator
         return implode($first, $arr);
     }
 
+    protected function splitValuePairs($parameters, $first = 'is ', $last = 'or ') {
+        $attribute = '';
+        collect($parameters)->map(function($item, $key) use (&$attribute, $first, $last) {
+            $attribute .= '`'.$item.'` ';
+            if (($key+1) % 2 === 0) {
+                $attribute .= $last;
+            } else {
+                $attribute .= $first;
+            }
+        });
+        $attribute = rtrim($attribute, $last);
+        return $attribute;
+    }
+
     /**
      * @param  string  $rule
      * @param  string  $ruleName
@@ -296,10 +310,10 @@ abstract class AbstractGenerator
                 $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription();
                 break;
             case 'required_if':
-                $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription();
+                $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription();
                 break;
             case 'required_unless':
-                $attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription();
+                $attributeData['description'][] = Description::parse($rule)->with($this->splitValuePairs($parameters))->getDescription();
                 break;
             case 'required_with':
                 $attributeData['description'][] = Description::parse($rule)->with($this->fancyImplode($parameters, ', ', ' or '))->getDescription();

+ 2 - 2
src/resources/lang/en/rules.php

@@ -20,8 +20,8 @@ return [
     'json' => 'Must be a valid JSON string.',
     'mimetypes' => 'Allowed mime types: :attribute',
     'mimes' => 'Allowed mime types: :attribute',
-    'required_if' => 'Required if `:attribute` is `:attribute`',
-    'required_unless' => 'Required unless `:attribute` is `:attribute`',
+    'required_if' => 'Required if :attribute',
+    'required_unless' => 'Required unless :attribute',
     'required_with' => 'Required if the parameters :attribute are present.',
     'required_with_all' => 'Required if the parameters :attribute are present.',
     'required_without' => 'Required if the parameters :attribute are not present.',

+ 6 - 0
tests/ApiDocGeneratorTest.php

@@ -267,6 +267,12 @@ class ApiDocGeneratorTest extends TestCase
                     $this->assertCount(1, $attribute['description']);
                     $this->assertSame('Must match this regular expression: `(.*)`', $attribute['description'][0]);
                     break;
+                case 'multiple_required_if':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if `foo` is `bar` or `baz` is `qux`', $attribute['description'][0]);
+                    break;
                 case 'required_if':
                     $this->assertFalse($attribute['required']);
                     $this->assertSame('string', $attribute['type']);

+ 6 - 0
tests/DingoGeneratorTest.php

@@ -270,6 +270,12 @@ class DingoGeneratorTest extends TestCase
                     $this->assertCount(1, $attribute['description']);
                     $this->assertSame('Must match this regular expression: `(.*)`', $attribute['description'][0]);
                     break;
+                case 'multiple_required_if':
+                    $this->assertFalse($attribute['required']);
+                    $this->assertSame('string', $attribute['type']);
+                    $this->assertCount(1, $attribute['description']);
+                    $this->assertSame('Required if `foo` is `bar` or `baz` is `qux`', $attribute['description'][0]);
+                    break;
                 case 'required_if':
                     $this->assertFalse($attribute['required']);
                     $this->assertSame('string', $attribute['type']);

+ 1 - 0
tests/Fixtures/DingoTestRequest.php

@@ -41,6 +41,7 @@ class DingoTestRequest extends FormRequest
             'numeric' => 'numeric',
             'regex' => 'regex:(.*)',
             'required_if' => 'required_if:foo,bar',
+            'multiple_required_if' => 'required_if:foo,bar,baz,qux',
             'required_unless' => 'required_unless:foo,bar',
             'required_with' => 'required_with:foo,bar,baz',
             'required_with_all' => 'required_with_all:foo,bar,baz',

+ 1 - 0
tests/Fixtures/TestRequest.php

@@ -41,6 +41,7 @@ class TestRequest extends FormRequest
             'numeric' => 'numeric',
             'regex' => 'regex:(.*)',
             'required_if' => 'required_if:foo,bar',
+            'multiple_required_if' => 'required_if:foo,bar,baz,qux',
             'required_unless' => 'required_unless:foo,bar',
             'required_with' => 'required_with:foo,bar,baz',
             'required_with_all' => 'required_with_all:foo,bar,baz',