Skip to content

Commit

Permalink
new option 'attribute'
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 8, 2022
1 parent 548ee03 commit 82a7906
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [2.1.0] - 2022-01-08
### Added
- New option `attribute()` for `ContentType` middleware, to save the format name in a `ServerRequest` attribute [#10].

## [2.0.2] - 2020-12-02
### Added
- Support for PHP 8
Expand Down Expand Up @@ -88,12 +92,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- *ContentEncoding* middleware removes the `Accept-Encoding` header if it does not match with any available option.

## 0.1.0 - 2016-10-01
## [0.1.0] - 2016-10-01
First version

[#6]: https://github.com/middlewares/negotiation/issues/6
[#8]: https://github.com/middlewares/negotiation/issues/8
[#10]: https://github.com/middlewares/negotiation/issues/10

[2.1.0]: https://github.com/middlewares/negotiation/compare/v2.0.2...v2.1.0
[2.0.2]: https://github.com/middlewares/negotiation/compare/v2.0.1...v2.0.2
[2.0.1]: https://github.com/middlewares/negotiation/compare/v2.0.0...v2.0.1
[2.0.0]: https://github.com/middlewares/negotiation/compare/v1.1.0...v2.0.0
Expand All @@ -104,3 +110,4 @@ First version
[0.3.1]: https://github.com/middlewares/negotiation/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/middlewares/negotiation/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/middlewares/negotiation/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/middlewares/negotiation/releases/tag/v0.1.0
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ Adds the `X-Content-Type-Options: nosniff` header, to mitigating [MIME confusió
$negotiator = (new Middlewares\ContentType())->noSniff(false);
```

### attribute

To store the format name (`json`, `html`, `css` etc) in an attribute of the `ServerRequest`.

## ContentLanguage

To detect the preferred language using the `Accept-Language` header or the path prefix and edit the header with this value. A `Content-Language` header is also added to the response if it's missing.
Expand Down
19 changes: 19 additions & 0 deletions src/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ContentType implements MiddlewareInterface
*/
private $formats;

/**
* @var string Attribute name to store the format
*/
private $attribute;

/**
* @var string
*/
Expand Down Expand Up @@ -100,6 +105,16 @@ public function errorResponse(ResponseFactoryInterface $responseFactory = null):
return $this;
}

/**
* Save the format name in a server request attribute.
*/
public function attribute(string $attribute): self
{
$this->attribute = $attribute;

return $this;
}

/**
* Set the available charsets. The first value will be used as default
*/
Expand Down Expand Up @@ -142,6 +157,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
->withHeader('Accept', $contentType)
->withHeader('Accept-Charset', $charset);

if ($this->attribute) {
$request = $request->withAttribute($this->attribute, $format);
}

$response = $handler->handle($request);

if (!$response->hasHeader('Content-Type')) {
Expand Down
16 changes: 16 additions & 0 deletions tests/ContentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,20 @@ function ($request) {
$this->assertEquals('', $response->getHeaderLine('Content-Type'));
$this->assertEquals(406, $response->getStatusCode());
}

public function testAttribute()
{
$request = Factory::createServerRequest('GET', '/')->withHeader('Accept', 'text/json');

$response = Dispatcher::run([
(new ContentType(['json', 'html']))->attribute("format_name"),
function ($request) {
echo $request->getAttribute('format_name');
},
], $request);

$this->assertEquals('', $response->getHeaderLine('application/json'));
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('json', (string) $response->getBody());
}
}

0 comments on commit 82a7906

Please sign in to comment.