1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 8/18/15 |
5
|
|
|
* Time: 11:19 PM |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Laravel5\JsonSerializer; |
12
|
|
|
|
13
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
14
|
|
|
|
15
|
|
|
trait JsonResponseTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
19
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
20
|
|
|
*/ |
21
|
|
|
protected function addHeaders(\Psr\Http\Message\ResponseInterface $response) |
22
|
|
|
{ |
23
|
|
|
return $response; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $json |
28
|
|
|
* |
29
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
30
|
|
|
*/ |
31
|
|
|
private function errorResponse($json) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
return (new HttpFoundationFactory()) |
34
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ErrorResponse($json))); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $json |
39
|
|
|
* |
40
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
41
|
|
|
*/ |
42
|
|
|
private function resourceCreatedResponse($json) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
return (new HttpFoundationFactory()) |
45
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceCreatedResponse($json))); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $json |
50
|
|
|
* |
51
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
52
|
|
|
*/ |
53
|
|
|
private function resourceDeletedResponse($json) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
return (new HttpFoundationFactory()) |
56
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceDeletedResponse($json))); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $json |
61
|
|
|
* |
62
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
63
|
|
|
*/ |
64
|
|
|
private function resourceNotFoundResponse($json) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
return (new HttpFoundationFactory()) |
67
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceNotFoundResponse($json))); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $json |
72
|
|
|
* |
73
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
74
|
|
|
*/ |
75
|
|
|
private function resourcePatchErrorResponse($json) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return (new HttpFoundationFactory()) |
78
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourcePatchErrorResponse($json))); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $json |
83
|
|
|
* |
84
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
85
|
|
|
*/ |
86
|
|
|
private function resourcePostErrorResponse($json) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return (new HttpFoundationFactory()) |
89
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourcePostErrorResponse($json))); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $json |
94
|
|
|
* |
95
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
96
|
|
|
*/ |
97
|
|
|
private function resourceProcessingResponse($json) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return (new HttpFoundationFactory()) |
100
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceProcessingResponse($json))); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $json |
105
|
|
|
* |
106
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
107
|
|
|
*/ |
108
|
|
|
private function resourceUpdatedResponse($json) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return (new HttpFoundationFactory()) |
111
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\ResourceUpdatedResponse($json))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string $json |
116
|
|
|
* |
117
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
118
|
|
|
*/ |
119
|
|
|
private function response($json) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
return (new HttpFoundationFactory()) |
122
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\Response($json))); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $json |
127
|
|
|
* |
128
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
129
|
|
|
*/ |
130
|
|
|
private function unsupportedActionResponse($json) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
return (new HttpFoundationFactory()) |
133
|
|
|
->createResponse($this->addHeaders(new \NilPortugues\Api\Json\Http\Message\UnsupportedActionResponse($json))); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|