|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* PHP version 5.5 |
|
5
|
|
|
* |
|
6
|
|
|
* @package Forecast |
|
7
|
|
|
* @author Sergey V.Kuzin <[email protected]> |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Forecast; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Fig\Cache\Memory\MemoryPool; |
|
15
|
|
|
use Forecast\Helper\Point; |
|
16
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
17
|
|
|
use Psr\Cache\NullCacheItemPool; |
|
18
|
|
|
use Psr\Log\LoggerInterface; |
|
19
|
|
|
use Psr\Log\NullLogger; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class WeatherAbstract |
|
23
|
|
|
* |
|
24
|
|
|
* PHP version 5.5 |
|
25
|
|
|
* |
|
26
|
|
|
* @package Forecast |
|
27
|
|
|
* @author Sergey V.Kuzin <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
29
|
|
|
* |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class WeatherAbstract |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @internal |
|
35
|
|
|
* @var NullCacheItemPool |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $cache = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @internal |
|
41
|
|
|
* @var LoggerInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $logger = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @internal |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $lang = 'en'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @internal |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $units = 'si'; |
|
56
|
|
|
|
|
57
|
|
|
/** Нет осадков */ |
|
58
|
|
|
const PRECIP_TYPE_NONE = 0; |
|
59
|
|
|
/** Дождь */ |
|
60
|
|
|
const PRECIP_TYPE_RAIN = 1; |
|
61
|
|
|
/** Снег */ |
|
62
|
|
|
const PRECIP_TYPE_SNOW = 2; |
|
63
|
|
|
/** Снег с дождём */ |
|
64
|
|
|
const PRECIP_TYPE_SLEET = 3; |
|
65
|
|
|
/** Град */ |
|
66
|
|
|
const PRECIP_TYPE_HAIL = 4; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* |
|
71
|
|
|
* @param CacheItemPoolInterface|null $cache Экземпляра класса кэширования по стандарту PSR-6 |
|
72
|
|
|
* @param LoggerInterface|null $logger Экземляр класса логера сандарта PSR-3 |
|
73
|
|
|
* |
|
74
|
|
|
* @api |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(CacheItemPoolInterface $cache = null, LoggerInterface $logger = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->cache = $cache ?: new MemoryPool(); |
|
|
|
|
|
|
79
|
|
|
$this->logger = $logger ?: new NullLogger(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Возвращает код языка |
|
84
|
|
|
* |
|
85
|
|
|
* @api |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getLang() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->lang; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @api |
|
96
|
|
|
* @param string $lang |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setLang($lang) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->lang = $lang; |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @api |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getUnits() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->units; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Возвращает объект с описанием текущей погоды |
|
116
|
|
|
* |
|
117
|
|
|
* @param Point $point Класс с коорденатами места для которой запрашивается погода |
|
118
|
|
|
* |
|
119
|
|
|
* @api |
|
120
|
|
|
* @return \Forecast\Current|null Объект с текущей погодой |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getCurrent(Point $point) |
|
123
|
|
|
{ |
|
124
|
|
|
$item = $this->cache->getItem($this->getCacheKeyCurrent($point)); |
|
125
|
|
|
if (!$item->exists()) { |
|
126
|
|
|
$item->set($this->doFetchCurrent($point), $this->getCacheExpirationCurrent()); |
|
127
|
|
|
$this->cache->save($item); |
|
128
|
|
|
} |
|
129
|
|
|
return $item->get(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @api |
|
134
|
|
|
* @return Hourly |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getHourly(Point $point, $forse = false) |
|
137
|
|
|
{ |
|
138
|
|
|
$item = $this->cache->getItem($this->getCacheKeyHourly($point)); |
|
139
|
|
|
if ($forse || !$item->exists()) { |
|
140
|
|
|
$item->set($this->doFetchHourly($point), $this->getCacheExpirationHourly()); |
|
141
|
|
|
$this->cache->save($item); |
|
142
|
|
|
} |
|
143
|
|
|
return $item->get(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @api |
|
149
|
|
|
* |
|
150
|
|
|
* @param string $units |
|
151
|
|
|
* @return $this |
|
152
|
|
|
*/ |
|
153
|
|
|
public function setUnits($units) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->units = $units; |
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param Point $point |
|
161
|
|
|
* |
|
162
|
|
|
* @internal |
|
163
|
|
|
* @return Current |
|
164
|
|
|
*/ |
|
165
|
|
|
abstract protected function doFetchCurrent(Point $point); |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param Point $point |
|
169
|
|
|
* @return Hourly |
|
170
|
|
|
*/ |
|
171
|
|
|
abstract protected function doFetchHourly(Point $point); |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param Point $point |
|
175
|
|
|
* |
|
176
|
|
|
* @internal |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
|
|
abstract protected function getCacheKeyCurrent(Point $point); |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param Point $point |
|
183
|
|
|
* |
|
184
|
|
|
* @internal |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
abstract protected function getCacheKeyHourly(Point $point); |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* |
|
191
|
|
|
* @internal |
|
192
|
|
|
* @return \DateTime |
|
193
|
|
|
*/ |
|
194
|
|
|
abstract protected function getCacheExpirationCurrent(); |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* |
|
198
|
|
|
* @internal |
|
199
|
|
|
* @return \DateTime |
|
200
|
|
|
*/ |
|
201
|
|
|
abstract protected function getCacheExpirationHourly(); |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/* abstract public function getDaily(); |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
abstract public function getHistory();*/ |
|
208
|
|
|
|
|
209
|
|
|
public function calcDewPoint($h) |
|
|
|
|
|
|
210
|
|
|
{ |
|
211
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..