Skip to content

Commit 35cefe6

Browse files
committed
Merge pull request #1 from Baabah/parameter-support
Added support for parameters changed indenting to spaces
2 parents 41fb2dd + 192b5a3 commit 35cefe6

File tree

10 files changed

+932
-914
lines changed

10 files changed

+932
-914
lines changed

src/ClientAdapterInterface.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@
1717
*/
1818
interface ClientAdapterInterface
1919
{
20-
/**
21-
* Returns the response data from the Forecast.io in the
22-
* form of an array
23-
*
24-
* @param float $latitude
25-
* @param float $longitude
26-
* @param \DateTime $time
27-
*
28-
* @return array
29-
*/
30-
public function getForecast($latitude, $longitude, \DateTime $time = null);
20+
/**
21+
* Returns the response data from the Forecast.io in the
22+
* form of an array
23+
*
24+
* @param float $latitude
25+
* @param float $longitude
26+
* @param \DateTime $time
27+
* @param array $parameters
28+
*
29+
* @return array
30+
*/
31+
public function getForecast($latitude, $longitude, \DateTime $time = null, array $parameters = null);
3132

32-
/**
33-
* Returns the relevant response headers from the Forecast.io API
34-
*
35-
* @return array
36-
*/
37-
public function getHeaders();
33+
/**
34+
* Returns the relevant response headers from the Forecast.io API
35+
*
36+
* @return array
37+
*/
38+
public function getHeaders();
3839
}

src/ClientAdapters/FileGetContentsClientAdapter.php

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,79 +21,84 @@
2121
*/
2222
class FileGetContentsClientAdapter implements ClientAdapterInterface
2323
{
24-
private $requestedUrl = null;
25-
private $response = null;
26-
private $responseHeaders = [];
24+
private $requestedUrl = null;
25+
private $response = null;
26+
private $responseHeaders = [];
2727

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;
4142

42-
if (!is_null($time)) {
43-
$this->requestedUrl .= ','.$time->getTimestamp();
44-
}
43+
if (!is_null($time)) {
44+
$this->requestedUrl .= ',' . $time->getTimestamp();
45+
}
4546

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+
}
4850

49-
return $this->response;
50-
}
51+
$this->response = json_decode(file_get_contents($this->requestedUrl), true);
52+
$this->responseHeaders = $this->parseForecastResponseHeaders($http_response_header);
5153

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+
}
6156

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+
}
99104
}

src/ClientAdapters/GuzzleClientAdapter.php

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,56 +21,67 @@
2121
*/
2222
class GuzzleClientAdapter implements ClientAdapterInterface
2323
{
24-
/**
25-
* @var Client
26-
*/
27-
private $guzzleClient;
28-
private $requestedUrl = null;
29-
private $responseHeaders = [];
24+
/**
25+
* @var Client
26+
*/
27+
private $guzzleClient;
28+
private $requestedUrl = null;
29+
private $responseHeaders = [];
3030

31-
/**
32-
* @param Client $guzzleClient
33-
*/
34-
public function __construct(Client $guzzleClient = null)
35-
{
36-
if (is_null($guzzleClient)){
37-
$guzzleClient = new Client();
38-
}
39-
$this->guzzleClient = $guzzleClient;
40-
}
31+
/**
32+
* @param Client $guzzleClient
33+
*/
34+
public function __construct(Client $guzzleClient = null)
35+
{
36+
if (is_null($guzzleClient)) {
37+
$guzzleClient = new Client();
38+
}
39+
$this->guzzleClient = $guzzleClient;
40+
}
4141

42-
/**
43-
* Returns the response data from the Forecast.io in the
44-
* form of an array
45-
*
46-
* @param float $latitude
47-
* @param float $longitude
48-
* @param \DateTime $time
49-
*
50-
* @return array
51-
*/
52-
public function getForecast($latitude, $longitude, \DateTime $time = null)
53-
{
54-
$this->requestedUrl = Overcast::API_ENDPOINT.Overcast::getApiKey().'/'.$latitude.','.$longitude;
55-
$response = $this->guzzleClient->get($this->requestedUrl);
56-
$this->responseHeaders = [
57-
'cache' => [
58-
'maxAge'=>(int)trim(substr($response->getHeader('cache-control'),strrpos($response->getHeader('cache-control'),'=')+1)),
59-
'expires'=>$response->getHeader('expires')
60-
],
61-
'responseTime'=>(int)$response->getHeader('x-response-time'),
62-
'apiCalls'=>(int)$response->getHeader('x-forecast-api-calls')
63-
];
64-
return $response->json();
65-
}
42+
/**
43+
* Returns the response data from the Forecast.io in the
44+
* form of an array
45+
*
46+
* @param float $latitude
47+
* @param float $longitude
48+
* @param \DateTime $time
49+
* @param array $parameters
50+
*
51+
* @return array
52+
*/
53+
public function getForecast($latitude, $longitude, \DateTime $time = null, array $parameters = null)
54+
{
55+
$this->requestedUrl = Overcast::API_ENDPOINT . Overcast::getApiKey() . '/' . $latitude . ',' . $longitude;
6656

67-
/**
68-
* Returns the relevant response headers from the Forecast.io API
69-
*
70-
* @return array
71-
*/
72-
public function getHeaders()
73-
{
74-
return $this->responseHeaders;
75-
}
57+
if (!is_null($time)) {
58+
$this->requestedUrl .= ',' . $time->getTimestamp();
59+
}
60+
61+
if (!is_null($parameters)) {
62+
$this->requestedUrl .= '?' . http_build_query($parameters);
63+
}
64+
65+
$response = $this->guzzleClient->get($this->requestedUrl);
66+
$this->responseHeaders = [
67+
'cache' => [
68+
'maxAge' => (int)trim(substr($response->getHeader('cache-control'),
69+
strrpos($response->getHeader('cache-control'), '=') + 1)),
70+
'expires' => $response->getHeader('expires')
71+
],
72+
'responseTime' => (int)$response->getHeader('x-response-time'),
73+
'apiCalls' => (int)$response->getHeader('x-forecast-api-calls')
74+
];
75+
return $response->json();
76+
}
77+
78+
/**
79+
* Returns the relevant response headers from the Forecast.io API
80+
*
81+
* @return array
82+
*/
83+
public function getHeaders()
84+
{
85+
return $this->responseHeaders;
86+
}
7687
}

0 commit comments

Comments
 (0)