diff --git a/composer.json b/composer.json index 281988a..f807ed2 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ ], "require": { "php": ">=5.6.0", - "guzzlehttp/guzzle": "^6.1@dev" + "guzzlehttp/guzzle": "^6.1@dev", + "symfony/options-resolver": "^3.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 83dfb3e..b6ca2ce 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "b0366ae644b75b7ddb691a641cc07ab0", - "content-hash": "f61c166195a878d58003a84e712c6ee3", + "hash": "14e3991ec20be5083ac1e0fa4b0a9f8e", + "content-hash": "f7f314893315dc1db3f1662c408099df", "packages": [ { "name": "guzzlehttp/guzzle", @@ -226,6 +226,60 @@ "response" ], "time": "2015-05-04 20:22:00" + }, + { + "name": "symfony/options-resolver", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/symfony/options-resolver.git", + "reference": "0fb705c94d78e3f6b151f8e0b68c4bae89a13c51" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/options-resolver/zipball/0fb705c94d78e3f6b151f8e0b68c4bae89a13c51", + "reference": "0fb705c94d78e3f6b151f8e0b68c4bae89a13c51", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\OptionsResolver\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony OptionsResolver Component", + "homepage": "https://symfony.com", + "keywords": [ + "config", + "configuration", + "options" + ], + "time": "2015-11-30 21:39:17" } ], "packages-dev": [], diff --git a/examples/CommunityOAuthExample.php b/examples/CommunityOAuthExample.php index 4dd4794..597b3d9 100644 --- a/examples/CommunityOAuthExample.php +++ b/examples/CommunityOAuthExample.php @@ -4,10 +4,10 @@ require_once __DIR__.'/../vendor/autoload.php'; // Create a new Blizzard client with Blizzard API key -$client = new \BlizzardApi\BlizzardClient('3hnw3qmca6bnc3x4japefbzhcd86smpr', 'vradys5bhcbe4c3cbv43285e', 'ru_RU', 'EU'); +$client = new \BlizzardApi\BlizzardClient('apiKey'); // Create a new Diablo service with configured Blizzard client -$diablo = new \BlizzardApi\Service\Starcraft($client); +$diablo = new \BlizzardApi\Service\Starcraft($client->setAccessToken('accessToken')); // Use API method for getting specific data $response = $diablo->getProfileUser(); diff --git a/examples/GameDataExample.php b/examples/GameDataExample.php index 9fcd94f..2ddc8b0 100644 --- a/examples/GameDataExample.php +++ b/examples/GameDataExample.php @@ -7,7 +7,7 @@ $client = new \BlizzardApi\BlizzardClient('apiKey'); // Create a new GameData service with configured Blizzard client -$gameData = new \BlizzardApi\Service\GameData($client); +$gameData = new \BlizzardApi\Service\GameData($client->setAccessToken('accessToken')); // Use API method for getting specific data $response = $gameData->getEraLeaderboard(1, 'rift-barbarian'); diff --git a/examples/StarcraftExample.php b/examples/StarcraftExample.php index eb89717..576e57f 100644 --- a/examples/StarcraftExample.php +++ b/examples/StarcraftExample.php @@ -7,7 +7,7 @@ $client = new \BlizzardApi\BlizzardClient('apiKey'); // Create a new Starcraft service with configured Blizzard client -$starcraft = new \BlizzardApi\Service\Starcraft($client); +$starcraft = new \BlizzardApi\Service\Starcraft($client->setAccessToken('accessToken')); // Use API method for getting specific data $response = $starcraft->getAchievements(); diff --git a/examples/WorldOfWarcraftExample.php b/examples/WorldOfWarcraftExample.php index 9c205a2..0182501 100644 --- a/examples/WorldOfWarcraftExample.php +++ b/examples/WorldOfWarcraftExample.php @@ -7,7 +7,7 @@ $client = new \BlizzardApi\BlizzardClient('apiKey'); // Create a new World Of Warcraft service with configured Blizzard client -$wow = new \BlizzardApi\Service\WorldOfWarcraft($client); +$wow = new \BlizzardApi\Service\WorldOfWarcraft($client->setAccessToken('accessToken')); // Use API method for getting specific data $response = $wow->getGuild('test-realm', 'test-guild', [ diff --git a/src/BlizzardClient.php b/src/BlizzardClient.php index f74fc9d..96d8d4e 100644 --- a/src/BlizzardClient.php +++ b/src/BlizzardClient.php @@ -2,6 +2,9 @@ namespace BlizzardApi; +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; +use Symfony\Component\OptionsResolver\OptionsResolver; + /** * Class Blizzard Client * @@ -40,18 +43,30 @@ class BlizzardClient * BlizzardClient constructor * * @param string $apiKey API key - * @param null|string $accessToken OAuth access token - * @param string $locale Locale * @param string $region Region + * @param string $locale Locale + * @param null|string $accessToken OAuth access token */ - public function __construct($apiKey, $accessToken = null, $locale = 'en_us', $region = 'us') + public function __construct($apiKey, $region = 'us', $locale = 'en_us', $accessToken = null) { - $this->apiKey = $apiKey; - $this->accessToken = $accessToken; - $this->locale = strtolower($locale); - $this->region = strtolower($region); + $options = [ + 'apiKey' => $apiKey, + 'region' => strtolower($region), + 'locale' => strtolower($locale), + 'accessToken' => $accessToken, + ]; - $this->updateApiUrl($region); + $resolver = (new OptionsResolver()); + $this->configureOptions($resolver, $options['region']); + + $options = $resolver->resolve($options); + + $this->apiKey = $options['apiKey']; + $this->region = $options['region']; + $this->locale = $options['locale']; + $this->accessToken = $options['accessToken']; + + $this->updateApiUrl($options['region']); } /** @@ -89,25 +104,27 @@ public function setApiKey($apiKey) } /** - * Get access token + * Get region * - * @return null|string Access token + * @return string Region */ - public function getAccessToken() + public function getRegion() { - return $this->accessToken; + return strtolower($this->region); } /** - * Set access token + * Set region * - * @param null|string $accessToken Access token + * @param string $region region * * @return $this */ - public function setAccessToken($accessToken) + public function setRegion($region) { - $this->accessToken = $accessToken; + $this->region = strtolower($region); + + $this->updateApiUrl($region); return $this; } @@ -137,27 +154,25 @@ public function setLocale($locale) } /** - * Get region + * Get access token * - * @return string Region + * @return null|string Access token */ - public function getRegion() + public function getAccessToken() { - return strtolower($this->region); + return $this->accessToken; } /** - * Set region + * Set access token * - * @param string $region region + * @param null|string $accessToken Access token * * @return $this */ - public function setRegion($region) + public function setAccessToken($accessToken) { - $this->region = strtolower($region); - - $this->updateApiUrl($region); + $this->accessToken = $accessToken; return $this; } @@ -175,4 +190,35 @@ private function updateApiUrl($region) { $this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN); } + + /** + * Configure options + * + * @param OptionsResolver $resolver Symfony options resolver + * @param string $region Region + * + * @throws InvalidOptionsException + */ + private function configureOptions(OptionsResolver $resolver, $region) + { + if (isset(GeoData::$list[$region])) { + $locales = GeoData::$list[$region]; + } else { + throw new InvalidOptionsException( + sprintf( + 'The option "region" with value "%s" is invalid. Accepted values are: "%s".', + $region, + implode('", "', array_keys(GeoData::$list)) + ) + ); + } + + $resolver->setRequired(['apiKey', 'region', 'locale', 'accessToken']) + ->setAllowedTypes('apiKey', 'string') + ->setAllowedTypes('region', 'string') + ->setAllowedValues('region', array_keys(GeoData::$list)) + ->setAllowedTypes('locale', 'string') + ->setAllowedValues('locale', $locales) + ->setAllowedTypes('accessToken', ['null', 'string']); + } } diff --git a/src/GeoData.php b/src/GeoData.php new file mode 100644 index 0000000..8ced2d1 --- /dev/null +++ b/src/GeoData.php @@ -0,0 +1,41 @@ + + */ +class GeoData +{ + /** + * @var array $list List of available regions and locales + */ + static public $list = [ + 'eu' => [ + 'en_gb', + 'de_de', + 'es_es', + 'fr_fr', + 'it_it', + 'pl_pl', + 'pt_pt', + 'ru_ru', + ], + 'us' => [ + 'en_us', + 'pt_br', + 'es_mx', + ], + 'kr' => [ + 'ko_kr', + ], + 'tw' => [ + 'zh_tw', + ], + 'sea' => [ + 'en_us', + ], + ]; +} diff --git a/src/Service/Service.php b/src/Service/Service.php index 6224c52..49fb53b 100644 --- a/src/Service/Service.php +++ b/src/Service/Service.php @@ -71,14 +71,10 @@ protected function request($urlSuffix, array $options) */ private function generateQueryOptions(array $options = []) { - $result = []; - - $defaultOption = $this->getDefaultOptions(); - - if (isset($options)) { - $result['query'] = $options + $defaultOption; + if (isset($options['query'])) { + $result = $options['query'] + $this->getDefaultOptions(); } else { - $result['query'] = $defaultOption; + $result['query'] = $options + $this->getDefaultOptions(); } return $result;