Skip to content

Commit

Permalink
feat: added Language and UnitSystem traits
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepimpao committed May 9, 2024
1 parent b39dadb commit b3e5f68
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 63 deletions.
28 changes: 0 additions & 28 deletions src/Endpoint/Util/LanguageTrait.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Endpoint/Util/UnitSystemTrait.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Language
public const VIETNAMESE = 'vi';
public const ZULU = 'zu';

public static function getList(): array
public static function getOptions(): array
{
return (new Language)->getClassConstants(self::class);
}
Expand Down
4 changes: 2 additions & 2 deletions src/OpenWeatherMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private function configureOptions(array $options): array
$this->optionsResolver->setAllowedTypes('unitSystem', 'string');
$this->optionsResolver->setAllowedTypes('language', 'string');

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

return $this->optionsResolver->resolve($options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Resource/GeocodingResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getByLocationName(string $locationName, int $numResults = self::
public function getByZipCode(string $zipCode, string $countryCode): ZipLocation
{
$this->validateQuery($zipCode, 'zipCode');
$this->validateCountry($countryCode, 'countryCode');
$this->validateCountryCode($countryCode);

$data = $this->api->request(
method: 'GET',
Expand Down
24 changes: 24 additions & 0 deletions src/Resource/Util/LanguageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;

use ProgrammatorDev\Validator\Exception\ValidationException;
use function DeepCopy\deep_copy;

trait LanguageTrait
{
use ValidationTrait;

/**
* @throws ValidationException
*/
public function withLanguage(string $language): static
{
$this->validateLanguage($language);

$clone = deep_copy($this);
$clone->api->addQueryDefault('lang', $language);

return $clone;
}
}
24 changes: 24 additions & 0 deletions src/Resource/Util/UnitSystemTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;

use ProgrammatorDev\Validator\Exception\ValidationException;
use function DeepCopy\deep_copy;

trait UnitSystemTrait
{
use ValidationTrait;

/**
* @throws ValidationException
*/
public function withUnitSystem(string $unitSystem): static
{
$this->validateUnitSystem($unitSystem);

$clone = deep_copy($this);
$clone->api->addQueryDefault('units', $unitSystem);

return $clone;
}
}
22 changes: 20 additions & 2 deletions src/Resource/Util/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;

use ProgrammatorDev\OpenWeatherMap\Language\Language;
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
use ProgrammatorDev\Validator\Exception\ValidationException;
use ProgrammatorDev\Validator\Validator;

Expand Down Expand Up @@ -35,8 +37,24 @@ private function validateCoordinate(float $latitude, float $longitude): void
/**
* @throws ValidationException
*/
private function validateCountry(string $countryCode, string $name): void
private function validateCountryCode(string $countryCode): void
{
Validator::country()->assert($countryCode, $name);
Validator::country()->assert($countryCode, 'countryCode');
}

/**
* @throws ValidationException
*/
private function validateLanguage(string $language): void
{
Validator::choice(Language::getOptions())->assert($language, 'language');
}

/**
* @throws ValidationException
*/
private function validateUnitSystem(string $unitSystem): void
{
Validator::choice(UnitSystem::getOptions())->assert($unitSystem, 'unitSystem');
}
}
4 changes: 4 additions & 0 deletions src/Resource/WeatherResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
use ProgrammatorDev\Api\Method;
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\Weather;
use ProgrammatorDev\OpenWeatherMap\Entity\Weather\WeatherCollection;
use ProgrammatorDev\OpenWeatherMap\Resource\Util\LanguageTrait;
use ProgrammatorDev\OpenWeatherMap\Resource\Util\UnitSystemTrait;
use ProgrammatorDev\OpenWeatherMap\Resource\Util\ValidationTrait;
use ProgrammatorDev\Validator\Exception\ValidationException;
use Psr\Http\Client\ClientExceptionInterface;

class WeatherResource extends Resource
{
use LanguageTrait;
use UnitSystemTrait;
use ValidationTrait;

private const NUM_RESULTS = 40;
Expand Down
2 changes: 1 addition & 1 deletion src/UnitSystem/UnitSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UnitSystem
public const IMPERIAL = 'imperial';
public const STANDARD = 'standard';

public static function getList(): array
public static function getOptions(): array
{
return (new UnitSystem)->getClassConstants(self::class);
}
Expand Down

0 comments on commit b3e5f68

Please sign in to comment.