@@ -53,6 +53,7 @@ public function addPath($name, $argument = null) {
5353
5454 public function addPathVariable ($ variable ) {
5555 $ this ->path [] = $ variable ;
56+ return $ this ;
5657 }
5758
5859 public function getUrl () {
@@ -85,18 +86,18 @@ public function dump() {
8586 return $ this ;
8687 }
8788
88- public function addFile ($ file_path ) {
89- $ this ->files [] = $ filePath ;
89+ public function addFile ($ field , $ file_path ) {
90+ $ this ->files [$ field ] = $ file_path ;
91+ return $ this ;
9092 }
9193
9294 public function addFormParam ($ key , $ value ) {
9395 $ this ->form_params [$ key ] = $ value ;
96+ return $ this ;
9497 }
9598
9699 public function call () {
97-
98100 $ client = new Client ();
99- $ method = $ this ->method ;
100101
101102 try {
102103
@@ -106,15 +107,8 @@ public function call() {
106107 ];
107108
108109 if (!empty ($ this ->files )) {
109- foreach ($ this ->files as $ file ) {
110- $ data ['multipart ' ][] = [
111- 'name ' => basename ($ file ),
112- 'contents ' => $ file
113- ];
114- }
115- }
116-
117- if (!empty ($ this ->form_params )) {
110+ $ data ['multipart ' ] = $ this ->buildMultiPart ();
111+ } else if (!empty ($ this ->form_params )) {
118112 $ data ['form_params ' ] = $ this ->form_params ;
119113 }
120114
@@ -126,7 +120,6 @@ public function call() {
126120 } catch (RequestException $ e ) {
127121 throw $ e ;
128122 }
129-
130123 }
131124
132125 public function withHeaders ($ headers ) {
@@ -170,4 +163,22 @@ public function withParams($params) {
170163 return $ this ;
171164 }
172165
166+ private function buildMultiPart () {
167+ $ multipart = array ();
168+
169+ foreach ($ this ->files as $ field => $ file ) {
170+ $ multipart [] = [
171+ 'name ' => $ field ,
172+ 'contents ' => fopen ($ file , 'r ' )
173+ ];
174+ }
175+ foreach ($ this ->form_params as $ param => $ value ) {
176+ $ multipart [] = [
177+ 'name ' => $ param ,
178+ 'contents ' => $ value
179+ ];
180+ }
181+ return $ multipart ;
182+ }
183+
173184}
0 commit comments