Skip to content

Commit e490eba

Browse files
committed
Fixed JsonApiTrait. Errors now use ErrorBag
1 parent a9205c4 commit e490eba

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"require-dev": {
2525
"symfony/symfony": "2.*",
2626
"fabpot/php-cs-fixer": "~1.9",
27-
"nilportugues/php_backslasher": "~0.2",
28-
"mmoreram/php-formatter": "dev-master"
27+
"nilportugues/php_backslasher": "~0.2"
2928
},
3029
"config": {
3130
"preferred-install": "dist"

src/Serializer/JsonApiResponseTrait.php

+30-20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use NilPortugues\Api\JsonApi\Http\Response\Response;
1212
use NilPortugues\Api\JsonApi\Http\Response\UnprocessableEntity;
1313
use NilPortugues\Api\JsonApi\Http\Response\UnsupportedAction;
14+
use NilPortugues\Api\JsonApi\Server\Errors\Error;
15+
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag;
1416
use Psr\Http\Message\ResponseInterface;
1517
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
1618

@@ -33,8 +35,9 @@ protected function addHeaders(ResponseInterface $response)
3335
*/
3436
protected function errorResponse($json)
3537
{
36-
return (new HttpFoundationFactory())
37-
->createResponse($this->addHeaders(new BadRequest($json)));
38+
$error = new Error('Bad Request', json_decode($json));
39+
40+
return $this->createResponse(new BadRequest(new ErrorBag([$error])));
3841
}
3942

4043
/**
@@ -44,8 +47,7 @@ protected function errorResponse($json)
4447
*/
4548
protected function resourceCreatedResponse($json)
4649
{
47-
return (new HttpFoundationFactory())
48-
->createResponse($this->addHeaders(new ResourceCreated($json)));
50+
return $this->createResponse(new ResourceCreated($json));
4951
}
5052

5153
/**
@@ -55,8 +57,7 @@ protected function resourceCreatedResponse($json)
5557
*/
5658
protected function resourceDeletedResponse($json)
5759
{
58-
return (new HttpFoundationFactory())
59-
->createResponse($this->addHeaders(new ResourceDeleted($json)));
60+
return $this->createResponse(new ResourceDeleted($json));
6061
}
6162

6263
/**
@@ -66,8 +67,7 @@ protected function resourceDeletedResponse($json)
6667
*/
6768
protected function resourceNotFoundResponse($json)
6869
{
69-
return (new HttpFoundationFactory())
70-
->createResponse($this->addHeaders(new ResourceNotFound($json)));
70+
return $this->createResponse(new ResourceNotFound($json));
7171
}
7272

7373
/**
@@ -77,8 +77,9 @@ protected function resourceNotFoundResponse($json)
7777
*/
7878
protected function resourcePatchErrorResponse($json)
7979
{
80-
return (new HttpFoundationFactory())
81-
->createResponse($this->addHeaders(new UnprocessableEntity($json)));
80+
$error = new Error('Unprocessable Entity', json_decode($json));
81+
82+
return $this->createResponse(new UnprocessableEntity([$error]));
8283
}
8384

8485
/**
@@ -88,8 +89,9 @@ protected function resourcePatchErrorResponse($json)
8889
*/
8990
protected function resourcePostErrorResponse($json)
9091
{
91-
return (new HttpFoundationFactory())
92-
->createResponse($this->addHeaders(new UnprocessableEntity($json)));
92+
$error = new Error('Unprocessable Entity', json_decode($json));
93+
94+
return $this->createResponse(new UnprocessableEntity([$error]));
9395
}
9496

9597
/**
@@ -99,8 +101,7 @@ protected function resourcePostErrorResponse($json)
99101
*/
100102
protected function resourceProcessingResponse($json)
101103
{
102-
return (new HttpFoundationFactory())
103-
->createResponse($this->addHeaders(new ResourceProcessing($json)));
104+
return $this->createResponse(new ResourceProcessing($json));
104105
}
105106

106107
/**
@@ -110,8 +111,7 @@ protected function resourceProcessingResponse($json)
110111
*/
111112
protected function resourceUpdatedResponse($json)
112113
{
113-
return (new HttpFoundationFactory())
114-
->createResponse($this->addHeaders(new ResourceUpdated($json)));
114+
return $this->createResponse(new ResourceUpdated($json));
115115
}
116116

117117
/**
@@ -121,8 +121,7 @@ protected function resourceUpdatedResponse($json)
121121
*/
122122
protected function response($json)
123123
{
124-
return (new HttpFoundationFactory())
125-
->createResponse($this->addHeaders(new Response($json)));
124+
return $this->createResponse(new Response($json));
126125
}
127126

128127
/**
@@ -132,7 +131,18 @@ protected function response($json)
132131
*/
133132
protected function unsupportedActionResponse($json)
134133
{
135-
return (new HttpFoundationFactory())
136-
->createResponse($this->addHeaders(new UnsupportedAction($json)));
134+
$error = new Error('Unsupported Action', json_decode($json));
135+
136+
return $this->createResponse(new UnsupportedAction([$error]));
137+
}
138+
139+
/**
140+
* @param $data
141+
*
142+
* @return \Symfony\Component\HttpFoundation\Response
143+
*/
144+
private function createResponse($data)
145+
{
146+
return (new HttpFoundationFactory())->createResponse($this->addHeaders($data));
137147
}
138148
}

0 commit comments

Comments
 (0)