Skip to content

Commit bedfc0f

Browse files
authored
Cleanup, add docBlocks, improve tests (#54)
1 parent ae38056 commit bedfc0f

25 files changed

+546
-351
lines changed

README.md

+46-24
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
<a href="https://github.com/yiisoft" target="_blank">
33
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px">
44
</a>
5-
<h1 align="center">Data response</h1>
5+
<h1 align="center">Yii Data response</h1>
66
<br>
77
</p>
88

9-
The package allows responding with data that is automatically converted into PSR-7 response.
10-
119
[![Latest Stable Version](https://poser.pugx.org/yiisoft/data-response/v/stable.png)](https://packagist.org/packages/yiisoft/data-response)
1210
[![Total Downloads](https://poser.pugx.org/yiisoft/data-response/downloads.png)](https://packagist.org/packages/yiisoft/data-response)
1311
[![Build status](https://github.com/yiisoft/data-response/workflows/build/badge.svg)](https://github.com/yiisoft/data-response/actions?query=workflow%3Abuild)
@@ -17,39 +15,60 @@ The package allows responding with data that is automatically converted into PSR
1715
[![static analysis](https://github.com/yiisoft/data-response/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/data-response/actions?query=workflow%3A%22static+analysis%22)
1816
[![type-coverage](https://shepherd.dev/github/yiisoft/data-response/coverage.svg)](https://shepherd.dev/github/yiisoft/data-response)
1917

18+
The package allows responding with data that is automatically converted into PSR-7 response.
19+
20+
## Requirements
21+
22+
- PHP 7.4 or higher.
23+
- `DOM` PHP extension.
24+
25+
## Installation
26+
27+
The package could be installed via composer:
28+
29+
```shell
30+
composer require yiisoft/data-response --prefer-dist
31+
```
32+
2033
## General usage
2134

22-
The package provides `DataResponseFactory` class that, given a PSR-17 response factory, is able to create data response. In this example we use `nyholm/psr7` package but any PSR-17 response factory would do.
35+
The package provides `DataResponseFactory` class that, given a PSR-17 response factory, is able to create data response.
2336

2437
Data response contains raw data to be processed later.
2538

2639
```php
27-
use Nyholm\Psr7\Factory\Psr17Factory;
2840
use Yiisoft\DataResponse\DataResponseFactory;
2941

30-
$factory = new DataResponseFactory(new Psr17Factory());
42+
/**
43+
* @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
44+
*/
45+
46+
$factory = new DataResponseFactory($responseFactory);
3147
$dataResponse = $factory->createResponse('test');
3248
$dataResponse->getBody()->rewind();
3349

34-
echo $dataResponse->getBody()->getContents(); //test
50+
echo $dataResponse->getBody()->getContents(); // "test"
3551
```
3652

3753
### Formatters
3854

3955
Formatters purpose is to format a data response. In the following example we format data as JSON.
4056

4157
```php
42-
use Nyholm\Psr7\Factory\Psr17Factory;
4358
use Yiisoft\DataResponse\DataResponseFactory;
4459
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
4560

46-
$factory = new DataResponseFactory(new Psr17Factory());
61+
/**
62+
* @var Psr\Http\Message\ResponseFactoryInterface $responseFactory
63+
*/
64+
65+
$factory = new DataResponseFactory($responseFactory);
4766
$dataResponse = $factory->createResponse('test');
4867
$dataResponse = $dataResponse->withResponseFormatter(new JsonDataResponseFormatter());
4968
$dataResponse->getBody()->rewind();
5069

51-
echo $dataResponse->getHeader('Content-Type'); //application/json
52-
echo $dataResponse->getBody()->getContents(); //"test"
70+
echo $dataResponse->getHeader('Content-Type'); // ["application/json; charset=UTF-8"]
71+
echo $dataResponse->getBody()->getContents(); // "test"
5372
```
5473

5574
The following formatters are available:
@@ -70,7 +89,7 @@ $middleware = (new FormatDataResponse(new JsonDataResponseFormatter()));
7089
//$middleware->process($request, $handler);
7190
```
7291

73-
Also the package provides PSR-15 middleware for content negotiation
92+
Also the package provides PSR-15 middleware for content negotiation:
7493

7594
```php
7695
use Yiisoft\DataResponse\Formatter\HtmlDataResponseFormatter;
@@ -85,7 +104,7 @@ $middleware = new ContentNegotiator([
85104
]);
86105
```
87106

88-
You can override middlewares with method `withContentFormatters`
107+
You can override middlewares with method `withContentFormatters()`:
89108

90109
```php
91110
$middleware->withContentFormatters([
@@ -94,6 +113,8 @@ $middleware->withContentFormatters([
94113
]);
95114
```
96115

116+
## Testing
117+
97118
### Unit testing
98119

99120
The package is tested with [PHPUnit](https://phpunit.de/). To run tests:
@@ -104,10 +125,11 @@ The package is tested with [PHPUnit](https://phpunit.de/). To run tests:
104125

105126
### Mutation testing
106127

107-
The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:
128+
The package tests are checked with [Infection](https://infection.github.io/) mutation framework with
129+
[Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:
108130

109131
```shell
110-
./vendor/bin/infection
132+
./vendor/bin/roave-infection-static-analysis-plugin
111133
```
112134

113135
### Static analysis
@@ -118,21 +140,21 @@ The code is statically analyzed with [Psalm](https://psalm.dev/). To run static
118140
./vendor/bin/psalm
119141
```
120142

121-
### Support the project
143+
## License
144+
145+
The Data response is free software. It is released under the terms of the BSD License.
146+
Please see [`LICENSE`](./LICENSE.md) for more information.
147+
148+
Maintained by [Yii Software](https://www.yiiframework.com/).
149+
150+
## Support the project
122151

123152
[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective&logoColor=7eadf1&labelColor=555555)](https://opencollective.com/yiisoft)
124153

125-
### Follow updates
154+
## Follow updates
126155

127156
[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)
128157
[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter&logoColor=1DA1F2&labelColor=555555?style=flat)](https://twitter.com/yiiframework)
129158
[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat&logo=telegram)](https://t.me/yii3en)
130159
[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat&logo=facebook&logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)
131160
[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat&logo=slack)](https://yiiframework.com/go/slack)
132-
133-
## License
134-
135-
The Data response is free software. It is released under the terms of the BSD License.
136-
Please see [`LICENSE`](./LICENSE.md) for more information.
137-
138-
Maintained by [Yii Software](https://www.yiiframework.com/).

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"yiisoft/strings": "^2.0"
2929
},
3030
"require-dev": {
31-
"nyholm/psr7": "^1.3",
31+
"httpsoft/http-message": "^1.0",
3232
"phpunit/phpunit": "^9.5",
3333
"roave/infection-static-analysis-plugin": "^1.7",
3434
"spatie/phpunit-watcher": "^1.23",

phpunit.xml.dist

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit bootstrap="vendor/autoload.php"
4-
colors="true"
5-
verbose="true"
6-
failOnRisky="true"
7-
failOnWarning="true"
8-
convertErrorsToExceptions="true"
9-
convertNoticesToExceptions="true"
10-
convertWarningsToExceptions="true"
11-
stopOnFailure="false"
12-
executionOrder="random"
13-
resolveDependencies="true">
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
colors="true"
5+
verbose="true"
6+
failOnRisky="true"
7+
failOnWarning="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
stopOnFailure="false"
12+
executionOrder="random"
13+
resolveDependencies="true"
14+
>
1415
<php>
1516
<ini name="error_reporting" value="-1"/>
1617
</php>
17-
1818
<testsuites>
1919
<testsuite name="Yii Data Response tests">
2020
<directory>./tests</directory>
2121
</testsuite>
2222
</testsuites>
23-
2423
<coverage>
2524
<include>
2625
<directory>./src</directory>

psalm.xml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="3"
4-
resolveFromConfigFile="true"
3+
errorLevel="1"
54
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
65
xmlns="https://getpsalm.org/schema/config"
76
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -12,4 +11,4 @@
1211
<directory name="vendor" />
1312
</ignoreFiles>
1413
</projectFiles>
15-
</psalm>
14+
</psalm>

0 commit comments

Comments
 (0)