Cmfcmf\OpenWeatherMap::__construct PHP Method

__construct() public method

Constructs the OpenWeatherMap object.
public __construct ( string $apiKey = '', null | Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface $fetcher = null, boolean | string $cache = false, integer $seconds = 600 )
$apiKey string The OpenWeatherMap API key. Required and only optional for BC.
$fetcher null | Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface The interface to fetch the data from OpenWeatherMap. Defaults to CurlFetcher() if cURL is available. Otherwise defaults to FileGetContentsFetcher() using 'file_get_contents()'.
$cache boolean | string If set to false, caching is disabled. Otherwise this must be a class extending AbstractCache. Defaults to false.
$seconds integer How long weather data shall be cached. Default 10 minutes.
    public function __construct($apiKey = '', $fetcher = null, $cache = false, $seconds = 600)
    {
        if (!is_string($apiKey) || empty($apiKey)) {
            // BC
            $seconds = $cache !== false ? $cache : 600;
            $cache = $fetcher !== null ? $fetcher : false;
            $fetcher = $apiKey !== '' ? $apiKey : null;
        } else {
            $this->apiKey = $apiKey;
        }
        if ($cache !== false && !$cache instanceof AbstractCache) {
            throw new \Exception('The cache class must implement the FetcherInterface!');
        }
        if (!is_numeric($seconds)) {
            throw new \Exception('$seconds must be numeric.');
        }
        if (!isset($fetcher)) {
            $fetcher = function_exists('curl_version') ? new CurlFetcher() : new FileGetContentsFetcher();
        }
        if ($seconds == 0) {
            $cache = false;
        }
        $this->cache = $cache;
        $this->seconds = $seconds;
        $this->fetcher = $fetcher;
    }