Browse Source

format form data params properly

Aaron Florey 4 years ago
parent
commit
df76adba71
1 changed files with 30 additions and 8 deletions
  1. 30 8
      src/Writing/PostmanCollectionWriter.php

+ 30 - 8
src/Writing/PostmanCollectionWriter.php

@@ -144,14 +144,7 @@ class PostmanCollectionWriter
 
         switch ($inputMode) {
             case 'formdata':
-                foreach ($endpoint['cleanBodyParameters'] as $key => $value) {
-                    $params = [
-                        'key' => $key,
-                        'value' => $value,
-                        'type' => 'text',
-                    ];
-                    $body[$inputMode][] = $params;
-                }
+                $body[$inputMode] = $this->getFormDataParams($endpoint['cleanBodyParameters']);
                 foreach ($endpoint['fileParameters'] as $key => $value) {
                     while (is_array($value)) { // For arrays of files, just send the first one
                         $key .= '[]';
@@ -172,6 +165,35 @@ class PostmanCollectionWriter
         return $body;
     }
 
+    /**
+	 * This formats the Form Paramaters correctly for Arrays eg. data[item][index] = value
+	 * @param array $array
+	 * @param string|null $key
+	 * @return array
+	 */
+	protected function getFormDataParams(array $array, ?string $key = null): array
+	{
+		$body = [];
+
+		foreach ($array as $index => $value) {
+			$index = $key ? ($key . '[' . $index . ']') : $index;
+
+			if (!is_array($value)) {
+				$body[] = [
+					'key'   => $index,
+					'value' => $value,
+					'type'  => 'text',
+				];
+
+				continue;
+			}
+
+			$body = array_merge($body, $this->getFormDataParams($value, $index));
+		}
+
+		return $body;
+	}
+
     protected function resolveHeadersForEndpoint($route)
     {
         [$where, $authParam] = $this->getAuthParamToExclude();