|
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 | | - * @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; |
| 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; |
42 | 42 |
|
43 | 43 | if (!is_null($time)) { |
44 | | - $this->requestedUrl .= ','.$time->getTimestamp(); |
| 44 | + $this->requestedUrl .= ',' . $time->getTimestamp(); |
45 | 45 | } |
46 | 46 |
|
47 | 47 | if (!is_null($parameters)) { |
48 | 48 | $this->requestedUrl .= '?' . http_build_query($parameters); |
49 | 49 | } |
50 | 50 |
|
51 | | - $this->response = json_decode(file_get_contents($this->requestedUrl),true); |
52 | | - $this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header); |
| 51 | + $this->response = json_decode(file_get_contents($this->requestedUrl), true); |
| 52 | + $this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header); |
53 | 53 |
|
54 | | - return $this->response; |
55 | | - } |
| 54 | + return $this->response; |
| 55 | + } |
56 | 56 |
|
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 | | - } |
| 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 | 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 | | - } |
| 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 | + } |
104 | 104 | } |
0 commit comments