Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ProgrammatorDev\OpenWeatherMap\Exception\ValidationException;
use ProgrammatorDev\OpenWeatherMap\HttpClient\HttpClientBuilder;
use ProgrammatorDev\OpenWeatherMap\Language\Language;
use ProgrammatorDev\OpenWeatherMap\MeasurementSystem\MeasurementSystem;
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
use ProgrammatorDev\OpenWeatherMap\Validator\BlankValidatorTrait;
use ProgrammatorDev\OpenWeatherMap\Validator\ChoiceValidatorTrait;
use Psr\Cache\CacheItemPoolInterface;
Expand All @@ -29,7 +29,7 @@ public function __construct(array $options = [])
private function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'measurementSystem' => MeasurementSystem::METRIC,
'unitSystem' => UnitSystem::METRIC,
'language' => Language::ENGLISH,
'httpClientBuilder' => new HttpClientBuilder(),
'cache' => null,
Expand All @@ -39,7 +39,7 @@ private function configureOptions(OptionsResolver $resolver): void
$resolver->setRequired('applicationKey');

$resolver->setAllowedTypes('applicationKey', 'string');
$resolver->setAllowedTypes('measurementSystem', 'string');
$resolver->setAllowedTypes('unitSystem', 'string');
$resolver->setAllowedTypes('language', 'string');
$resolver->setAllowedTypes('httpClientBuilder', HttpClientBuilder::class);
$resolver->setAllowedTypes('cache', ['null', CacheItemPoolInterface::class]);
Expand All @@ -48,7 +48,7 @@ private function configureOptions(OptionsResolver $resolver): void
$resolver->setAllowedValues('applicationKey', function($value) {
return !empty($value);
});
$resolver->setAllowedValues('measurementSystem', MeasurementSystem::getList());
$resolver->setAllowedValues('unitSystem', UnitSystem::getList());
$resolver->setAllowedValues('language', Language::getList());
}

Expand All @@ -69,19 +69,19 @@ public function setApplicationKey(string $applicationKey): self
return $this;
}

public function getMeasurementSystem(): string
public function getUnitSystem(): string
{
return $this->options['measurementSystem'];
return $this->options['unitSystem'];
}

/**
* @throws ValidationException
*/
public function setMeasurementSystem(string $measurementSystem): self
public function setUnitSystem(string $unitSystem): self
{
$this->validateChoice('measurementSystem', $measurementSystem, MeasurementSystem::getList());
$this->validateChoice('unitSystem', $unitSystem, UnitSystem::getList());

$this->options['measurementSystem'] = $measurementSystem;
$this->options['unitSystem'] = $unitSystem;

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Endpoint/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AbstractEndpoint

private ?LoggerInterface $logger;

protected string $measurementSystem;
protected string $unitSystem;

protected string $language;

Expand All @@ -48,7 +48,7 @@ public function __construct(protected OpenWeatherMap $api)
$this->httpClientBuilder = $this->config->getHttpClientBuilder();
$this->cache = $this->config->getCache();
$this->logger = $this->config->getLogger();
$this->measurementSystem = $this->config->getMeasurementSystem();
$this->unitSystem = $this->config->getUnitSystem();
$this->language = $this->config->getLanguage();
}

Expand Down
10 changes: 5 additions & 5 deletions src/Endpoint/OneCallEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Http\Client\Exception;
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithLanguageTrait;
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithMeasurementSystemTrait;
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithUnitSystemTrait;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\HistoryDaySummary;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\HistoryMoment;
use ProgrammatorDev\OpenWeatherMap\Entity\OneCall\OneCall;
Expand All @@ -19,7 +19,7 @@

class OneCallEndpoint extends AbstractEndpoint
{
use WithMeasurementSystemTrait;
use WithUnitSystemTrait;
use WithLanguageTrait;
use CoordinateValidatorTrait;
use LessThanValidatorTrait;
Expand Down Expand Up @@ -49,7 +49,7 @@ public function getWeather(float $latitude, float $longitude): OneCall
query: [
'lat' => $latitude,
'lon' => $longitude,
'units' => $this->getMeasurementSystem(),
'units' => $this->getUnitSystem(),
'lang' => $this->getLanguage()
]
);
Expand Down Expand Up @@ -78,7 +78,7 @@ public function getHistoryMoment(float $latitude, float $longitude, \DateTimeInt
'lat' => $latitude,
'lon' => $longitude,
'dt' => $dateTime->setTimezone(new \DateTimeZone('UTC'))->getTimestamp(),
'units' => $this->getMeasurementSystem(),
'units' => $this->getUnitSystem(),
'lang' => $this->getLanguage()
]
);
Expand Down Expand Up @@ -107,7 +107,7 @@ public function getHistoryDaySummary(float $latitude, float $longitude, \DateTim
'lat' => $latitude,
'lon' => $longitude,
'date' => $dateTime->format('Y-m-d'),
'units' => $this->getMeasurementSystem(),
'units' => $this->getUnitSystem(),
'lang' => $this->getLanguage()
]
);
Expand Down
26 changes: 0 additions & 26 deletions src/Endpoint/Util/WithMeasurementSystemTrait.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/Endpoint/Util/WithUnitSystemTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Endpoint\Util;

use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
use ProgrammatorDev\OpenWeatherMap\Validator\ChoiceValidatorTrait;

trait WithUnitSystemTrait
{
use ChoiceValidatorTrait;

public function withUnitSystem(string $unitSystem): static
{
$this->validateChoice('unitSystem', $unitSystem, UnitSystem::getList());

$clone = clone $this;
$clone->unitSystem = $unitSystem;

return $clone;
}

public function getUnitSystem(): string
{
return $this->unitSystem;
}
}
8 changes: 4 additions & 4 deletions src/Endpoint/WeatherEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Http\Client\Exception;
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithLanguageTrait;
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithMeasurementSystemTrait;
use ProgrammatorDev\OpenWeatherMap\Endpoint\Util\WithUnitSystemTrait;
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\CurrentWeather;
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherList;
use ProgrammatorDev\OpenWeatherMap\Exception\BadRequestException;
Expand All @@ -18,7 +18,7 @@

class WeatherEndpoint extends AbstractEndpoint
{
use WithMeasurementSystemTrait;
use WithUnitSystemTrait;
use WithLanguageTrait;
use CoordinateValidatorTrait;
use GreaterThanValidatorTrait;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function getCurrent(float $latitude, float $longitude): CurrentWeather
query: [
'lat' => $latitude,
'lon' => $longitude,
'units' => $this->getMeasurementSystem(),
'units' => $this->getUnitSystem(),
'lang' => $this->getLanguage()
]
);
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getForecast(float $latitude, float $longitude, int $numResults =
'lat' => $latitude,
'lon' => $longitude,
'cnt' => $numResults,
'units' => $this->getMeasurementSystem(),
'units' => $this->getUnitSystem(),
'lang' => $this->getLanguage()
]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\MeasurementSystem;
namespace ProgrammatorDev\OpenWeatherMap\UnitSystem;

class Fahrenheit
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\MeasurementSystem;
namespace ProgrammatorDev\OpenWeatherMap\UnitSystem;

class Imperial
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\MeasurementSystem;
namespace ProgrammatorDev\OpenWeatherMap\UnitSystem;

class Standard
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\MeasurementSystem;
namespace ProgrammatorDev\OpenWeatherMap\UnitSystem;

class MeasurementSystem
class UnitSystem
{
public const METRIC = 'metric';
public const IMPERIAL = 'imperial';
Expand Down
22 changes: 11 additions & 11 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function setUp(): void
public function testConfigDefaultOptions()
{
$this->assertSame(self::APPLICATION_KEY, $this->config->getApplicationKey());
$this->assertSame('metric', $this->config->getMeasurementSystem());
$this->assertSame('metric', $this->config->getUnitSystem());
$this->assertSame('en', $this->config->getLanguage());
$this->assertInstanceOf(HttpClientBuilder::class, $this->config->getHttpClientBuilder());
$this->assertSame(null, $this->config->getCache());
Expand All @@ -42,15 +42,15 @@ public function testConfigWithOptions()
{
$config = new Config([
'applicationKey' => 'newtestappkey',
'measurementSystem' => 'imperial',
'unitSystem' => 'imperial',
'language' => 'pt',
'httpClientBuilder' => new HttpClientBuilder(),
'cache' => new FilesystemAdapter(),
'logger' => new Logger('test')
]);

$this->assertSame('newtestappkey', $config->getApplicationKey());
$this->assertSame('imperial', $config->getMeasurementSystem());
$this->assertSame('imperial', $config->getUnitSystem());
$this->assertSame('pt', $config->getLanguage());
$this->assertInstanceOf(HttpClientBuilder::class, $config->getHttpClientBuilder());
$this->assertInstanceOf(CacheItemPoolInterface::class, $config->getCache());
Expand All @@ -77,10 +77,10 @@ public static function provideInvalidConfigOptionsData(): \Generator
],
InvalidOptionsException::class
];
yield 'invalid measurement system' => [
yield 'invalid unit system' => [
[
'applicationKey' => self::APPLICATION_KEY,
'measurementSystem' => 'invalid'
'unitSystem' => 'invalid'
],
InvalidOptionsException::class
];
Expand All @@ -105,17 +105,17 @@ public function testConfigSetApplicationKeyWithBlankValue()
$this->config->setApplicationKey('');
}

public function testConfigSetMeasurementSystem()
public function testConfigSetUnitSystem()
{
$this->config->setMeasurementSystem('imperial');
$this->assertSame('imperial', $this->config->getMeasurementSystem());
$this->config->setUnitSystem('imperial');
$this->assertSame('imperial', $this->config->getUnitSystem());
}

#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidMeasurementSystemData')]
public function testConfigSetMeasurementSystemWithInvalidValue(string $measurementSystem, string $expectedException)
#[DataProviderExternal(InvalidParamDataProvider::class, 'provideInvalidUnitSystemData')]
public function testConfigSetUnitSystemWithInvalidValue(string $unitSystem, string $expectedException)
{
$this->expectException($expectedException);
$this->config->setMeasurementSystem($measurementSystem);
$this->config->setUnitSystem($unitSystem);
}

public function testConfigSetLanguage()
Expand Down
4 changes: 2 additions & 2 deletions tests/DataProvider/InvalidParamDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static function provideInvalidNumResultsData(): \Generator
yield 'negative num results' => [-1, ValidationException::class];
}

public static function provideInvalidMeasurementSystemData(): \Generator
public static function provideInvalidUnitSystemData(): \Generator
{
yield 'not allowed measurement system' => ['invalid', ValidationException::class];
yield 'not allowed unit system' => ['invalid', ValidationException::class];
}

public static function provideInvalidLanguageData(): \Generator
Expand Down
2 changes: 1 addition & 1 deletion tests/OneCallEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function testOneCallMethodsWithExist()
$weatherEndpoint = $this->getApi()->getWeather();

$this->assertSame(true, method_exists($weatherEndpoint, 'withLanguage'));
$this->assertSame(true, method_exists($weatherEndpoint, 'withMeasurementSystem'));
$this->assertSame(true, method_exists($weatherEndpoint, 'withUnitSystem'));
}

private function assertWeatherResponse(OneCall $response): void
Expand Down
2 changes: 1 addition & 1 deletion tests/WeatherEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testWeatherMethodsWithExist()
$weatherEndpoint = $this->getApi()->getWeather();

$this->assertSame(true, method_exists($weatherEndpoint, 'withLanguage'));
$this->assertSame(true, method_exists($weatherEndpoint, 'withMeasurementSystem'));
$this->assertSame(true, method_exists($weatherEndpoint, 'withUnitSystem'));
}

private function assertCurrentResponse(CurrentWeather $response): void
Expand Down
31 changes: 0 additions & 31 deletions tests/WithMeasurementSystemTest.php

This file was deleted.

Loading