Skip to content

Commit b3e5f68

Browse files
committed
feat: added Language and UnitSystem traits
1 parent b39dadb commit b3e5f68

10 files changed

+77
-63
lines changed

src/Endpoint/Util/LanguageTrait.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Endpoint/Util/UnitSystemTrait.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Language/Language.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Language
5555
public const VIETNAMESE = 'vi';
5656
public const ZULU = 'zu';
5757

58-
public static function getList(): array
58+
public static function getOptions(): array
5959
{
6060
return (new Language)->getClassConstants(self::class);
6161
}

src/OpenWeatherMap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ private function configureOptions(array $options): array
6464
$this->optionsResolver->setAllowedTypes('unitSystem', 'string');
6565
$this->optionsResolver->setAllowedTypes('language', 'string');
6666

67-
$this->optionsResolver->setAllowedValues('unitSystem', UnitSystem::getList());
68-
$this->optionsResolver->setAllowedValues('language', Language::getList());
67+
$this->optionsResolver->setAllowedValues('unitSystem', UnitSystem::getOptions());
68+
$this->optionsResolver->setAllowedValues('language', Language::getOptions());
6969

7070
return $this->optionsResolver->resolve($options);
7171
}

src/Resource/GeocodingResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getByLocationName(string $locationName, int $numResults = self::
4646
public function getByZipCode(string $zipCode, string $countryCode): ZipLocation
4747
{
4848
$this->validateQuery($zipCode, 'zipCode');
49-
$this->validateCountry($countryCode, 'countryCode');
49+
$this->validateCountryCode($countryCode);
5050

5151
$data = $this->api->request(
5252
method: 'GET',

src/Resource/Util/LanguageTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;
4+
5+
use ProgrammatorDev\Validator\Exception\ValidationException;
6+
use function DeepCopy\deep_copy;
7+
8+
trait LanguageTrait
9+
{
10+
use ValidationTrait;
11+
12+
/**
13+
* @throws ValidationException
14+
*/
15+
public function withLanguage(string $language): static
16+
{
17+
$this->validateLanguage($language);
18+
19+
$clone = deep_copy($this);
20+
$clone->api->addQueryDefault('lang', $language);
21+
22+
return $clone;
23+
}
24+
}

src/Resource/Util/UnitSystemTrait.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;
4+
5+
use ProgrammatorDev\Validator\Exception\ValidationException;
6+
use function DeepCopy\deep_copy;
7+
8+
trait UnitSystemTrait
9+
{
10+
use ValidationTrait;
11+
12+
/**
13+
* @throws ValidationException
14+
*/
15+
public function withUnitSystem(string $unitSystem): static
16+
{
17+
$this->validateUnitSystem($unitSystem);
18+
19+
$clone = deep_copy($this);
20+
$clone->api->addQueryDefault('units', $unitSystem);
21+
22+
return $clone;
23+
}
24+
}

src/Resource/Util/ValidationTrait.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;
44

5+
use ProgrammatorDev\OpenWeatherMap\Language\Language;
6+
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
57
use ProgrammatorDev\Validator\Exception\ValidationException;
68
use ProgrammatorDev\Validator\Validator;
79

@@ -35,8 +37,24 @@ private function validateCoordinate(float $latitude, float $longitude): void
3537
/**
3638
* @throws ValidationException
3739
*/
38-
private function validateCountry(string $countryCode, string $name): void
40+
private function validateCountryCode(string $countryCode): void
3941
{
40-
Validator::country()->assert($countryCode, $name);
42+
Validator::country()->assert($countryCode, 'countryCode');
43+
}
44+
45+
/**
46+
* @throws ValidationException
47+
*/
48+
private function validateLanguage(string $language): void
49+
{
50+
Validator::choice(Language::getOptions())->assert($language, 'language');
51+
}
52+
53+
/**
54+
* @throws ValidationException
55+
*/
56+
private function validateUnitSystem(string $unitSystem): void
57+
{
58+
Validator::choice(UnitSystem::getOptions())->assert($unitSystem, 'unitSystem');
4159
}
4260
}

src/Resource/WeatherResource.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
use ProgrammatorDev\Api\Method;
66
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\Weather;
77
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherCollection;
8+
use ProgrammatorDev\OpenWeatherMap\Resource\Util\LanguageTrait;
9+
use ProgrammatorDev\OpenWeatherMap\Resource\Util\UnitSystemTrait;
810
use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait;
911
use ProgrammatorDev\Validator\Exception\ValidationException;
1012
use Psr\Http\Client\ClientExceptionInterface;
1113

1214
class WeatherResource extends Resource
1315
{
16+
use LanguageTrait;
17+
use UnitSystemTrait;
1418
use ValidationTrait;
1519

1620
private const NUM_RESULTS = 40;

src/UnitSystem/UnitSystem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class UnitSystem
1212
public const IMPERIAL = 'imperial';
1313
public const STANDARD = 'standard';
1414

15-
public static function getList(): array
15+
public static function getOptions(): array
1616
{
1717
return (new UnitSystem)->getClassConstants(self::class);
1818
}

0 commit comments

Comments
 (0)