17
17
18
18
namespace Cmfcmf ;
19
19
20
- use Cmfcmf \OpenWeatherMap \AbstractCache ;
21
20
use Cmfcmf \OpenWeatherMap \CurrentWeather ;
22
21
use Cmfcmf \OpenWeatherMap \UVIndex ;
23
22
use Cmfcmf \OpenWeatherMap \CurrentWeatherGroup ;
27
26
use Cmfcmf \OpenWeatherMap \Fetcher \FileGetContentsFetcher ;
28
27
use Cmfcmf \OpenWeatherMap \WeatherForecast ;
29
28
use Cmfcmf \OpenWeatherMap \WeatherHistory ;
29
+ use Psr \Cache \CacheItemPoolInterface ;
30
30
31
31
/**
32
32
* Main class for the OpenWeatherMap-PHP-API. Only use this class.
@@ -74,14 +74,14 @@ class OpenWeatherMap
74
74
private $ uvIndexUrl = 'https://api.openweathermap.org/data/2.5/uvi ' ;
75
75
76
76
/**
77
- * @var AbstractCache|bool $cache The cache to use.
77
+ * @var CacheItemPoolInterface|null $cache The cache to use.
78
78
*/
79
- private $ cache = false ;
79
+ private $ cache = null ;
80
80
81
81
/**
82
82
* @var int
83
83
*/
84
- private $ seconds ;
84
+ private $ ttl ;
85
85
86
86
/**
87
87
* @var bool
@@ -101,41 +101,36 @@ class OpenWeatherMap
101
101
/**
102
102
* Constructs the OpenWeatherMap object.
103
103
*
104
- * @param string $apiKey The OpenWeatherMap API key. Required.
105
- * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
106
- * CurlFetcher() if cURL is available. Otherwise defaults to
107
- * FileGetContentsFetcher() using 'file_get_contents()'.
108
- * @param bool|string $cache If set to false, caching is disabled. Otherwise this must be a class
109
- * extending AbstractCache. Defaults to false.
110
- * @param int $seconds How long weather data shall be cached. Default 10 minutes.
111
- *
112
- * @throws \Exception If $cache is neither false nor a valid callable extending Cmfcmf\OpenWeatherMap\Util\Cache.
104
+ * @param string $apiKey The OpenWeatherMap API key. Required.
105
+ * @param null|FetcherInterface $fetcher The interface to fetch the data from OpenWeatherMap. Defaults to
106
+ * CurlFetcher() if cURL is available. Otherwise defaults to
107
+ * FileGetContentsFetcher() using 'file_get_contents()'.
108
+ * @param null|CacheItemPoolInterface $cache If set to null, caching is disabled. Otherwise this must be
109
+ * a PSR 16-compatible cache instance.
110
+ * @param int $ttl How long weather data shall be cached. Defaults to 10 minutes.
111
+ * Only used if $cache is not null.
113
112
*
114
113
* @api
115
114
*/
116
- public function __construct ($ apiKey , $ fetcher = null , $ cache = false , $ seconds = 600 )
115
+ public function __construct ($ apiKey , $ fetcher = null , $ cache = null , $ ttl = 600 )
117
116
{
118
117
if (!is_string ($ apiKey ) || empty ($ apiKey )) {
119
118
throw new \InvalidArgumentException ("You must provide an API key. " );
120
119
}
121
120
122
- $ this ->apiKey = $ apiKey ;
123
-
124
- if ($ cache !== false && !($ cache instanceof AbstractCache)) {
125
- throw new \InvalidArgumentException ('The cache class must implement the FetcherInterface! ' );
121
+ if ($ cache !== null && !($ cache instanceof CacheItemPoolInterface)) {
122
+ throw new \InvalidArgumentException ('The cache class must implement the \Psr\Cache\CacheItemPoolInterface! ' );
126
123
}
127
- if (!is_numeric ($ seconds )) {
124
+ if (!is_numeric ($ ttl )) {
128
125
throw new \InvalidArgumentException ('$seconds must be numeric. ' );
129
126
}
130
127
if (!isset ($ fetcher )) {
131
128
$ fetcher = (function_exists ('curl_version ' )) ? new CurlFetcher () : new FileGetContentsFetcher ();
132
129
}
133
- if ($ seconds == 0 ) {
134
- $ cache = false ;
135
- }
136
130
131
+ $ this ->apiKey = $ apiKey ;
137
132
$ this ->cache = $ cache ;
138
- $ this ->seconds = $ seconds ;
133
+ $ this ->ttl = $ ttl ;
139
134
$ this ->fetcher = $ fetcher ;
140
135
}
141
136
@@ -576,18 +571,21 @@ public function wasCached()
576
571
*/
577
572
private function cacheOrFetchResult ($ url )
578
573
{
579
- if ($ this ->cache !== false ) {
580
- /** @var AbstractCache $cache */
581
- $ cache = $ this ->cache ;
582
- $ cache ->setSeconds ($ this ->seconds );
583
-
584
- if ($ cache ->isCached ($ url )) {
574
+ if ($ this ->cache !== null ) {
575
+ $ key = str_replace (
576
+ ["{ " , "} " , "( " , ") " , "/ " , "\\" , "@ " , ": " ],
577
+ ["_ " , "_ " , "_ " , "_ " , "_ " , "_ " , "_ " , "_ " ],
578
+ $ url );
579
+ $ item = $ this ->cache ->getItem ($ key );
580
+ if ($ item ->isHit ()) {
585
581
$ this ->wasCached = true ;
586
- return $ cache -> getCached ( $ url );
582
+ return $ item -> get ( );
587
583
}
588
584
589
585
$ result = $ this ->fetcher ->fetch ($ url );
590
- $ cache ->setCached ($ url , $ result );
586
+ $ item ->set ($ result );
587
+ $ item ->expiresAfter ($ this ->ttl );
588
+ $ this ->cache ->save ($ item );
591
589
} else {
592
590
$ result = $ this ->fetcher ->fetch ($ url );
593
591
}
@@ -712,7 +710,7 @@ private function parseXML($answer)
712
710
/**
713
711
* @param string $answer The content returned by OpenWeatherMap.
714
712
*
715
- * @return \stdClass
713
+ * @return \stdClass|array
716
714
* @throws OWMException If the content isn't valid JSON.
717
715
*/
718
716
private function parseJson ($ answer )
0 commit comments