Skip to content

Commit

Permalink
fixed disable associative config #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Otero committed Nov 8, 2018
1 parent 5322aef commit 339c01d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Use `phpstan` as a dev dependency to detect bugs
- Fixed disabled associative config, that could return `scalar|array|object` instead only array #8

## [2.1.0] - 2018-08-04

Expand Down
10 changes: 7 additions & 3 deletions src/JsonPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public function options(int $options): self
/**
* {@inheritdoc}
*/
protected function parse(StreamInterface $stream): array
protected function parse(StreamInterface $stream)
{
$json = trim((string) $stream);

if ($json === '') {
return [];
return $this->associative ? [] : null;
}

$data = json_decode($json, $this->associative, $this->depth, $this->options);
Expand All @@ -85,6 +85,10 @@ protected function parse(StreamInterface $stream): array
throw new Exception(sprintf('JSON: %s', json_last_error_msg()), $code);
}

return $data ?: [];
if ($this->associative) {
return $data ?: [];
}

return $data;
}
}
2 changes: 1 addition & 1 deletion src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
/**
* Parse the body.
*/
abstract protected function parse(StreamInterface $stream): array;
abstract protected function parse(StreamInterface $stream);

/**
* Check whether the request payload need to be processed
Expand Down
2 changes: 1 addition & 1 deletion src/UrlEncodePayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class UrlEncodePayload extends Payload implements MiddlewareInterface
/**
* {@inheritdoc}
*/
protected function parse(StreamInterface $stream): array
protected function parse(StreamInterface $stream)
{
$string = trim((string) $stream);
parse_str($string, $data);
Expand Down
36 changes: 36 additions & 0 deletions tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,40 @@ public function testJsonOptions()
$request
);
}

public function disabledAssociativeProvider()
{
return [
['{}', (object) []],
['{"foo": "bar"}', (object) ['foo' => 'bar']],
['["foo", "bar"]', ['foo', 'bar']],
['', null],
];
}

/**
* @dataProvider disabledAssociativeProvider
* @param mixed $expected
*/
public function testAssociativeDisabled(string $body, $expected)
{
$request = Factory::createServerRequest('POST', '/')
->withHeader('Content-Type', 'application/json');

$request->getBody()->write($body);

$response = Dispatcher::run(
[
(new JsonPayload())->associative(false),
function ($request) use ($expected) {
$this->assertEquals($expected, $request->getParsedBody());

echo 'Ok';
},
],
$request
);

$this->assertEquals('Ok', (string) $response->getBody());
}
}

1 comment on commit 339c01d

@shadowhand
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.