1 | <?php |
||
2 | |||
3 | namespace JeroenG\Flickr; |
||
4 | |||
5 | use GuzzleHttp\Client; |
||
6 | |||
7 | class Api |
||
8 | { |
||
9 | protected string $key; |
||
10 | |||
11 | protected Client $client; |
||
12 | |||
13 | public string $format; |
||
14 | |||
15 | public function __construct( |
||
16 | string $apiKey, |
||
17 | string $format = 'php_serial', |
||
18 | string $endpoint = 'https://api.flickr.com/services/rest/' |
||
19 | ) { |
||
20 | $this->key = $apiKey; |
||
21 | $this->format = $format; |
||
22 | |||
23 | $this->client = new Client([ |
||
24 | 'base_uri' => $endpoint, |
||
25 | ]); |
||
26 | } |
||
27 | |||
28 | public function request(string $call, ?array $parameters = null) |
||
29 | { |
||
30 | $guzzleResponse = $this->client->get($this->api().'&method='.$call.$this->parameters($parameters)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
31 | |||
32 | if ($guzzleResponse->getStatusCode() === 200) { |
||
33 | return new Response($guzzleResponse); |
||
34 | } |
||
35 | |||
36 | return 'Failed request'; |
||
37 | } |
||
38 | |||
39 | protected function api(): string |
||
40 | { |
||
41 | return '?api_key='.$this->key.'&format='.$this->format; |
||
42 | } |
||
43 | |||
44 | protected function parameters(array $array): string |
||
45 | { |
||
46 | if (! is_array($array)) { |
||
0 ignored issues
–
show
|
|||
47 | return ''; |
||
48 | } |
||
49 | |||
50 | $encoded = []; |
||
51 | |||
52 | foreach ($array as $k => $v) { |
||
53 | $encoded[] = urlencode($k).'='.urlencode($v); |
||
54 | } |
||
55 | |||
56 | return '&'.implode('&', $encoded); |
||
57 | } |
||
58 | } |
||
59 |