|
21 | 21 | */ |
22 | 22 | class FileGetContentsClientAdapter implements ClientAdapterInterface |
23 | 23 | { |
24 | | - private $requestedUrl = null; |
25 | | - private $response = null; |
26 | | - private $responseHeaders = []; |
| 24 | + private $requestedUrl = null; |
| 25 | + private $response = null; |
| 26 | + private $responseHeaders = []; |
27 | 27 |
|
28 | | - /** |
29 | | - * Returns the response data from the Forecast.io in the |
30 | | - * form of an array |
31 | | - * |
32 | | - * @param float $latitude |
33 | | - * @param float $longitude |
34 | | - * @param \DateTime $time |
35 | | - * |
36 | | - * @return array |
37 | | - */ |
38 | | - public function getForecast($latitude, $longitude, \DateTime $time = null) |
39 | | - { |
40 | | - $this->requestedUrl = Overcast::API_ENDPOINT.Overcast::getApiKey().'/'.$latitude.','.$longitude; |
| 28 | + /** |
| 29 | + * Returns the response data from the Forecast.io in the |
| 30 | + * form of an array |
| 31 | + * |
| 32 | + * @param float $latitude |
| 33 | + * @param float $longitude |
| 34 | + * @param \DateTime $time |
| 35 | + * @param array $parameters |
| 36 | + * |
| 37 | + * @return array |
| 38 | + */ |
| 39 | + public function getForecast($latitude, $longitude, \DateTime $time = null, array $parameters = null) |
| 40 | + { |
| 41 | + $this->requestedUrl = Overcast::API_ENDPOINT . Overcast::getApiKey() . '/' . $latitude . ',' . $longitude; |
41 | 42 |
|
42 | | - if (!is_null($time)) { |
43 | | - $this->requestedUrl .= ','.$time->getTimestamp(); |
44 | | - } |
| 43 | + if (!is_null($time)) { |
| 44 | + $this->requestedUrl .= ',' . $time->getTimestamp(); |
| 45 | + } |
45 | 46 |
|
46 | | - $this->response = json_decode(file_get_contents($this->requestedUrl),true); |
47 | | - $this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header); |
| 47 | + if (!is_null($parameters)) { |
| 48 | + $this->requestedUrl .= '?' . http_build_query($parameters); |
| 49 | + } |
48 | 50 |
|
49 | | - return $this->response; |
50 | | - } |
| 51 | + $this->response = json_decode(file_get_contents($this->requestedUrl), true); |
| 52 | + $this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header); |
51 | 53 |
|
52 | | - /** |
53 | | - * Returns the relevant response headers from the Forecast.io API |
54 | | - * |
55 | | - * @return array |
56 | | - */ |
57 | | - public function getHeaders() |
58 | | - { |
59 | | - return $this->responseHeaders; |
60 | | - } |
| 54 | + return $this->response; |
| 55 | + } |
61 | 56 |
|
62 | | - /** |
63 | | - * Parses the response headers |
64 | | - * |
65 | | - * @param array $headers |
66 | | - * |
67 | | - * @return array |
68 | | - */ |
69 | | - private function parseForecastResponseHeaders($headers) |
70 | | - { |
71 | | - $responseHeaders = [ |
72 | | - 'cache' => [ |
73 | | - 'maxAge'=>null, |
74 | | - 'expires'=>null |
75 | | - ], |
76 | | - 'responseTime'=>null, |
77 | | - 'apiCalls'=>null |
78 | | - ]; |
79 | | - foreach ($headers as $header) { |
80 | | - switch (true) { |
81 | | - case (substr($header,0,14) === 'Cache-Control:'): |
82 | | - $responseHeaders['cache']['maxAge'] = trim(substr($header,strrpos($header,'=')+1)); |
83 | | - break; |
84 | | - case (substr($header,0,8) === 'Expires:'): |
85 | | - $responseHeaders['cache']['expires'] = trim(substr($header,8)); |
86 | | - break; |
87 | | - case (substr($header,0,21) === 'X-Forecast-API-Calls:'): |
88 | | - $responseHeaders['apiCalls'] = trim(substr($header,21)); |
89 | | - break; |
90 | | - case (substr($header,0,16) === 'X-Response-Time:'): |
91 | | - $responseHeaders['responseTime'] = (int)trim(substr($header,16)); |
92 | | - break; |
93 | | - default: |
94 | | - break; |
95 | | - } |
96 | | - } |
97 | | - return $responseHeaders; |
98 | | - } |
| 57 | + /** |
| 58 | + * Returns the relevant response headers from the Forecast.io API |
| 59 | + * |
| 60 | + * @return array |
| 61 | + */ |
| 62 | + public function getHeaders() |
| 63 | + { |
| 64 | + return $this->responseHeaders; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Parses the response headers |
| 69 | + * |
| 70 | + * @param array $headers |
| 71 | + * |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + private function parseForecastResponseHeaders($headers) |
| 75 | + { |
| 76 | + $responseHeaders = [ |
| 77 | + 'cache' => [ |
| 78 | + 'maxAge' => null, |
| 79 | + 'expires' => null |
| 80 | + ], |
| 81 | + 'responseTime' => null, |
| 82 | + 'apiCalls' => null |
| 83 | + ]; |
| 84 | + foreach ($headers as $header) { |
| 85 | + switch (true) { |
| 86 | + case (substr($header, 0, 14) === 'Cache-Control:'): |
| 87 | + $responseHeaders['cache']['maxAge'] = trim(substr($header, strrpos($header, '=') + 1)); |
| 88 | + break; |
| 89 | + case (substr($header, 0, 8) === 'Expires:'): |
| 90 | + $responseHeaders['cache']['expires'] = trim(substr($header, 8)); |
| 91 | + break; |
| 92 | + case (substr($header, 0, 21) === 'X-Forecast-API-Calls:'): |
| 93 | + $responseHeaders['apiCalls'] = trim(substr($header, 21)); |
| 94 | + break; |
| 95 | + case (substr($header, 0, 16) === 'X-Response-Time:'): |
| 96 | + $responseHeaders['responseTime'] = (int)trim(substr($header, 16)); |
| 97 | + break; |
| 98 | + default: |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + return $responseHeaders; |
| 103 | + } |
99 | 104 | } |
0 commit comments