1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ElePHPant; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Taco |
11
|
|
|
* @author Sérgio Danilo Jr <[email protected]> |
12
|
|
|
* @see https://taco-food-api.herokuapp.com/ |
13
|
|
|
* @package ElePHPant |
14
|
|
|
*/ |
15
|
|
|
class Taco |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $base; |
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
private $endpoint; |
29
|
|
|
/** |
30
|
|
|
* @var float |
31
|
|
|
*/ |
32
|
|
|
private $timeout = 2.0; |
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
private $method; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var |
40
|
|
|
*/ |
41
|
|
|
private $response; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var |
45
|
|
|
*/ |
46
|
|
|
private $code; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var |
50
|
|
|
*/ |
51
|
|
|
private $message; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Request GET Method |
55
|
|
|
*/ |
56
|
|
|
private const REQUEST_GET = "GET"; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
|
private const RESPONSE_ACCEPT = 200; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Taco constructor. |
66
|
|
|
*/ |
67
|
|
|
public function __construct() |
68
|
|
|
{ |
69
|
|
|
$this->base = "https://taco-food-api.herokuapp.com/"; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $timeout |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setTimeout($seconds = 2): Taco |
77
|
|
|
{ |
78
|
|
|
$this->timeout = (float)$seconds; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function getResponse() |
86
|
|
|
{ |
87
|
|
|
return $this->response; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getMessage() |
94
|
|
|
{ |
95
|
|
|
return $this->message; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function categories(bool $decoded = true) |
102
|
|
|
{ |
103
|
|
|
$call = $this->call("category") |
104
|
|
|
->connect() |
105
|
|
|
->getBody(); |
106
|
|
|
|
107
|
|
|
if ($decoded) { |
108
|
|
|
return $this->decoder($call); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $call; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $categoryId |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function category(int $categoryId, bool $decoded = true) |
119
|
|
|
{ |
120
|
|
|
$call = $this->call("category/{$categoryId}") |
121
|
|
|
->connect() |
122
|
|
|
->getBody(); |
123
|
|
|
|
124
|
|
|
if ($decoded) { |
125
|
|
|
return $this->decoder($call); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $call; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param int $categoryId |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function foodFromCategory(int $categoryId, bool $decoded = true) |
136
|
|
|
{ |
137
|
|
|
$call = $this->call("category/{$categoryId}/food")->connect()->getBody(); |
138
|
|
|
if ($decoded) { |
139
|
|
|
return $this->decoder($call); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $call; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param int $foodId |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public function food(int $foodId, bool $decoded = true) |
150
|
|
|
{ |
151
|
|
|
$call = $this->call("food/{$foodId}")->connect()->getBody(); |
152
|
|
|
|
153
|
|
|
if ($decoded) { |
154
|
|
|
return $this->decoder($call); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $call; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $call |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
|
|
private function decoder($call) |
165
|
|
|
{ |
166
|
|
|
return json_decode($call); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $endpoint |
171
|
|
|
*/ |
172
|
|
|
private function setEndpoint($endpoint): void |
173
|
|
|
{ |
174
|
|
|
$this->endpoint = "api/v1/{$endpoint}"; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $endpoint |
179
|
|
|
* @param $method |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
private function call(string $endpoint, $method = self::REQUEST_GET): Taco |
183
|
|
|
{ |
184
|
|
|
$this->setEndpoint($endpoint); |
185
|
|
|
$this->method = $method; |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return Client |
191
|
|
|
*/ |
192
|
|
|
private function setClient(): Client |
193
|
|
|
{ |
194
|
|
|
return $this->client = new Client([ |
195
|
|
|
"base_uri" => $this->base, |
196
|
|
|
"timeout" => $this->timeout |
197
|
|
|
]); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return \Psr\Http\Message\ResponseInterface|string |
202
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
203
|
|
|
*/ |
204
|
|
|
protected function connect() |
205
|
|
|
{ |
206
|
|
|
$this->setClient(); |
207
|
|
|
|
208
|
|
|
$this->response = $this->client->request($this->method, $this->endpoint); |
209
|
|
|
|
210
|
|
|
if ($this->code = $this->response->getStatusCode() !== self::RESPONSE_ACCEPT) { |
211
|
|
|
return $this->message = $this->response->getReasonPhrase(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $this->response; |
215
|
|
|
} |
216
|
|
|
} |