1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Presentation; |
15
|
|
|
|
16
|
|
|
use FiveLab\Component\Resource\Resource\ResourceInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Presentation factory for easy create presentations. |
20
|
|
|
* |
21
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PresentationFactory |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Create the presentation |
27
|
|
|
* |
28
|
|
|
* @param int $statusCode |
29
|
|
|
* @param ResourceInterface $resource |
30
|
|
|
* |
31
|
|
|
* @return PresentationInterface |
32
|
|
|
*/ |
33
|
38 |
|
public static function create(int $statusCode, ResourceInterface $resource = null): PresentationInterface |
34
|
|
|
{ |
35
|
38 |
|
return new Presentation($statusCode, $resource); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create the success presentation |
40
|
|
|
* |
41
|
|
|
* @param ResourceInterface $resource |
42
|
|
|
* |
43
|
|
|
* @return PresentationInterface |
44
|
|
|
*/ |
45
|
1 |
|
public static function ok(ResourceInterface $resource): PresentationInterface |
46
|
|
|
{ |
47
|
1 |
|
return static::create(200, $resource); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create the created presentation |
52
|
|
|
* |
53
|
|
|
* @param ResourceInterface|null $resource |
54
|
|
|
* |
55
|
|
|
* @return PresentationInterface |
56
|
|
|
*/ |
57
|
2 |
|
public static function created(ResourceInterface $resource = null): PresentationInterface |
58
|
|
|
{ |
59
|
2 |
|
return static::create(201, $resource); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create accepted presentation |
64
|
|
|
* |
65
|
|
|
* @param ResourceInterface $resource |
66
|
|
|
* |
67
|
|
|
* @return PresentationInterface |
68
|
|
|
*/ |
69
|
2 |
|
public static function accepted(ResourceInterface $resource = null): PresentationInterface |
70
|
|
|
{ |
71
|
2 |
|
return static::create(202, $resource); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create non-authoritative information presentation |
76
|
|
|
* |
77
|
|
|
* @param ResourceInterface $resource |
78
|
|
|
* |
79
|
|
|
* @return PresentationInterface |
80
|
|
|
*/ |
81
|
1 |
|
public static function nonAuthoritativeInformation(ResourceInterface $resource): PresentationInterface |
82
|
|
|
{ |
83
|
1 |
|
return static::create(203, $resource); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create the no content presentation |
88
|
|
|
* |
89
|
|
|
* @return PresentationInterface |
90
|
|
|
*/ |
91
|
1 |
|
public static function noContent(): PresentationInterface |
92
|
|
|
{ |
93
|
1 |
|
return static::create(204); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create the reset content presentation |
98
|
|
|
* |
99
|
|
|
* @return PresentationInterface |
100
|
|
|
*/ |
101
|
1 |
|
public static function resetContent(): PresentationInterface |
102
|
|
|
{ |
103
|
1 |
|
return static::create(205); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create the bad request presentation |
108
|
|
|
* |
109
|
|
|
* @param ResourceInterface $resource |
110
|
|
|
* |
111
|
|
|
* @return PresentationInterface |
112
|
|
|
*/ |
113
|
3 |
|
public static function badRequest(ResourceInterface $resource = null): PresentationInterface |
114
|
|
|
{ |
115
|
3 |
|
return static::create(400, $resource); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Create unauthorized presentation |
120
|
|
|
* |
121
|
|
|
* @param ResourceInterface $resource |
122
|
|
|
* |
123
|
|
|
* @return PresentationInterface |
124
|
|
|
*/ |
125
|
3 |
|
public static function unauthorized(ResourceInterface $resource = null): PresentationInterface |
126
|
|
|
{ |
127
|
3 |
|
return static::create(401, $resource); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Create forbidden presentation |
132
|
|
|
* |
133
|
|
|
* @param ResourceInterface $resource |
134
|
|
|
* |
135
|
|
|
* @return PresentationInterface |
136
|
|
|
*/ |
137
|
3 |
|
public static function forbidden(ResourceInterface $resource = null): PresentationInterface |
138
|
|
|
{ |
139
|
3 |
|
return static::create(403, $resource); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Create not found presentation |
144
|
|
|
* |
145
|
|
|
* @param ResourceInterface|null $resource |
146
|
|
|
* |
147
|
|
|
* @return PresentationInterface |
148
|
|
|
*/ |
149
|
3 |
|
public static function notFound(ResourceInterface $resource = null): PresentationInterface |
150
|
|
|
{ |
151
|
3 |
|
return static::create(404, $resource); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create conflict presentation |
156
|
|
|
* |
157
|
|
|
* @param ResourceInterface $resource |
158
|
|
|
* |
159
|
|
|
* @return PresentationInterface |
160
|
|
|
*/ |
161
|
3 |
|
public static function conflict(ResourceInterface $resource = null): PresentationInterface |
162
|
|
|
{ |
163
|
3 |
|
return static::create(409, $resource); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create gone presentation |
168
|
|
|
* |
169
|
|
|
* @param ResourceInterface $resource |
170
|
|
|
* |
171
|
|
|
* @return PresentationInterface |
172
|
|
|
*/ |
173
|
3 |
|
public static function gone(ResourceInterface $resource = null): PresentationInterface |
174
|
|
|
{ |
175
|
3 |
|
return static::create(410, $resource); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create request entity to large presentation |
180
|
|
|
* |
181
|
|
|
* @param ResourceInterface $resource |
182
|
|
|
* |
183
|
|
|
* @return PresentationInterface |
184
|
|
|
*/ |
185
|
3 |
|
public static function requestEntityToLarge(ResourceInterface $resource = null): PresentationInterface |
186
|
|
|
{ |
187
|
3 |
|
return static::create(413, $resource); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Create unsupported media type presentation |
192
|
|
|
* |
193
|
|
|
* @param ResourceInterface $resource |
194
|
|
|
* |
195
|
|
|
* @return PresentationInterface |
196
|
|
|
*/ |
197
|
3 |
|
public static function unsupportedMediaType(ResourceInterface $resource = null): PresentationInterface |
198
|
|
|
{ |
199
|
3 |
|
return static::create(415, $resource); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Create internal server error presentation |
204
|
|
|
* |
205
|
|
|
* @param ResourceInterface $resource |
206
|
|
|
* |
207
|
|
|
* @return PresentationInterface |
208
|
|
|
*/ |
209
|
3 |
|
public static function internalServerError(ResourceInterface $resource = null): PresentationInterface |
210
|
|
|
{ |
211
|
3 |
|
return static::create(500, $resource); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|