Skip to content

Commit a34afa6

Browse files
committed
Add require dependency httpsoft/http-message
1 parent ecc3771 commit a34afa6

22 files changed

+85
-618
lines changed

README.md

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,7 @@ This package requires PHP version 7.4 or later.
2222
composer require httpsoft/http-response
2323
```
2424

25-
## Usage Response
26-
27-
```php
28-
use HttpSoft\Response\Response;
29-
use HttpSoft\Response\ResponseFactory;
30-
31-
$response = ResponseFactory::create();
32-
// equivalently to:
33-
$response = new Response();
34-
// default values
35-
$response->getStatusCode(); // 200
36-
$response->getReasonPhrase(); // 'OK'
37-
$response->getBody()->getContents(); // ''
38-
$response->getBody()->getMetadata('uri') // 'php://temp'
39-
$response->getHeaders(); // []
40-
$response->getProtocolVersion(); // '1.1'
41-
42-
// Create with the passed parameters
43-
$response = new Response(404, ['Content-Language' => 'en'], 'php://memory', '2');
44-
$response->getStatusCode(); // 404
45-
$response->getReasonPhrase(); // 'Not Found'
46-
$response->getBody()->getContents(); // ''
47-
$response->getBody()->getMetadata('uri') // 'php://memory'
48-
$response->getHeaders(); // ['Content-Language' => ['en']]
49-
$response->getProtocolVersion(); // '2'
50-
51-
// Write to the response body:
52-
$response->getBody()->write('Content');
53-
$response->getBody()->getContents(); // 'Content'
54-
55-
// With `Content-Type` header:
56-
$newResponse = $response->withHeader('Content-Type', 'text/plain');
57-
$newResponse->getHeaderLine('content-type'); // 'text/plain'
58-
$newResponse->getHeaders(); // ['Content-Language' => ['ru'], 'Content-Type' => ['text/plain']]
59-
60-
// With status code:
61-
$newResponse = $response->withStatus(Response::STATUS_INTERNAL_SERVER_ERROR);
62-
$newResponse->getStatusCode(); // 500
63-
$newResponse->getReasonPhrase(); // 'Internal Server Error'
64-
65-
// With status code and reason phrase:
66-
$newResponse = $response->withStatus(599, 'Custom Phrase');
67-
$newResponse->getStatusCode(); // 599
68-
$newResponse->getReasonPhrase(); // 'Custom Phrase'
69-
```
70-
71-
## Create custom responses
25+
## Usage
7226

7327
```php
7428
// Create `Psr\Http\Message\ResponseInterface` instance from HTML:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"require": {
2222
"php": "^7.4|^8.0",
23-
"httpsoft/http-stream": "^1.1"
23+
"httpsoft/http-message": "^1.0"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "^9.3",

psalm.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
<issueHandlers>
1717
<DocblockTypeContradiction errorLevel="info" />
1818
<MixedAssignment errorLevel="info" />
19-
<MixedArgumentTypeCoercion errorLevel="info" />
20-
<MixedPropertyTypeCoercion errorLevel="info" />
2119
<PropertyNotSetInConstructor errorLevel="info" />
2220
<RedundantCondition errorLevel="info" />
23-
<RedundantConditionGivenDocblockType errorLevel="info" />
2421
<TypeDoesNotContainType errorLevel="info" />
2522
</issueHandlers>
2623
</psalm>

src/ContentTrait.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/EmptyResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace HttpSoft\Response;
66

7-
use HttpSoft\Stream\StreamFactory;
7+
use HttpSoft\Message\Stream;
88
use Psr\Http\Message\ResponseInterface;
99

1010
final class EmptyResponse implements ResponseInterface, ResponseStatusCodeInterface
1111
{
12-
use ResponseTrait;
12+
use ResponseExtensionTrait;
1313

1414
/**
1515
* @param int $code
@@ -23,6 +23,6 @@ public function __construct(
2323
string $protocol = '1.1',
2424
string $reasonPhrase = ''
2525
) {
26-
$this->init($code, $reasonPhrase, $headers, StreamFactory::create('php://temp', 'r'), $protocol);
26+
$this->init($code, $reasonPhrase, $headers, new Stream('php://temp', 'r'), $protocol);
2727
}
2828
}

src/HtmlResponse.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
namespace HttpSoft\Response;
66

7-
use HttpSoft\Stream\StreamFactory;
87
use Psr\Http\Message\ResponseInterface;
98

109
final class HtmlResponse implements ResponseInterface, ResponseStatusCodeInterface
1110
{
12-
use ResponseTrait;
11+
use ResponseExtensionTrait;
1312

1413
/**
1514
* @param string $html
@@ -25,7 +24,7 @@ public function __construct(
2524
string $protocol = '1.1',
2625
string $reasonPhrase = ''
2726
) {
28-
$this->init($code, $reasonPhrase, $headers, StreamFactory::createFromContent($html), $protocol);
27+
$this->init($code, $reasonPhrase, $headers, $this->createBody($html), $protocol);
2928
$this->setContentTypeHeaderIfNotExists('text/html; charset=UTF-8');
3029
}
3130
}

src/JsonResponse.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace HttpSoft\Response;
66

7-
use HttpSoft\Stream\StreamFactory;
87
use InvalidArgumentException;
98
use JsonException;
109
use Psr\Http\Message\ResponseInterface;
@@ -25,7 +24,7 @@
2524

2625
final class JsonResponse implements ResponseInterface, ResponseStatusCodeInterface
2726
{
28-
use ResponseTrait;
27+
use ResponseExtensionTrait;
2928

3029
/**
3130
* Default options for `json_encode()`.
@@ -55,7 +54,7 @@ public function __construct(
5554
int $encodingOptions = self::DEFAULT_OPTIONS
5655
) {
5756
$json = $this->encode($data, $encodingOptions);
58-
$this->init($code, $reasonPhrase, $headers, StreamFactory::createFromContent($json), $protocol);
57+
$this->init($code, $reasonPhrase, $headers, $this->createBody($json), $protocol);
5958
$this->setContentTypeHeaderIfNotExists('application/json; charset=UTF-8');
6059
}
6160

@@ -66,7 +65,7 @@ public function __construct(
6665
*/
6766
public function withJsonData($data, int $encodingOptions = self::DEFAULT_OPTIONS): self
6867
{
69-
return $this->withBody(StreamFactory::createFromContent($this->encode($data, $encodingOptions)));
68+
return $this->withBody($this->createBody($this->encode($data, $encodingOptions)));
7069
}
7170

7271
/**

src/RedirectResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
final class RedirectResponse implements ResponseInterface, ResponseStatusCodeInterface
1010
{
11-
use ResponseTrait;
11+
use ResponseExtensionTrait;
1212

1313
/**
1414
* @param string $uri

src/ResponseExtensionTrait.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace HttpSoft\Response;
6+
7+
use HttpSoft\Message\ResponseTrait;
8+
use HttpSoft\Message\Stream;
9+
10+
/**
11+
* Trait extends the standard functionality defined in `Psr\Http\Message\ResponseInterface`.
12+
*
13+
* @see https://github.com/php-fig/http-message/tree/master/src/ResponseInterface.php
14+
*/
15+
trait ResponseExtensionTrait
16+
{
17+
use ResponseTrait;
18+
19+
/**
20+
* Sets the provided Content-Type, if none is already present.
21+
*
22+
* @param string $contentType
23+
*/
24+
private function setContentTypeHeaderIfNotExists(string $contentType): void
25+
{
26+
if (!$this->hasHeader('content-type')) {
27+
$this->headerNames['content-type'] = 'Content-Type';
28+
$this->headers[$this->headerNames['content-type']] = [$contentType];
29+
}
30+
}
31+
32+
/**
33+
* Create the message body.
34+
*
35+
* @param string $content
36+
* @return Stream
37+
*/
38+
private function createBody(string $content = ''): Stream
39+
{
40+
$stream = new Stream();
41+
$stream->write($content);
42+
$stream->rewind();
43+
return $stream;
44+
}
45+
}

src/ResponseFactory.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)