Skip to content

Commit

Permalink
Merge pull request #8 from LogansUA/feature-options-resolving
Browse files Browse the repository at this point in the history
Feature options resolving
  • Loading branch information
LogansUA committed Dec 28, 2015
2 parents 42a0249 + fc904f2 commit f252f0c
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 41 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
58 changes: 56 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/CommunityOAuthExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/GameDataExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion examples/StarcraftExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion examples/WorldOfWarcraftExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
98 changes: 72 additions & 26 deletions src/BlizzardClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace BlizzardApi;

use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Class Blizzard Client
*
Expand Down Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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']);
}
}
41 changes: 41 additions & 0 deletions src/GeoData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace BlizzardApi;

/**
* Class GeoData
*
* @author Oleg Kachinsky <[email protected]>
*/
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',
],
];
}
10 changes: 3 additions & 7 deletions src/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f252f0c

Please sign in to comment.