diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c190ff..792b3e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.2] - 2024-02-27 + +### Changed + +* Updated the HTTP provider to replace HTTPlug factories by PSR-17 + + ## [0.2.1] - 2022-12-17 ### Changed diff --git a/README.md b/README.md index 0f1c323..7f1ebd9 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,9 @@ ![Packagist Version](https://img.shields.io/packagist/v/php-weather/tomorrow) ![PHP Weather Common Version](https://img.shields.io/badge/phpweather--core-0.4.*-brightgreen) -![PHP Weather HTTP Provider Version](https://img.shields.io/badge/phpweather--http--provider-0.5.*-brightgreen) +![PHP Weather HTTP Provider Version](https://img.shields.io/badge/phpweather--http--provider-0.6.*-brightgreen) ![GitHub Release Date](https://img.shields.io/github/release-date/php-weather/tomorrow) -![GitHub commits since tagged version](https://img.shields.io/github/commits-since/php-weather/tomorrow/0.2.1) +![GitHub commits since tagged version](https://img.shields.io/github/commits-since/php-weather/tomorrow/0.2.2) ![GitHub last commit](https://img.shields.io/github/last-commit/php-weather/tomorrow) ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/php-weather/tomorrow/php.yml?branch=main) ![GitHub](https://img.shields.io/github/license/php-weather/tomorrow) diff --git a/Src/Tomorrow.php b/Src/Tomorrow.php index 8e9820b..32ec6b5 100644 --- a/Src/Tomorrow.php +++ b/Src/Tomorrow.php @@ -40,6 +40,46 @@ protected function getCurrentWeatherQueryString(WeatherQuery $query): string return sprintf('https://api.tomorrow.io/v4/timelines?%s', $this->createQueryString($queryArray)); } + /** + * @return string[] + */ + private function getQueryFields(): array + { + return [ + 'cloudCover', + 'dewPoint', + 'temperatureApparent', + 'humidity', + 'precipitationIntensity', + 'precipitationProbability', + 'pressureSeaLevel', + 'temperature', + 'weatherCode', + 'windSpeed', + 'windDirection', + ]; + } + + /** + * @param array $queryArray + * @return string + */ + private function createQueryString(array $queryArray): string + { + $lines = []; + foreach ($queryArray as $key => $value) { + if (!is_array($value)) { + $lines[] = sprintf('%s=%s', $key, $value); + } else { + foreach ($value as $valueItem) { + $lines[] = sprintf('%s=%s', $key, $valueItem); + } + } + } + + return implode('&', $lines); + } + protected function getForecastWeatherQueryString(WeatherQuery $query): string { $queryArray = [ @@ -53,6 +93,11 @@ protected function getForecastWeatherQueryString(WeatherQuery $query): string return sprintf('https://api.tomorrow.io/v4/timelines?%s', $this->createQueryString($queryArray)); } + protected function getHistoricalTimeLineWeatherQueryString(WeatherQuery $query): string + { + return $this->getHistoricalWeatherQueryString($query); + } + protected function getHistoricalWeatherQueryString(WeatherQuery $query): string { $queryArray = [ @@ -70,11 +115,6 @@ protected function getHistoricalWeatherQueryString(WeatherQuery $query): string return sprintf('https://api.tomorrow.io/v4/timelines?%s', $this->createQueryString($queryArray)); } - protected function getHistoricalTimeLineWeatherQueryString(WeatherQuery $query): string - { - return $this->getHistoricalWeatherQueryString($query); - } - protected function mapRawData(float $latitude, float $longitude, array $rawData, ?string $type = null, ?string $units = null): Weather|WeatherCollection { $weatherCollection = new \PhpWeather\Common\WeatherCollection(); @@ -117,53 +157,6 @@ protected function mapRawData(float $latitude, float $longitude, array $rawData, return $weatherCollection; } - public function getSources(): array - { - return [ - new Source('tomorrow', 'tomorrow.io', 'https://www.tomorrow.io/'), - ]; - } - - /** - * @return string[] - */ - private function getQueryFields(): array - { - return [ - 'cloudCover', - 'dewPoint', - 'temperatureApparent', - 'humidity', - 'precipitationIntensity', - 'precipitationProbability', - 'pressureSeaLevel', - 'temperature', - 'weatherCode', - 'windSpeed', - 'windDirection', - ]; - } - - /** - * @param array $queryArray - * @return string - */ - private function createQueryString(array $queryArray): string - { - $lines = []; - foreach ($queryArray as $key => $value) { - if (!is_array($value)) { - $lines[] = sprintf('%s=%s', $key, $value); - } else { - foreach ($value as $valueItem) { - $lines[] = sprintf('%s=%s', $key, $valueItem); - } - } - } - - return implode('&', $lines); - } - /** * @param array $intervalRawData * @param float $latitude @@ -242,6 +235,13 @@ private function mapRawIntervalData(array $intervalRawData, float $latitude, flo return $weather; } + public function getSources(): array + { + return [ + new Source('tomorrow', 'tomorrow.io', 'https://www.tomorrow.io/'), + ]; + } + private function mapWeatherCode(int $weatherCode): ?int { $weatherCode = (int)substr((string)$weatherCode, 0, 4); diff --git a/Tests/TomorrowTest.php b/Tests/TomorrowTest.php index 49fcb28..ebc1d65 100644 --- a/Tests/TomorrowTest.php +++ b/Tests/TomorrowTest.php @@ -10,6 +10,7 @@ use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; class TomorrowTest extends TestCase { @@ -29,7 +30,7 @@ public function testCurrentWeather(): void { $latitude = 47.8739259; $longitude = 8.0043961; - $datetime = (new \DateTime())->setTimezone(new \DateTimeZone('UTC'))->setDate(2022, 07,31)->setTime(16,00); + $datetime = (new \DateTime())->setTimezone(new \DateTimeZone('UTC'))->setDate(2022, 07, 31)->setTime(16, 00); $testQuery = WeatherQuery::create($latitude, $longitude, $datetime); $testString = 'https://api.tomorrow.io/v4/timelines?location=47.8739259,8.0043961&fields=cloudCover&fields=dewPoint&fields=temperatureApparent&fields=humidity&fields=precipitationIntensity&fields=precipitationProbability&fields=pressureSeaLevel&fields=temperature&fields=weatherCode&fields=windSpeed&fields=windDirection&units=metric×teps=current&apikey=key'; @@ -37,8 +38,11 @@ public function testCurrentWeather(): void $this->requestFactory->expects(self::once())->method('createRequest')->with('GET', $testString)->willReturn($request); $responseBodyString = file_get_contents(__DIR__.'/resources/currentWeather.json'); + $body = $this->createMock(StreamInterface::class); + $body->method('__toString')->willReturn($responseBodyString); + $response = $this->createMock(ResponseInterface::class); - $response->expects(self::once())->method('getBody')->willReturn($responseBodyString); + $response->expects(self::once())->method('getBody')->willReturn($body); $this->client->expects(self::once())->method('sendRequest')->with($request)->willReturn($response); $currentWeather = $this->provider->getCurrentWeather($testQuery); diff --git a/composer.json b/composer.json index 6e93131..a75b163 100644 --- a/composer.json +++ b/composer.json @@ -16,12 +16,11 @@ "require": { "php": "^8", "ext-json": "*", - "php-weather/http-provider": "^0.5", + "php-weather/http-provider": "^0.6", "php-weather/core": "^0.4" }, "require-dev": { - "jetbrains/phpstorm-attributes": "^1.0", - "php-http/guzzle7-adapter": "dev-master", + "php-http/guzzle7-adapter": "^1.0", "phpstan/phpstan": "^1.6", "phpunit/phpunit": ">=8.0" },