@@ -213,8 +213,27 @@ public function addHeaderLine($headerFieldNameOrLine, $fieldValue = null)
213
213
*/
214
214
public function addHeader (Header \HeaderInterface $ header )
215
215
{
216
- $ this ->headersKeys [] = static ::createKey ($ header ->getFieldName ());
217
- $ this ->headers [] = $ header ;
216
+ $ key = static ::createKey ($ header ->getFieldName ());
217
+ $ index = array_search ($ key , $ this ->headersKeys );
218
+
219
+ // No header by that key presently; append key and header to list.
220
+ if ($ index === false ) {
221
+ $ this ->headersKeys [] = $ key ;
222
+ $ this ->headers [] = $ header ;
223
+ return $ this ;
224
+ }
225
+
226
+ // Header exists, and is a multi-value header; append key and header to
227
+ // list (as multi-value headers are aggregated on retrieval)
228
+ $ class = ($ this ->getPluginClassLoader ()->load (str_replace ('- ' , '' , $ key ))) ?: Header \GenericHeader::class;
229
+ if (in_array (Header \MultipleHeaderInterface::class, class_implements ($ class , true ))) {
230
+ $ this ->headersKeys [] = $ key ;
231
+ $ this ->headers [] = $ header ;
232
+ return $ this ;
233
+ }
234
+
235
+ // Otherwise, we replace the current instance.
236
+ $ this ->headers [$ index ] = $ header ;
218
237
219
238
return $ this ;
220
239
}
@@ -286,6 +305,7 @@ public function get($name)
286
305
if (is_array ($ this ->headers [$ index ])) {
287
306
return $ this ->lazyLoadHeader ($ index );
288
307
}
308
+
289
309
return $ this ->headers [$ index ];
290
310
}
291
311
0 commit comments