1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rawaby88\OpenWeatherMap\Services; |
4
|
|
|
|
5
|
|
|
use Rawaby88\OpenWeatherMap\Interfaces\CWSingleResultInterface; |
6
|
|
|
use Rawaby88\OpenWeatherMap\Traits\CWSingleResultTrait; |
7
|
|
|
use Rawaby88\OpenWeatherMap\WeatherFactory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class CWByCityName. |
11
|
|
|
*/ |
12
|
|
|
class CWByCityName extends WeatherFactory implements CWSingleResultInterface |
13
|
|
|
{ |
14
|
|
|
use CWSingleResultTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string The city name. |
18
|
|
|
*/ |
19
|
|
|
protected $cityName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The state code. |
23
|
|
|
*/ |
24
|
|
|
protected $stateCode; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string ISO 3166 country codes. |
28
|
|
|
*/ |
29
|
|
|
protected $countryCode; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* CWByCityName constructor. |
33
|
|
|
* |
34
|
|
|
* You can call by city name or city name, state code and country code. |
35
|
|
|
* Please note that searching by states available only for the USA locations. |
36
|
|
|
* |
37
|
|
|
* @param string $cityName The city name. |
38
|
|
|
* @param string|null $countryCode ISO 3166 country codes. |
39
|
|
|
* @param string|null $stateCode The state code. |
40
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
41
|
|
|
*/ |
42
|
|
|
public function __construct(string $cityName, string $countryCode = null, string $stateCode = null) |
43
|
|
|
{ |
44
|
|
|
parent::__construct(); |
45
|
|
|
$this->apiCall = 'weather'; |
46
|
|
|
$this->cityName = $cityName; |
47
|
|
|
$this->stateCode = $stateCode; |
48
|
|
|
$this->countryCode = $countryCode; |
49
|
|
|
$this->params = $this->paramsToArray(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Generate query parameters for api call. |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function paramsToArray(): array |
57
|
|
|
{ |
58
|
|
|
$q = $this->cityName; |
59
|
|
|
|
60
|
|
|
if ($this->stateCode) { |
61
|
|
|
$q .= ','.$this->stateCode; |
62
|
|
|
} |
63
|
|
|
if ($this->countryCode) { |
64
|
|
|
$q .= ','.$this->countryCode; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return ['q' => $q]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|