|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Guillermoandrae\Lambda\Contracts; |
|
4
|
|
|
|
|
5
|
|
|
abstract class AbstractApiGatewayResponse implements ApiGatewayResponseInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The event data from the API Gateway request. |
|
9
|
|
|
* |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $event; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Successful HTTP status code; returned by default. |
|
16
|
|
|
* |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $statusCode = 200; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* An array of headers to be sent with the response. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $headers = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* An array of headers to be sent with every response. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $requiredHeaders = [ |
|
34
|
|
|
'Access-Control-Allow-Origin' => '*', |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* An array of data that represents the body of the response. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $body = [ |
|
43
|
|
|
'meta' => [], |
|
44
|
|
|
'data' => [] |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
1 |
|
final public function setEvent(array $event): ApiGatewayResponseInterface |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->event = $event; |
|
50
|
1 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
final public function getEvent(): array |
|
54
|
|
|
{ |
|
55
|
1 |
|
return $this->event; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
final public function setStatusCode(int $statusCode): ApiGatewayResponseInterface |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->statusCode = $statusCode; |
|
61
|
1 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
final public function getStatusCode(): int |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->statusCode; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
final public function addHeader(string $name, $value): ApiGatewayResponseInterface |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->headers[$name] = $value; |
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
final public function getHeaders(): array |
|
76
|
|
|
{ |
|
77
|
2 |
|
return array_merge($this->headers, $this->requiredHeaders); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
final public function addBodyMeta(string $name, $value): ApiGatewayResponseInterface |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->body['meta'][$name] = $value; |
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
1 |
|
final public function setBodyData(array $value): ApiGatewayResponseInterface |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->body['data'] = $value; |
|
89
|
1 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
final public function getBody(): array |
|
93
|
|
|
{ |
|
94
|
1 |
|
return $this->body; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
final public function send(): array |
|
98
|
|
|
{ |
|
99
|
1 |
|
$this->handle(); |
|
100
|
|
|
return [ |
|
101
|
1 |
|
'statusCode' => $this->getStatusCode(), |
|
102
|
1 |
|
'headers' => $this->getHeaders(), |
|
103
|
1 |
|
'body' => json_encode($this->getBody()) |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
public function handle(): void |
|
108
|
|
|
{ |
|
109
|
|
|
$songs = [ |
|
110
|
1 |
|
['artist' => 'Afta-1', 'songTitle' => 'Quest', 'albumTitle' => 'Aftathoughts Vol. 1'], |
|
111
|
|
|
['artist' => 'Eric Lau', 'songTitle' => 'Cloud Burst', 'albumTitle' => 'Quadrivium'], |
|
112
|
|
|
['artist' => 'Dr. Who Dat?', 'songTitle' => 'Braziliant Thought', 'albumTitle' => 'Beat Journey'], |
|
113
|
|
|
]; |
|
114
|
1 |
|
$this->addBodyMeta('count', count($songs)); |
|
115
|
1 |
|
$this->setBodyData($songs); |
|
116
|
1 |
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|