1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Http\Responses; |
4
|
|
|
|
5
|
|
|
use Flugg\Responder\Contracts\ResponseFactory; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
8
|
|
|
use Illuminate\Http\JsonResponse; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* An abstract builder class for building responses. |
13
|
|
|
* |
14
|
|
|
* @package flugger/laravel-responder |
15
|
|
|
* @author Alexander Tømmerås <[email protected]> |
16
|
|
|
* @license The MIT License |
17
|
|
|
*/ |
18
|
|
|
abstract class ResponseBuilder implements Arrayable, Jsonable |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* A factory for making responses. |
22
|
|
|
* |
23
|
|
|
* @var \Flugg\Responder\Http\Responses\Factories\ResponseFactory |
24
|
|
|
*/ |
25
|
|
|
protected $responseFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* A HTTP status code for the response. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $status; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct the builder class. |
36
|
|
|
* |
37
|
|
|
* @param \Flugg\Responder\Contracts\ResponseFactory $responseFactory |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ResponseFactory $responseFactory) |
40
|
|
|
{ |
41
|
|
|
$this->responseFactory = $responseFactory; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Respond with a successful response. |
46
|
|
|
* |
47
|
|
|
* @param int|null $status |
48
|
|
|
* @param array $headers |
49
|
|
|
* @return \Illuminate\Http\JsonResponse |
50
|
|
|
*/ |
51
|
|
|
public function respond(int $status = null, array $headers = []): JsonResponse |
52
|
|
|
{ |
53
|
|
|
if (! is_null($status)) { |
54
|
|
|
$this->setStatusCode($status); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->responseFactory->make($this->toArray(), $this->status, $headers); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Convert the response to an array. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function toArray(): array |
66
|
|
|
{ |
67
|
|
|
return $this->getOutput(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Convert the response to an Illuminate collection. |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Support\Collection |
74
|
|
|
*/ |
75
|
|
|
public function toCollection(): Collection |
76
|
|
|
{ |
77
|
|
|
return new Collection($this->toArray()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convert the response to JSON. |
82
|
|
|
* |
83
|
|
|
* @param int $options |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function toJson($options = 0): string |
87
|
|
|
{ |
88
|
|
|
return json_encode($this->toArray(), $options); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the HTTP status code for the response. |
93
|
|
|
* |
94
|
|
|
* @param int $status |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
protected function setStatusCode(int $status): void |
98
|
|
|
{ |
99
|
|
|
$this->validateStatusCode($this->status = $status); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the serialized response output. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
abstract protected function getOutput(): array; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Convert the response to an array. |
111
|
|
|
* |
112
|
|
|
* @param int $status |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
abstract protected function validateStatusCode(int $status): void; |
116
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..