voku/weather

Weather API Wrapper


README

SWUbanner

Build Status codecov.io

Weather Data Wrapper

This is a simple wrapper around the "Bright Sky" (https://brightsky.dev/) weather api.

  • you can simply replace the weather api by implementing another weather provider class.

DEMO: (https://github.com/voku/weather-demo)

http://weather-demo.suckup.de/

Install via "composer require"

composer require voku/weather
composer require voku/httpful # or another php-http/client-implementation e.g. "php-http/react-adapter" + "nyholm/psr7"

Usage:

use voku\weather\provider\BrightskyHttpProvider;
use voku\weather\WeatherQueryDto;

require_once __DIR__ . '/vendor/autoload.php'; // example path

$latitude = 48.137154;
$longitude = 11.576124;
$dateTime = new \DateTimeImmutable('2023-01-01 12:00:00');

$weatherQuery = new WeatherQueryDto(
    $latitude,
    $longitude,
    $dateTime
);

$weather = (new BrightskyHttpProvider())->getWeatherHistorical($weatherQuery);

echo $weather->temperature; // 17.1

Example 1: (temperature with unit)

echo $weather->getTemperatureWithUnit(); // 17.1 °C

Example 2: (wind-speed with unit)

echo $weather->getWindSpeedWithUnit(); // 9 km/h

Unit Test:

  1. Composer is a prerequisite for running the tests.
composer install
  1. The tests can be executed by running this command from the root directory:
./vendor/bin/phpunit

AbstractHttpProvider methods

getSources(): WeatherSourceDto[]

Parameters: nothing

Return:

  • \WeatherSourceDto[]

getWeatherCurrent(voku\weather\WeatherQueryDto $query):

Parameters:

  • voku\weather\WeatherQueryDto $query

Return:

  • voku\weather\WeatherDto

getWeatherForecast(voku\weather\WeatherQueryDto $query):

Parameters:

  • voku\weather\WeatherQueryDto $query

Return:

  • voku\weather\WeatherDto

getWeatherForecastCollection(voku\weather\WeatherQueryDto $query):

Parameters:

  • voku\weather\WeatherQueryDto $query

Return:

  • voku\weather\WeatherCollection

getWeatherHistorical(voku\weather\WeatherQueryDto $query):

Parameters:

  • voku\weather\WeatherQueryDto $query

Return:

  • voku\weather\WeatherDto

getWeatherHistoricalCollection(voku\weather\WeatherQueryDto $query):

Parameters:

  • voku\weather\WeatherQueryDto $query

Return:

  • voku\weather\WeatherCollection

Thanks

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Management, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to StyleCI for the simple but powerful code style check.
  • Thanks to PHPStan && Psalm for really great Static analysis tools and for discover bugs in the code!