|
1
|
|
|
<?php declare (strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Flute\Http; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2019 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface; |
|
22
|
|
|
use Zend\Diactoros\Response; |
|
23
|
|
|
use Zend\Diactoros\Response\InjectContentTypeTrait; |
|
24
|
|
|
use Zend\Diactoros\Stream; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @package Limoncello\Flute |
|
28
|
|
|
*/ |
|
29
|
|
|
class JsonApiResponse extends Response |
|
30
|
|
|
{ |
|
31
|
|
|
/** HTTP code */ |
|
32
|
|
|
const HTTP_OK = 200; |
|
33
|
|
|
|
|
34
|
|
|
/** HTTP code */ |
|
35
|
|
|
const HTTP_CREATED = 201; |
|
36
|
|
|
|
|
37
|
|
|
/** HTTP code */ |
|
38
|
|
|
const HTTP_NO_CONTENT = 204; |
|
39
|
|
|
|
|
40
|
|
|
/** HTTP code */ |
|
41
|
|
|
const HTTP_BAD_REQUEST = 400; |
|
42
|
|
|
|
|
43
|
|
|
/** HTTP code */ |
|
44
|
|
|
const HTTP_NOT_FOUND = 404; |
|
45
|
|
|
|
|
46
|
|
|
/** HTTP code */ |
|
47
|
|
|
const HTTP_CONFLICT = 409; |
|
48
|
|
|
|
|
49
|
|
|
/** HTTP code */ |
|
50
|
|
|
const HTTP_UNPROCESSABLE_ENTITY = 422; |
|
51
|
|
|
|
|
52
|
|
|
use InjectContentTypeTrait; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string|null $content |
|
56
|
|
|
* @param int $status |
|
57
|
|
|
* @param array $headers |
|
58
|
|
|
*/ |
|
59
|
26 |
|
public function __construct(string $content = null, int $status = 200, array $headers = []) |
|
60
|
|
|
{ |
|
61
|
26 |
|
$body = new Stream('php://temp', 'wb+'); |
|
62
|
|
|
|
|
63
|
26 |
|
if ($content !== null) { |
|
64
|
22 |
|
$body->write($content); |
|
65
|
22 |
|
$body->rewind(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// inject content-type even when there is no content otherwise |
|
69
|
|
|
// it would be set to 'text/html' by PHP/Web server/Browser |
|
70
|
26 |
|
$headers = $this->injectContentType(MediaTypeInterface::JSON_API_MEDIA_TYPE, $headers); |
|
71
|
|
|
|
|
72
|
26 |
|
parent::__construct($body, $status, $headers); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|