@@ -48,38 +48,13 @@ public function getResponse()
48
48
*/
49
49
public function setHeadersFromArray (array $ headers )
50
50
{
51
- $ statusLine = trim (array_shift ($ headers ));
52
- $ parts = explode (' ' , $ statusLine , 3 );
53
- if (count ($ parts ) < 2 || substr (strtolower ($ parts [0 ]), 0 , 5 ) !== 'http/ ' ) {
54
- throw new \UnexpectedValueException (
55
- sprintf ('"%s" is not a valid HTTP status line ' , $ statusLine )
56
- );
57
- }
58
-
59
- $ reasonPhrase = count ($ parts ) > 2 ? $ parts [2 ] : '' ;
60
- $ this ->response = $ this ->response
61
- ->withStatus ((int ) $ parts [1 ], $ reasonPhrase )
62
- ->withProtocolVersion (substr ($ parts [0 ], 5 ));
63
-
64
51
foreach ($ headers as $ headerLine ) {
65
52
$ headerLine = trim ($ headerLine );
66
53
if ('' === $ headerLine ) {
67
54
continue ;
68
55
}
69
56
70
- $ parts = explode (': ' , $ headerLine , 2 );
71
- if (count ($ parts ) !== 2 ) {
72
- throw new \UnexpectedValueException (
73
- sprintf ('"%s" is not a valid HTTP header line ' , $ headerLine )
74
- );
75
- }
76
- $ name = trim (urldecode ($ parts [0 ]));
77
- $ value = trim (urldecode ($ parts [1 ]));
78
- if ($ this ->response ->hasHeader ($ name )) {
79
- $ this ->response = $ this ->response ->withAddedHeader ($ name , $ value );
80
- } else {
81
- $ this ->response = $ this ->response ->withHeader ($ name , $ value );
82
- }
57
+ $ this ->addHeader ($ headerLine );
83
58
}
84
59
85
60
return $ this ;
@@ -113,4 +88,47 @@ public function setHeadersFromString($headers)
113
88
114
89
return $ this ;
115
90
}
91
+
92
+ /**
93
+ * Add header represented by a string.
94
+ *
95
+ * @param string $headerLine Response header as a string.
96
+ *
97
+ * @return $this
98
+ *
99
+ * @throws \UnexpectedValueException For invalid header values.
100
+ * @throws \InvalidArgumentException For invalid header names or values.
101
+ */
102
+ public function addHeader ($ headerLine )
103
+ {
104
+ if (strpos (strtolower ($ headerLine ), 'http/ ' ) === 0 ) {
105
+ $ parts = explode (' ' , $ headerLine , 3 );
106
+ if (count ($ parts ) < 2 ) {
107
+ throw new \UnexpectedValueException (
108
+ sprintf ('"%s" is not a valid HTTP status line ' , $ headerLine )
109
+ );
110
+ }
111
+
112
+ $ reasonPhrase = count ($ parts ) > 2 ? $ parts [2 ] : '' ;
113
+ $ this ->response = $ this ->response
114
+ ->withStatus ((int ) $ parts [1 ], $ reasonPhrase )
115
+ ->withProtocolVersion (substr ($ parts [0 ], 5 ));
116
+ } else {
117
+ $ parts = explode (': ' , $ headerLine , 2 );
118
+ if (count ($ parts ) !== 2 ) {
119
+ throw new \UnexpectedValueException (
120
+ sprintf ('"%s" is not a valid HTTP header line ' , $ headerLine )
121
+ );
122
+ }
123
+ $ name = trim (urldecode ($ parts [0 ]));
124
+ $ value = trim (urldecode ($ parts [1 ]));
125
+ if ($ this ->response ->hasHeader ($ name )) {
126
+ $ this ->response = $ this ->response ->withAddedHeader ($ name , $ value );
127
+ } else {
128
+ $ this ->response = $ this ->response ->withHeader ($ name , $ value );
129
+ }
130
+ }
131
+
132
+ return $ this ;
133
+ }
116
134
}
0 commit comments