|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author: James Murray <jaimz@vertigolabs.org> |
| 4 | + * @copyright: |
| 5 | + * @date: 4/14/2015 |
| 6 | + * @time: 12:50 PM |
| 7 | + */ |
| 8 | +namespace VertigoLabs\Overcast\ClientAdapters; |
| 9 | + |
| 10 | +use VertigoLabs\Overcast\ClientAdapterInterface; |
| 11 | +use VertigoLabs\Overcast\Overcast; |
| 12 | + |
| 13 | +/** |
| 14 | + * Class FileGetContentsClientAdapter |
| 15 | + * |
| 16 | + * The FileGetContents client adapter uses PHP's built in |
| 17 | + * file_get_contents function to retrieve data from the |
| 18 | + * Forecast.io API |
| 19 | + * |
| 20 | + * @package VertigoLabs\Overcast\ClientAdapters |
| 21 | + */ |
| 22 | +class FileGetContentsClientAdapter implements ClientAdapterInterface |
| 23 | +{ |
| 24 | + private $requestedUrl = null; |
| 25 | + private $response = null; |
| 26 | + private $responseHeaders = []; |
| 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; |
| 41 | + |
| 42 | + if (!is_null($time)) { |
| 43 | + $this->requestedUrl .= ','.$time->getTimestamp(); |
| 44 | + } |
| 45 | + |
| 46 | + $this->response = json_decode(file_get_contents($this->requestedUrl),true); |
| 47 | + $this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header); |
| 48 | + |
| 49 | + return $this->response; |
| 50 | + } |
| 51 | + |
| 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 | + } |
| 61 | + |
| 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 | + } |
| 99 | +} |
0 commit comments