88
99namespace PhpPkg \Http \Message ;
1010
11+ use InvalidArgumentException ;
1112use PhpPkg \Http \Message \Component \Collection ;
1213use PhpPkg \Http \Message \Request \RequestBody ;
1314use Psr \Http \Message \RequestInterface ;
1617use Psr \Http \Message \StreamInterface ;
1718use Psr \Http \Message \UploadedFileInterface ;
1819use Psr \Http \Message \UriInterface ;
20+ use RuntimeException ;
21+ use function array_change_key_case ;
22+ use function explode ;
23+ use function fopen ;
24+ use function getallheaders ;
25+ use function in_array ;
26+ use function is_array ;
27+ use function is_callable ;
28+ use function is_string ;
29+ use function parse_url ;
30+ use function preg_match ;
31+ use function strlen ;
32+ use function strpos ;
33+ use function strstr ;
34+ use function strtoupper ;
35+ use function substr ;
1936
2037/**
2138 * Class HttpFactory
@@ -46,12 +63,12 @@ class HttpFactory
4663 * @param string $method
4764 * @param UriInterface|string $uri
4865 * @return RequestInterface
49- * @throws \ RuntimeException
50- * @throws \ InvalidArgumentException
66+ * @throws RuntimeException
67+ * @throws InvalidArgumentException
5168 */
5269 public static function createRequest (string $ method , $ uri ): RequestInterface
5370 {
54- if (\ is_string ($ uri )) {
71+ if (is_string ($ uri )) {
5572 $ uri = Uri::createFromString ($ uri );
5673 }
5774
@@ -66,7 +83,7 @@ public static function createRequest(string $method, $uri): RequestInterface
6683 * Create a new response.
6784 * @param integer $code HTTP status code
6885 * @return ResponseInterface
69- * @throws \ InvalidArgumentException
86+ * @throws InvalidArgumentException
7087 */
7188 public static function createResponse (int $ code = 200 ): ResponseInterface
7289 {
@@ -82,12 +99,12 @@ public static function createResponse(int $code = 200): ResponseInterface
8299 * @param string $method
83100 * @param UriInterface|string $uri
84101 * @return ServerRequestInterface
85- * @throws \ RuntimeException
86- * @throws \ InvalidArgumentException
102+ * @throws RuntimeException
103+ * @throws InvalidArgumentException
87104 */
88105 public static function createServerRequest ($ method , $ uri ): ServerRequestInterface
89106 {
90- if (\ is_string ($ uri )) {
107+ if (is_string ($ uri )) {
91108 $ uri = Uri::createFromString ($ uri );
92109 }
93110
@@ -99,8 +116,8 @@ public static function createServerRequest($method, $uri): ServerRequestInterfac
99116 * @param array|mixed $server Typically $_SERVER or similar structure.
100117 * @param string|null $class The custom request class
101118 * @return ServerRequestInterface
102- * @throws \ RuntimeException
103- * @throws \ InvalidArgumentException
119+ * @throws RuntimeException
120+ * @throws InvalidArgumentException
104121 * If no valid method or URI can be determined.
105122 */
106123 public static function createServerRequestFromArray ($ server , string $ class = null ): ServerRequestInterface
@@ -122,7 +139,7 @@ public static function createServerRequestFromArray($server, string $class = nul
122139
123140 if (
124141 $ method === 'POST ' &&
125- \ in_array ($ request ->getMediaType (), ['application/x-www-form-urlencoded ' , 'multipart/form-data ' ], true )
142+ in_array ($ request ->getMediaType (), ['application/x-www-form-urlencoded ' , 'multipart/form-data ' ], true )
126143 ) {
127144 // parsed body must be $_POST
128145 $ request = $ request ->withParsedBody ($ _POST );
@@ -143,7 +160,7 @@ public static function createServerRequestFromRaw(string $rawData): ServerReques
143160
144161 // $rawData = trim($rawData);
145162 // split head and body
146- $ two = \ explode ("\r\n\r\n" , $ rawData , 2 );
163+ $ two = explode ("\r\n\r\n" , $ rawData , 2 );
147164
148165 if (!$ rawHeader = $ two [0 ] ?? '' ) {
149166 return new ServerRequest ('GET ' , Uri::createFromString ('/ ' ));
@@ -152,19 +169,19 @@ public static function createServerRequestFromRaw(string $rawData): ServerReques
152169 $ body = $ two [1 ] ? new RequestBody ($ two [1 ]) : null ;
153170
154171 /** @var array $list */
155- $ list = \ explode ("\n" , trim ($ rawHeader ));
172+ $ list = explode ("\n" , trim ($ rawHeader ));
156173
157174 // e.g: `GET / HTTP/1.1`
158175 $ first = \array_shift ($ list );
159176 // parse
160- [$ method , $ uri , $ protoStr ] = \array_map ('trim ' , \ explode (' ' , \trim ($ first )));
161- [$ protocol , $ protocolVersion ] = \ explode ('/ ' , $ protoStr );
177+ [$ method , $ uri , $ protoStr ] = \array_map ('trim ' , explode (' ' , \trim ($ first )));
178+ [$ protocol , $ protocolVersion ] = explode ('/ ' , $ protoStr );
162179
163180 // other header info
164181 $ headers = [];
165182 foreach ($ list as $ item ) {
166183 if ($ item ) {
167- [$ name , $ value ] = \ explode (': ' , \trim ($ item ));
184+ [$ name , $ value ] = explode (': ' , \trim ($ item ));
168185 $ headers [$ name ] = \trim ($ value );
169186 }
170187 }
@@ -177,13 +194,13 @@ public static function createServerRequestFromRaw(string $rawData): ServerReques
177194 $ port = 80 ;
178195 $ host = '' ;
179196 if ($ val = $ headers ['Host ' ] ?? '' ) {
180- [$ host , $ port ] = \ strpos ($ val , ': ' ) ? \ explode (': ' , $ val ) : [$ val , 80 ];
197+ [$ host , $ port ] = strpos ($ val , ': ' ) ? explode (': ' , $ val ) : [$ val , 80 ];
181198 }
182199
183200 $ path = $ uri ;
184201 $ query = $ fragment = '' ;
185- if (\ strlen ($ uri ) > 1 ) {
186- $ parts = \ parse_url ($ uri );
202+ if (strlen ($ uri ) > 1 ) {
203+ $ parts = parse_url ($ uri );
187204 $ path = $ parts ['path ' ] ?? '' ;
188205 $ query = $ parts ['query ' ] ?? '' ;
189206 $ fragment = $ parts ['fragment ' ] ?? '' ;
@@ -202,8 +219,8 @@ public static function createServerRequestFromRaw(string $rawData): ServerReques
202219 * The stream SHOULD be created with a temporary resource.
203220 * @param string $content
204221 * @return StreamInterface
205- * @throws \ RuntimeException
206- * @throws \ InvalidArgumentException
222+ * @throws RuntimeException
223+ * @throws InvalidArgumentException
207224 */
208225 public static function createStream (string $ content = '' ): StreamInterface
209226 {
@@ -218,12 +235,12 @@ public static function createStream(string $content = ''): StreamInterface
218235 * @param string $filename
219236 * @param string $mode
220237 * @return StreamInterface
221- * @throws \ InvalidArgumentException
238+ * @throws InvalidArgumentException
222239 */
223240 public static function createStreamFromFile (string $ filename , string $ mode = 'rb ' ): StreamInterface
224241 {
225242 // $stream = fopen('php://temp', $mode);
226- $ stream = \ fopen ($ filename , $ mode );
243+ $ stream = fopen ($ filename , $ mode );
227244
228245 return new Stream ($ stream );
229246 }
@@ -233,7 +250,7 @@ public static function createStreamFromFile(string $filename, string $mode = 'rb
233250 * The stream MUST be readable and may be writable.
234251 * @param resource $resource e.g `$resource = fopen('php://temp', 'r+');`
235252 * @return StreamInterface
236- * @throws \ InvalidArgumentException
253+ * @throws InvalidArgumentException
237254 */
238255 public static function createStreamFromResource ($ resource ): StreamInterface
239256 {
@@ -258,7 +275,7 @@ public static function createStreamFromResource($resource): StreamInterface
258275 * @param string $clientFilename
259276 * @param string $clientMediaType
260277 * @return UploadedFileInterface
261- * @throws \ InvalidArgumentException If the file resource is not readable.
278+ * @throws InvalidArgumentException If the file resource is not readable.
262279 */
263280 public static function createUploadedFile (
264281 $ file ,
@@ -278,7 +295,7 @@ public static function createUploadedFile(
278295 * Create a new URI.
279296 * @param string $uri
280297 * @return UriInterface
281- * @throws \ InvalidArgumentException If the given URI cannot be parsed.
298+ * @throws InvalidArgumentException If the given URI cannot be parsed.
282299 */
283300 public static function createUri (string $ uri = '' ): UriInterface
284301 {
@@ -288,7 +305,7 @@ public static function createUri(string $uri = ''): UriInterface
288305 /**
289306 * @param Collection|array $env
290307 * @return Uri
291- * @throws \ InvalidArgumentException
308+ * @throws InvalidArgumentException
292309 */
293310 public static function createUriFromArray ($ env ): Uri
294311 {
@@ -311,17 +328,17 @@ public static function createUriFromArray($env): Uri
311328
312329 // Authority: Port
313330 $ port = (int )$ env ->get ('SERVER_PORT ' , 80 );
314- if (\ preg_match ('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/ ' , $ host , $ matches )) {
331+ if (preg_match ('/^(\[[a-fA-F0-9:.]+\])(:\d+)?\z/ ' , $ host , $ matches )) {
315332 $ host = $ matches [1 ];
316333
317334 if ($ matches [2 ]) {
318- $ port = (int )\ substr ($ matches [2 ], 1 );
335+ $ port = (int )substr ($ matches [2 ], 1 );
319336 }
320337 } else {
321- $ pos = \ strpos ($ host , ': ' );
338+ $ pos = strpos ($ host , ': ' );
322339 if ($ pos !== false ) {
323- $ port = (int )\ substr ($ host , $ pos + 1 );
324- $ host = \ strstr ($ host , ': ' , true );
340+ $ port = (int )substr ($ host , $ pos + 1 );
341+ $ host = strstr ($ host , ': ' , true );
325342 }
326343 }
327344
@@ -331,12 +348,12 @@ public static function createUriFromArray($env): Uri
331348
332349 // parse_url() requires a full URL. As we don't extract the domain name or scheme,
333350 // we use a stand-in.
334- $ uriPath = \ parse_url ('http://abc.com ' . $ env ->get ('REQUEST_URI ' ), PHP_URL_PATH );
351+ $ uriPath = parse_url ('http://abc.com ' . $ env ->get ('REQUEST_URI ' ), PHP_URL_PATH );
335352
336353 // Query string
337354 $ queryString = $ env ->get ('QUERY_STRING ' , '' );
338355 if ($ queryString === '' ) {
339- $ queryString = \ parse_url ('http://abc.com ' . $ env ->get ('REQUEST_URI ' ), PHP_URL_QUERY );
356+ $ queryString = parse_url ('http://abc.com ' . $ env ->get ('REQUEST_URI ' ), PHP_URL_QUERY );
340357 }
341358
342359 // Fragment
@@ -363,7 +380,7 @@ public static function createHeadersFromArray($env): Headers
363380 $ env = self ::determineAuthorization ($ env );
364381
365382 foreach ($ env as $ key => $ value ) {
366- $ key = \ strtoupper ($ key );
383+ $ key = strtoupper ($ key );
367384
368385 if (isset (static ::$ special [$ key ]) || strpos ($ key , 'HTTP_ ' ) === 0 ) {
369386 if ($ key !== 'HTTP_CONTENT_LENGTH ' ) {
@@ -385,9 +402,9 @@ public static function determineAuthorization($env): Collection
385402 {
386403 $ authorization = $ env ->get ('HTTP_AUTHORIZATION ' );
387404
388- if (null === $ authorization && \ is_callable ('getallheaders ' )) {
389- $ headers = \ getallheaders ();
390- $ headers = \ array_change_key_case ($ headers , CASE_LOWER );
405+ if (null === $ authorization && is_callable ('getallheaders ' )) {
406+ $ headers = getallheaders ();
407+ $ headers = array_change_key_case ($ headers , CASE_LOWER );
391408
392409 if (isset ($ headers ['authorization ' ])) {
393410 $ env ->set ('HTTP_AUTHORIZATION ' , $ headers ['authorization ' ]);
@@ -403,7 +420,7 @@ public static function determineAuthorization($env): Collection
403420 */
404421 public static function ensureIsCollection ($ data ): Collection
405422 {
406- if (\ is_array ($ data )) {
423+ if (is_array ($ data )) {
407424 return new Collection ($ data );
408425 }
409426
0 commit comments