Skip to content

Commit 7586b67

Browse files
authored
Merge branch 'master' into late-static-binding
2 parents eff7220 + ff98230 commit 7586b67

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
sudo: false
2-
31
language: php
42

53
dist: trusty

src/ServerRequest.php

+26-23
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,17 @@ public function getParsedBody()
268268
return null;
269269
}
270270

271-
// Look for a media type with a structured syntax suffix (RFC 6839)
272-
$parts = explode('+', $mediaType);
273-
if (count($parts) >= 2) {
274-
$mediaType = 'application/' . $parts[count($parts)-1];
271+
// Check if this specific media type has a parser registered first
272+
if (!isset($this->bodyParsers[$mediaType])) {
273+
// If not, look for a media type with a structured syntax suffix (RFC 6839)
274+
$parts = explode('+', $mediaType);
275+
if (count($parts) >= 2) {
276+
$mediaType = 'application/' . $parts[count($parts) - 1];
277+
}
275278
}
276279

277-
if (isset($this->bodyParsers[$mediaType]) === true) {
278-
$body = (string) $this->getBody();
280+
if (isset($this->bodyParsers[$mediaType])) {
281+
$body = (string)$this->getBody();
279282
$parsed = $this->bodyParsers[$mediaType]($body);
280283

281284
if (!is_null($parsed) && !is_object($parsed) && !is_array($parsed)) {
@@ -762,7 +765,7 @@ public function withUri(UriInterface $uri, $preserveHost = false)
762765
*
763766
* @return string|null
764767
*/
765-
public function getContentCharset()
768+
public function getContentCharset(): ?string
766769
{
767770
$mediaTypeParams = $this->getMediaTypeParams();
768771

@@ -780,7 +783,7 @@ public function getContentCharset()
780783
*
781784
* @return string|null The serverRequest content type, if known
782785
*/
783-
public function getContentType()
786+
public function getContentType(): ?string
784787
{
785788
$result = $this->serverRequest->getHeader('Content-Type');
786789
return $result ? $result[0] : null;
@@ -793,7 +796,7 @@ public function getContentType()
793796
*
794797
* @return int|null
795798
*/
796-
public function getContentLength()
799+
public function getContentLength(): ?int
797800
{
798801
$result = $this->serverRequest->getHeader('Content-Length');
799802
return $result ? (int) $result[0] : null;
@@ -828,7 +831,7 @@ public function getCookieParam($key, $default = null)
828831
*
829832
* @return string|null The serverRequest media type, minus content-type params
830833
*/
831-
public function getMediaType()
834+
public function getMediaType(): ?string
832835
{
833836
$contentType = $this->getContentType();
834837

@@ -848,9 +851,9 @@ public function getMediaType()
848851
*
849852
* Note: This method is not part of the PSR-7 standard.
850853
*
851-
* @return array
854+
* @return mixed[]
852855
*/
853-
public function getMediaTypeParams()
856+
public function getMediaTypeParams(): array
854857
{
855858
$contentType = $this->getContentType();
856859
$contentTypeParams = [];
@@ -901,9 +904,9 @@ public function getParam($key, $default = null)
901904
*
902905
* Note: This method is not part of the PSR-7 standard.
903906
*
904-
* @return array
907+
* @return mixed[]
905908
*/
906-
public function getParams()
909+
public function getParams(): array
907910
{
908911
$params = $this->getQueryParams();
909912
$postParams = $this->getParsedBody();
@@ -1003,7 +1006,7 @@ public function registerMediaTypeParser($mediaType, callable $callable): ServerR
10031006
*
10041007
* @return bool
10051008
*/
1006-
public function isDelete()
1009+
public function isDelete(): bool
10071010
{
10081011
return $this->isMethod('DELETE');
10091012
}
@@ -1015,7 +1018,7 @@ public function isDelete()
10151018
*
10161019
* @return bool
10171020
*/
1018-
public function isGet()
1021+
public function isGet(): bool
10191022
{
10201023
return $this->isMethod('GET');
10211024
}
@@ -1027,7 +1030,7 @@ public function isGet()
10271030
*
10281031
* @return bool
10291032
*/
1030-
public function isHead()
1033+
public function isHead(): bool
10311034
{
10321035
return $this->isMethod('HEAD');
10331036
}
@@ -1040,7 +1043,7 @@ public function isHead()
10401043
* @param string $method HTTP method
10411044
* @return bool
10421045
*/
1043-
public function isMethod($method)
1046+
public function isMethod($method): bool
10441047
{
10451048
return $this->serverRequest->getMethod() === $method;
10461049
}
@@ -1052,7 +1055,7 @@ public function isMethod($method)
10521055
*
10531056
* @return bool
10541057
*/
1055-
public function isOptions()
1058+
public function isOptions(): bool
10561059
{
10571060
return $this->isMethod('OPTIONS');
10581061
}
@@ -1064,7 +1067,7 @@ public function isOptions()
10641067
*
10651068
* @return bool
10661069
*/
1067-
public function isPatch()
1070+
public function isPatch(): bool
10681071
{
10691072
return $this->isMethod('PATCH');
10701073
}
@@ -1076,7 +1079,7 @@ public function isPatch()
10761079
*
10771080
* @return bool
10781081
*/
1079-
public function isPost()
1082+
public function isPost(): bool
10801083
{
10811084
return $this->isMethod('POST');
10821085
}
@@ -1088,7 +1091,7 @@ public function isPost()
10881091
*
10891092
* @return bool
10901093
*/
1091-
public function isPut()
1094+
public function isPut(): bool
10921095
{
10931096
return $this->isMethod('PUT');
10941097
}
@@ -1100,7 +1103,7 @@ public function isPut()
11001103
*
11011104
* @return bool
11021105
*/
1103-
public function isXhr()
1106+
public function isXhr(): bool
11041107
{
11051108
return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
11061109
}

tests/ServerRequestTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,29 @@ public function testGetParsedBodyWithJsonStructuredSuffix()
780780
}
781781
}
782782

783+
public function testGetParsedBodyWithJsonStructuredSuffixAndRegisteredParser()
784+
{
785+
foreach ($this->factoryProviders as $factoryProvider) {
786+
/** @var Psr17FactoryProvider $provider */
787+
$provider = new $factoryProvider;
788+
$decoratedServerRequestFactory = new DecoratedServerRequestFactory($provider->getServerRequestFactory());
789+
790+
$streamFactory = $provider->getStreamFactory();
791+
$stream = $streamFactory->createStream('{"foo":"bar"}');
792+
793+
$request = $decoratedServerRequestFactory->createServerRequest('POST', 'https://google.com');
794+
$request = $request
795+
->withHeader('Content-Type', 'application/vnd.api+json;charset=utf8')
796+
->withBody($stream);
797+
798+
$request->registerMediaTypeParser('application/vnd.api+json', function ($input) {
799+
return ['data' => $input];
800+
});
801+
802+
$this->assertEquals(['data' => '{"foo":"bar"}'], $request->getParsedBody());
803+
}
804+
}
805+
783806
public function testGetParsedBodyXml()
784807
{
785808
foreach ($this->factoryProviders as $factoryProvider) {

0 commit comments

Comments
 (0)