@@ -48,6 +48,9 @@ public function getResponse()
48
48
*/
49
49
public function setHeadersFromArray (array $ headers )
50
50
{
51
+ $ status = array_shift ($ headers );
52
+ $ this ->setStatus ($ status );
53
+
51
54
foreach ($ headers as $ headerLine ) {
52
55
$ headerLine = trim ($ headerLine );
53
56
if ('' === $ headerLine ) {
@@ -89,44 +92,56 @@ public function setHeadersFromString($headers)
89
92
return $ this ;
90
93
}
91
94
95
+ /**
96
+ * Set response status from a status string.
97
+ *
98
+ * @param string $statusLine Response status as a string.
99
+ *
100
+ * @return $this
101
+ *
102
+ * @ throws \UnexpectedValueException For invalid header values.
103
+ * @throws \InvalidArgumentException For invalid status line.
104
+ */
105
+ public function setStatus ($ statusLine )
106
+ {
107
+ $ parts = explode (' ' , $ statusLine , 3 );
108
+ if (count ($ parts ) < 2 || strpos (strtolower ($ parts [0 ]), 'http/ ' ) !== 0 ) {
109
+ throw new \InvalidArgumentException (
110
+ sprintf ('"%s" is not a valid HTTP status line ' , $ statusLine )
111
+ );
112
+ }
113
+
114
+ $ reasonPhrase = count ($ parts ) > 2 ? $ parts [2 ] : '' ;
115
+ $ this ->response = $ this ->response
116
+ ->withStatus ((int ) $ parts [1 ], $ reasonPhrase )
117
+ ->withProtocolVersion (substr ($ parts [0 ], 5 ));
118
+
119
+ return $ this ;
120
+ }
121
+
92
122
/**
93
123
* Add header represented by a string.
94
124
*
95
125
* @param string $headerLine Response header as a string.
96
126
*
97
127
* @return $this
98
128
*
99
- * @throws \UnexpectedValueException For invalid header values.
100
129
* @throws \InvalidArgumentException For invalid header names or values.
101
130
*/
102
131
public function addHeader ($ headerLine )
103
132
{
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 ));
133
+ $ parts = explode (': ' , $ headerLine , 2 );
134
+ if (count ($ parts ) !== 2 ) {
135
+ throw new \InvalidArgumentException (
136
+ sprintf ('"%s" is not a valid HTTP header line ' , $ headerLine )
137
+ );
138
+ }
139
+ $ name = trim (urldecode ($ parts [0 ]));
140
+ $ value = trim (urldecode ($ parts [1 ]));
141
+ if ($ this ->response ->hasHeader ($ name )) {
142
+ $ this ->response = $ this ->response ->withAddedHeader ($ name , $ value );
116
143
} 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
- }
144
+ $ this ->response = $ this ->response ->withHeader ($ name , $ value );
130
145
}
131
146
132
147
return $ this ;
0 commit comments