1 | <?php |
||
18 | abstract class ResponseBuilder implements Arrayable, Jsonable |
||
19 | { |
||
20 | /** |
||
21 | * A factory for making responses. |
||
22 | * |
||
23 | * @var \Flugg\Responder\Contracts\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 | 80 | public function __construct(ResponseFactory $responseFactory) |
|
40 | { |
||
41 | 80 | $this->responseFactory = $responseFactory; |
|
42 | 80 | } |
|
43 | |||
44 | /** |
||
45 | * Decorate the response with the given decorator. |
||
46 | * |
||
47 | * @param string[]|string $decorator |
||
48 | * @return $this |
||
49 | */ |
||
50 | public function decorator($decorator) |
||
51 | { |
||
52 | $decorators = is_array($decorator) ? $decorator : func_get_args(); |
||
53 | |||
54 | foreach ($decorators as $decorator) { |
||
55 | $this->responseFactory = new $decorator($this->responseFactory); |
||
56 | }; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Respond with an HTTP response. |
||
63 | * |
||
64 | * @param int|null $status |
||
65 | * @param array $headers |
||
66 | * @return \Illuminate\Http\JsonResponse |
||
67 | */ |
||
68 | 71 | public function respond(int $status = null, array $headers = []): JsonResponse |
|
69 | { |
||
70 | 71 | if (! is_null($status)) { |
|
71 | 5 | $this->setStatusCode($status); |
|
72 | } |
||
73 | |||
74 | 69 | return $this->responseFactory->make($this->getOutput(), $this->status, $headers); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Convert the response to an array. |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | 11 | public function toArray(): array |
|
83 | { |
||
84 | 11 | return $this->respond()->getData(true); |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * Convert the response to an Illuminate collection. |
||
89 | * |
||
90 | * @return \Illuminate\Support\Collection |
||
91 | */ |
||
92 | 3 | public function toCollection(): Collection |
|
93 | { |
||
94 | 3 | return new Collection($this->toArray()); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Convert the response to JSON. |
||
99 | * |
||
100 | * @param int $options |
||
101 | * @return string |
||
102 | */ |
||
103 | 5 | public function toJson($options = 0): string |
|
104 | { |
||
105 | 5 | return json_encode($this->toArray(), $options); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set the HTTP status code for the response. |
||
110 | * |
||
111 | * @param int $status |
||
112 | * @return void |
||
113 | */ |
||
114 | 5 | protected function setStatusCode(int $status) |
|
115 | { |
||
116 | 5 | $this->validateStatusCode($this->status = $status); |
|
117 | 3 | } |
|
118 | |||
119 | /** |
||
120 | * Get the serialized response output. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | abstract protected function getOutput(): array; |
||
125 | |||
126 | /** |
||
127 | * Convert the response to an array. |
||
128 | * |
||
129 | * @param int $status |
||
130 | * @return void |
||
131 | */ |
||
132 | abstract protected function validateStatusCode(int $status); |
||
133 | } |
||
134 |