1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Marek\OpenWeatherMap\Factory; |
6
|
|
|
|
7
|
|
|
use Marek\OpenWeatherMap\API\Value\Configuration\APIConfiguration; |
8
|
|
|
use Marek\OpenWeatherMap\API\Value\Parameter\GetParameterInterface; |
9
|
|
|
use Marek\OpenWeatherMap\API\Value\Parameter\InputParameterBag; |
10
|
|
|
use Marek\OpenWeatherMap\API\Value\Parameter\UriParameterInterface; |
11
|
|
|
|
12
|
|
|
final class UrlFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Marek\OpenWeatherMap\API\Value\Configuration\APIConfiguration |
16
|
|
|
*/ |
17
|
|
|
private $configuration; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* UrlFactory constructor. |
21
|
|
|
* |
22
|
|
|
* @param \Marek\OpenWeatherMap\API\Value\Configuration\APIConfiguration $configuration |
23
|
|
|
*/ |
24
|
|
|
public function __construct(APIConfiguration $configuration) |
25
|
|
|
{ |
26
|
|
|
$this->configuration = $configuration; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \Marek\OpenWeatherMap\API\Value\Parameter\InputParameterBag $bag |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function build(InputParameterBag $bag): string |
35
|
|
|
{ |
36
|
|
|
$url = $bag->getUrl(); |
37
|
|
|
$url = $this->transformUriParameters($url, $bag); |
38
|
|
|
|
39
|
|
|
return $this->transformGetParameters($url, $bag); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $url |
44
|
|
|
* |
45
|
|
|
* @return \Marek\OpenWeatherMap\API\Value\Parameter\InputParameterBag |
46
|
|
|
*/ |
47
|
|
|
public function buildBag(string $url): InputParameterBag |
48
|
|
|
{ |
49
|
|
|
return new InputParameterBag($url); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Transforms Uri paramters. |
54
|
|
|
* |
55
|
|
|
* @param string $url |
56
|
|
|
* @param \Marek\OpenWeatherMap\API\Value\Parameter\InputParameterBag $bag |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
private function transformUriParameters(string $url, InputParameterBag $bag): string |
61
|
|
|
{ |
62
|
|
|
foreach ($bag->getUriParameters() as $item) { |
63
|
|
|
if ($item instanceof UriParameterInterface) { |
64
|
|
|
$name = '{' . $item->getUriParameterName() . '}'; |
65
|
|
|
$url = str_replace($name, $item->getUriParameterValue(), $url); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $url; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Transforms Uri GET parameters. |
74
|
|
|
* |
75
|
|
|
* @param string $url |
76
|
|
|
* @param \Marek\OpenWeatherMap\API\Value\Parameter\InputParameterBag $bag |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
private function transformGetParameters(string $url, InputParameterBag $bag): string |
81
|
|
|
{ |
82
|
|
|
$params = []; |
83
|
|
|
foreach ($bag->getGetParameters() as $item) { |
84
|
|
|
if ($item instanceof GetParameterInterface) { |
85
|
|
|
$params[$item->getGetParameterName()] = $item->getGetParameterValue(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$params['appid'] = $this->configuration->getKey(); |
90
|
|
|
$params['units'] = $this->configuration->getUnits(); |
91
|
|
|
$params['lang'] = $this->configuration->getLanguage(); |
92
|
|
|
$params['type'] = $this->configuration->getType(); |
93
|
|
|
|
94
|
|
|
return $url . '?' . http_build_query($params); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|