Skip to content

Commit 6ce9f9d

Browse files
committed
v3.4.3
1 parent 75aa45d commit 6ce9f9d

15 files changed

Lines changed: 43 additions & 205 deletions

README.md

Lines changed: 3 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,5 @@
1-
# HTTP message wrapper for PHP 7.4+, based on RFC-7230, PSR-7, and PSR-17.
1+
# An HTTP message implementation based on PSR-7, PSR-17 and RFC-7230
22

3-
[![Build Status](https://scrutinizer-ci.com/g/sunrise-php/http-message/badges/build.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-message/build-status/master)
4-
[![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/http-message/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-message/?branch=master)
5-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sunrise-php/http-message/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-message/?branch=master)
6-
[![Total Downloads](https://poser.pugx.org/sunrise/http-message/downloads?format=flat)](https://packagist.org/packages/sunrise/http-message)
7-
[![Latest Stable Version](https://poser.pugx.org/sunrise/http-message/v/stable?format=flat)](https://packagist.org/packages/sunrise/http-message)
8-
[![License](https://poser.pugx.org/sunrise/http-message/license?format=flat)](https://packagist.org/packages/sunrise/http-message)
3+
## Resources
94

10-
---
11-
12-
## Installation
13-
14-
```bash
15-
composer require sunrise/http-message
16-
```
17-
18-
## How to Use
19-
20-
We highly recommend studying [PSR-7](https://www.php-fig.org/psr/psr-7/) and [PSR-17](https://www.php-fig.org/psr/psr-17/), as only basic examples are provided below.
21-
22-
### Server Request from Global Environment
23-
24-
```php
25-
$request = \Sunrise\Http\Message\ServerRequestFactory::fromGlobals();
26-
```
27-
28-
### Typed Messages
29-
30-
#### JSON Request
31-
32-
```php
33-
$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', ['foo' => 'bar']);
34-
```
35-
36-
You can also specify [encoding flags](https://www.php.net/manual/en/json.constants.php#constant.json-hex-tag) and the maximum nesting depth as shown below:
37-
38-
```php
39-
$request = new \Sunrise\Http\Message\Request\JsonRequest('POST', '/', [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512);
40-
```
41-
42-
#### URL Encoded Request
43-
44-
```php
45-
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', ['foo' => 'bar']);
46-
```
47-
48-
You can also specify the [encoding type](https://www.php.net/manual/ru/url.constants.php#constant.php-query-rfc1738) as shown below:
49-
50-
```php
51-
$rfc1738 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC1738;
52-
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc1738);
53-
```
54-
55-
```php
56-
$rfc3986 = \Sunrise\Http\Message\Request\UrlEncodedRequest::ENCODING_TYPE_RFC3986;
57-
$request = new \Sunrise\Http\Message\Request\UrlEncodedRequest('POST', '/', [], $rfc3986);
58-
```
59-
60-
#### JSON Response
61-
62-
```php
63-
$response = new \Sunrise\Http\Message\Response\JsonResponse(200, ['foo' => 'bar']);
64-
```
65-
66-
You can also specify [encoding flags](https://www.php.net/manual/en/json.constants.php#constant.json-hex-tag) and the maximum nesting depth as shown below:
67-
68-
```php
69-
$response = new \Sunrise\Http\Message\Response\JsonResponse(200, [], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 512);
70-
```
71-
72-
#### HTML Response
73-
74-
```php
75-
$response = new \Sunrise\Http\Message\Response\HtmlResponse(200, '<h1>Welcome!</h1>');
76-
```
77-
78-
### Streams
79-
80-
#### File Stream
81-
82-
```php
83-
$stream = new \Sunrise\Http\Message\Stream\FileStream('/folder/file', 'r+b');
84-
```
85-
86-
#### Input Stream
87-
88-
More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).
89-
90-
```php
91-
$stream = new \Sunrise\Http\Message\Stream\PhpInputStream();
92-
```
93-
94-
#### Memory Stream
95-
96-
More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).
97-
98-
```php
99-
$stream = new \Sunrise\Http\Message\Stream\PhpMemoryStream('r+b');
100-
```
101-
102-
#### Temporary Stream
103-
104-
More details about this stream can be found on the [official page](https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input).
105-
106-
```php
107-
$stream = new \Sunrise\Http\Message\Stream\PhpTempStream('r+b');
108-
```
109-
110-
You can also specify a memory limit. When this limit is reached, PHP will switch to using a temporary file instead of memory.
111-
112-
> Please note that the default memory limit is 2MB.
113-
114-
```php
115-
$stream = new \Sunrise\Http\Message\Stream\PhpTempStream('r+b', 1e+6);
116-
```
117-
118-
#### Temporary File Stream
119-
120-
For more details about the behavior of temporary files, visit [the official page](https://www.php.net/manual/en/function.tmpfile).
121-
122-
The stream opens a unique temporary file in binary read/write mode (w+b). The file will be automatically deleted when it is closed or when the program terminates.
123-
124-
```php
125-
$stream = new \Sunrise\Http\Message\Stream\TmpfileStream();
126-
$stream->getMetadata('uri'); // the file path
127-
```
128-
129-
If you don't require the behavior described above, you can use an alternative temporary file stream:
130-
131-
```php
132-
$stream = new \Sunrise\Http\Message\Stream\TempFileStream();
133-
$stream->getMetadata('uri'); // the file path
134-
```
135-
136-
### PSR-7 and PSR-17
137-
138-
The following classes are implementations PSR-7:
139-
140-
- `Sunrise\Http\Message\Request`
141-
- `Sunrise\Http\Message\Response`
142-
- `Sunrise\Http\Message\ServerRequest`
143-
- `Sunrise\Http\Message\Stream`
144-
- `Sunrise\Http\Message\UploadedFile`
145-
- `Sunrise\Http\Message\Uri`
146-
147-
The following classes are implementations PSR-17:
148-
149-
- `Sunrise\Http\Message\RequestFactory`
150-
- `Sunrise\Http\Message\ResponseFactory`
151-
- `Sunrise\Http\Message\ServerRequestFactory`
152-
- `Sunrise\Http\Message\StreamFactory`
153-
- `Sunrise\Http\Message\UploadedFileFactory`
154-
- `Sunrise\Http\Message\UriFactory`
155-
156-
### Error Handling
157-
158-
Any exceptions thrown by this package can be caught through the following interface:
159-
160-
```php
161-
try {
162-
} catch (\Sunrise\Http\Message\Exception\ExceptionInterface $e) {
163-
}
164-
```
165-
166-
---
167-
168-
## Test Run
169-
170-
```bash
171-
composer test
172-
```
173-
174-
## Useful Links
175-
176-
- https://tools.ietf.org/html/rfc7230
177-
- https://www.php-fig.org/psr/psr-7/
178-
- https://www.php-fig.org/psr/psr-17/
5+
- [Documentation](https://dev.sunrise-studio.io/docs/packages/sunrise/http-message/)

composer.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
{
22
"name": "sunrise/http-message",
33
"homepage": "https://github.com/sunrise-php/http-message",
4-
"description": "HTTP message implementation for PHP 7.4+ based on RFC-7230, PSR-7 and PSR-17.",
4+
"description": "An HTTP message implementation based on PSR-7, PSR-17 and RFC-7230.",
55
"license": "MIT",
66
"keywords": [
77
"fenric",
88
"sunrise",
99
"http",
10-
"header",
11-
"message",
1210
"request",
1311
"response",
14-
"stream",
15-
"uri",
16-
"upload",
17-
"rfc-7230",
1812
"psr-7",
19-
"psr-17"
13+
"psr-17",
14+
"rfc-7230"
2015
],
2116
"authors": [
2217
{

psalm.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
</projectFiles>
1515

1616
<issueHandlers>
17-
<ClassMustBeFinal errorLevel="suppress" />
1817
<MissingOverrideAttribute errorLevel="suppress" />
1918
<PossiblyUnusedMethod errorLevel="suppress" />
2019
<PossiblyUnusedReturnValue errorLevel="suppress" />

src/Exception/InvalidArgumentException.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Sunrise\Http\Message\Exception;
1313

14+
/**
15+
* @psalm-suppress ClassMustBeFinal
16+
*/
1417
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
1518
{
1619
}

src/Exception/RuntimeException.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Sunrise\Http\Message\Exception;
1313

14+
/**
15+
* @psalm-suppress ClassMustBeFinal
16+
*/
1417
class RuntimeException extends \RuntimeException implements ExceptionInterface
1518
{
1619
}

src/Request/UrlEncodedRequest.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@
1515
use Sunrise\Http\Message\Exception\InvalidArgumentException;
1616
use Sunrise\Http\Message\Request;
1717
use Sunrise\Http\Message\Stream\PhpTempStream;
18-
use TypeError;
1918

20-
use function gettype;
2119
use function http_build_query;
22-
use function is_array;
23-
use function is_object;
24-
use function sprintf;
2520

2621
use const PHP_QUERY_RFC1738;
2722
use const PHP_QUERY_RFC3986;
@@ -36,32 +31,21 @@ final class UrlEncodedRequest extends Request
3631

3732
/**
3833
* @param mixed $uri
39-
* @param array<array-key, mixed>|object $data
34+
* @param mixed $data
4035
* @param self::ENCODING_TYPE_* $encodingType
4136
*
4237
* @throws InvalidArgumentException
4338
*/
4439
public function __construct(string $method, $uri, $data, int $encodingType = self::ENCODING_TYPE_RFC1738)
4540
{
46-
/**
47-
* @psalm-suppress DocblockTypeContradiction
48-
* @phpstan-ignore-next-line
49-
*/
50-
if (!is_array($data) && !is_object($data)) {
51-
throw new TypeError(sprintf(
52-
'Argument #3 ($data) must be of type string, %s given',
53-
gettype($data),
54-
));
55-
}
56-
5741
parent::__construct($method, $uri);
5842

5943
$this->setBody(self::createBody($data, $encodingType));
6044
$this->setHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
6145
}
6246

6347
/**
64-
* @param array<array-key, mixed>|object $data
48+
* @param mixed $data
6549
* @param self::ENCODING_TYPE_* $encodingType
6650
*/
6751
private static function createBody($data, int $encodingType): StreamInterface
@@ -70,10 +54,10 @@ private static function createBody($data, int $encodingType): StreamInterface
7054
return $data;
7155
}
7256

73-
$encodedData = http_build_query($data, '', '&', $encodingType);
57+
$query = http_build_query((array) $data, '', '&', $encodingType);
7458

7559
$stream = new PhpTempStream('r+b');
76-
$stream->write($encodedData);
60+
$stream->write($query);
7761
$stream->rewind();
7862

7963
return $stream;

src/RequestFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Psr\Http\Message\RequestFactoryInterface;
1515
use Psr\Http\Message\RequestInterface;
1616

17+
/**
18+
* @psalm-suppress ClassMustBeFinal
19+
*/
1720
class RequestFactory implements RequestFactoryInterface
1821
{
1922
/**

src/ResponseFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Psr\Http\Message\ResponseFactoryInterface;
1515
use Psr\Http\Message\ResponseInterface;
1616

17+
/**
18+
* @psalm-suppress ClassMustBeFinal
19+
*/
1720
class ResponseFactory implements ResponseFactoryInterface
1821
{
1922
/**

src/ServerRequest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use function is_array;
2222
use function is_object;
2323

24+
/**
25+
* @psalm-suppress ClassMustBeFinal
26+
*/
2427
class ServerRequest extends Request implements ServerRequestInterface
2528
{
2629
/**

src/ServerRequestFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Psr\Http\Message\ServerRequestInterface;
1616
use Sunrise\Http\Message\Stream\PhpInputStream;
1717

18+
/**
19+
* @psalm-suppress ClassMustBeFinal
20+
*/
1821
class ServerRequestFactory implements ServerRequestFactoryInterface
1922
{
2023
/**

0 commit comments

Comments
 (0)