Skip to content

Commit f318df5

Browse files
authored
Add documentation (#29)
1 parent 01a3967 commit f318df5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,57 @@ The package allows responding with data that is automatically converted into PSR
1919

2020
## General usage
2121

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` pacakge but any PSR-17 response factory would do.
23+
24+
Data response contains raw data to be processed later.
25+
26+
```php
27+
use Nyholm\Psr7\Factory\Psr17Factory;
28+
use Yiisoft\DataResponse\DataResponseFactory;
29+
30+
$factory = new DataResponseFactory(new Psr17Factory());
31+
$dataResponse = $factory->createResponse('test');
32+
$dataResponse->getBody()->rewind();
33+
34+
echo $dataResponse->getBody()->getContents(); //test
35+
```
36+
37+
### Formatters
38+
39+
Formatter purpose if to format data response. In the following example we format data as JSON.
40+
41+
```php
42+
use Nyholm\Psr7\Factory\Psr17Factory;
43+
use Yiisoft\DataResponse\DataResponseFactory;
44+
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
45+
46+
$factory = new DataResponseFactory(new Psr17Factory());
47+
$dataResponse = $factory->createResponse('test');
48+
$dataResponse = $dataResponse->withResponseFormatter(new JsonDataResponseFormatter());
49+
$dataResponse->getBody()->rewind();
50+
51+
echo $dataResponse->getHeader('Content-Type'); //application/json
52+
echo $dataResponse->getBody()->getContents(); //"test"
53+
```
54+
55+
The following formatters are available:
56+
* HtmlDataResponseFormatter
57+
* JsonDataResponseFormatter
58+
* XmlDataResponseFormatter
59+
60+
### Middleware
61+
62+
The package provides a PSR-15 middleware that is able to format a data response.
63+
64+
```php
65+
66+
use Yiisoft\DataResponse\Middleware\FormatDataResponse;
67+
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
68+
69+
$middleware = (new FormatDataResponse(new JsonDataResponseFormatter()));
70+
//$middleware->process($request, $handler);
71+
```
72+
2273
### Unit testing
2374

2475
The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

0 commit comments

Comments
 (0)