Skip to content

Commit b95a6db

Browse files
committed
update Response
1 parent 8dd95a9 commit b95a6db

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

src/Contracts/Response.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace PhpBootstrap\Contracts;
1010

1111
use League\Fractal\Pagination\Cursor;
12+
use League\Fractal\TransformerAbstract;
1213
use Psr\Http\Message\ResponseInterface;
1314

1415
interface Response extends ResponseInterface
@@ -23,7 +24,7 @@ public function withArray(array $data, $code = 200, array $headers = []);
2324

2425
/**
2526
* @param $data
26-
* @param $transformer
27+
* @param TransformerAbstract|callable $transformer
2728
* @param int $code
2829
* @param null $resourceKey
2930
* @param array $meta
@@ -34,7 +35,7 @@ public function withItem($data, $transformer, $code = 200, $resourceKey = null,
3435

3536
/**
3637
* @param $data
37-
* @param $transformer
38+
* @param TransformerAbstract|callable $transformer
3839
* @param int $code
3940
* @param null $resourceKey
4041
* @param Cursor|null $cursor

src/Controller/HelloWorld.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace PhpBootstrap\Controller;
1010

1111
use PhpBootstrap\Contracts\Hello as HelloInterface;
12-
use Psr\Http\Message\ResponseInterface;
12+
use PhpBootstrap\Contracts\Response as ResponseInterface;
1313
use Psr\Http\Message\ServerRequestInterface;
1414

1515
class HelloWorld
@@ -26,12 +26,11 @@ public function __construct(HelloInterface $hello)
2626

2727
public function sayHi(ServerRequestInterface $request, ResponseInterface $response, array $args)
2828
{
29-
/**
30-
* http://route.thephpleague.com/json-strategy/
31-
*/
32-
$response->getBody()->write(json_encode([
33-
'message' => sprintf('%s %s', $this->hello->sayHi(), $args['name'])
34-
]));
35-
return $response->withStatus(200);
29+
return $response->withArray(
30+
[
31+
'message' => sprintf('%s %s', $this->hello->sayHi(), $args['name'])
32+
],
33+
200
34+
);
3635
}
3736
}

src/Routes.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ function (ServerRequestInterface $request, ResponseInterface $response) {
5151
$container->get('helloworldcontroller'),
5252
'sayHi'
5353
]
54-
)->setStrategy(new JsonStrategy())
55-
->middleware([
54+
)->middleware([
5655
new ExampleMiddleware(), 'checkToken'
5756
]);
5857
}

src/Services/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function withArray(array $data, $code = 200, array $headers = [])
162162
{
163163
$new = clone $this;
164164
$new->setStatusCode($code);
165-
$new->getBody()->write(json_encode([$data]));
165+
$new->getBody()->write(json_encode($data));
166166
$new = $new->withHeader('Content-Type', 'application/json');
167167
$new->headers = array_merge($new->headers, $headers);
168168
return $new;

0 commit comments

Comments
 (0)