Skip to content

Commit

Permalink
StaticData linking fixes + tests (#26)
Browse files Browse the repository at this point in the history
[LeagueAPI] StaticData linking fixes + tests
  • Loading branch information
dolejska-daniel committed Dec 13, 2018
1 parent 3dd3294 commit 205d6d7
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 36 deletions.
59 changes: 42 additions & 17 deletions src/RiotAPI/Objects/ApiObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct( array $data, RiotAPI $api = null )
: false;
$linkableProp = $selfRef->hasProperty('staticData')
? self::getLinkablePropertyData($selfRef->getDocComment())
: [ 'name' => false, 'function' => false];
: [ 'function' => false, 'parameter' => false ];

foreach ($data as $property => $value)
{
Expand Down Expand Up @@ -88,20 +88,42 @@ public function __construct( array $data, RiotAPI $api = null )
if ($api)
{
// Should this property be linked and is it allowed?
if ($linkableProp['name'] == $property && $api->getSetting(RiotAPI::SET_STATICDATA_LINKING, false))
if ($linkableProp['parameter'] == $property && $api->getSetting(RiotAPI::SET_STATICDATA_LINKING, false))
{
$params = [];
// Request locale
$params[] = $api->getSetting(RiotAPI::SET_STATICDATA_LOCALE, null);
// Static data version
$params[] = $api->getSetting(RiotAPI::SET_STATICDATA_VERSION, null);
// Data by ID?
$params[] = true;
// Included tags
$params[] = $api->getSetting(RiotAPI::SET_STATICDATA_TAGS, 'info');

$data = call_user_func_array(array($api, $linkableProp['function']), $params);
$this->staticData = $data->data[$value];
$apiRef = new \ReflectionClass(RiotAPI::class);
$linkingFunctionRef = $apiRef->getMethod($linkableProp['function']);

$params = [ $value ];
foreach ($linkingFunctionRef->getParameters() as $parameter)
{
switch ($parameter->getName())
{
// Extended data fetch?
case "extended":
$params[] = true;
break;

// Data by key?
case "data_by_key":
$params[] = true;
break;

// Request locale
case "locale":
$params[] = $api->getSetting(RiotAPI::SET_STATICDATA_LOCALE, $parameter->getDefaultValue());
break;

// Static data version
case "version":
$params[] = $api->getSetting(RiotAPI::SET_STATICDATA_VERSION, $parameter->getDefaultValue());
break;

default:
break;
}
}

$this->staticData = $linkingFunctionRef->invokeArgs($api, $params);
}
}
}
Expand Down Expand Up @@ -150,9 +172,12 @@ public static function getIterablePropertyName( string $phpDocComment )
*/
public static function getLinkablePropertyData( string $phpDocComment )
{
preg_match('/@linkable\s\$([\w]+)\s\(([\w\$]+)\)/', $phpDocComment, $matches);
if (isset($matches[1]) && isset($matches[2]))
return [ 'name' => $matches[1], 'function' => $matches[2]];
preg_match('/@linkable\s(?<function>[\w]+)(?:\(\$(?<parameter>[\w]+)+?\))?/', $phpDocComment, $matches);

// Filter only named capture groups
$matches = array_filter($matches, function ($v, $k) { return is_string($k); }, ARRAY_FILTER_USE_BOTH);
if (@$matches['function'] && @$matches['parameter'])
return $matches;

return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/BannedChampion.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @link https://developer.riotgames.com/api-methods/#spectator-v4/GET_getCurrentGameInfoBySummoner
* @link https://developer.riotgames.com/api-methods/#spectator-v4/GET_getFeaturedGames
*
* @linkable $championId (getStaticChampions)
* @linkable getStaticChampion($championId)
*
* @package RiotAPI\Objects
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/ChampionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* @link https://developer.riotgames.com/api-methods/#champion-v3/GET_getChampions
* @link https://developer.riotgames.com/api-methods/#champion-v3/GET_getChampionsById
*
* @linkable $id (getStaticChampions)
* @linkable getStaticChampion($id)
*
* @package RiotAPI\Objects
*/
Expand Down
7 changes: 1 addition & 6 deletions src/RiotAPI/Objects/ChampionMasteryDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
* Class ChampionMasteryDto
* This object contains single Champion Mastery information for player and champion combination.
*
* Used in:
* champion-mastery (v4)
* @link https://developer.riotgames.com/api-methods/#champion-mastery-v4/GET_getAllChampionMasteries
* @link https://developer.riotgames.com/api-methods/#champion-mastery-v4/GET_getChampionMastery
*
* @linkable $championId (getStaticChampions)
* @linkable getStaticChampion($championId)
*
* @package RiotAPI\Objects
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/CurrentGameParticipant.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* spectator (v4)
* @link https://developer.riotgames.com/api-methods/#spectator-v4/GET_getCurrentGameInfoBySummoner
*
* @linkable $championId (getStaticChampions)
* @linkable getStaticChampion($championId)
*
* @package RiotAPI\Objects
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/MatchReferenceDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* match (v4)
* @link https://developer.riotgames.com/api-methods/#match-v4/GET_getMatchlist
*
* @linkable $champion (getStaticChampions)
* @linkable getStaticChampion($champion)
*
* @package RiotAPI\Objects
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* spectator (v4)
* @link https://developer.riotgames.com/api-methods/#spectator-v4/GET_getFeaturedGames
*
* @linkable $championId (getStaticChampions)
* @linkable getStaticChampion($championId)
*
* @package RiotAPI\Objects
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/ParticipantDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @link https://developer.riotgames.com/api-methods/#match-v4/GET_getMatchIdsByTournamentCode
* @link https://developer.riotgames.com/api-methods/#match-v4/GET_getMatchByTournamentCode
*
* @linkable $championId (getStaticChampions)
* @linkable getStaticChampion($championId)
*
* @package RiotAPI\Objects
*/
Expand Down
2 changes: 1 addition & 1 deletion src/RiotAPI/Objects/TeamBansDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @link https://developer.riotgames.com/api-methods/#match-v4/GET_getMatchIdsByTournamentCode
* @link https://developer.riotgames.com/api-methods/#match-v4/GET_getMatchByTournamentCode
*
* @linkable $championId (getStaticChampions)
* @linkable getStaticChampion($championId)
*
* @package RiotAPI\Objects
*/
Expand Down
14 changes: 8 additions & 6 deletions tests/RiotAPI/EndpointObjectIntegrity/ApiObjectLinkableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@

use RiotAPI\Definitions\Region;
use RiotAPI\Objects\ApiObjectLinkable;
use RiotAPI\Objects\ChampionMasteryDto;
use RiotAPI\RiotAPI;


class ApiObjectLinkableTest extends RiotAPITestCase
{
public function testInit()
{
$this->markTestSkipped("Static-data API endpoint has been deprecated.");

$api = new RiotAPI([
RiotAPI::SET_KEY => getenv('API_KEY'),
RiotAPI::SET_REGION => Region::EUROPE_EAST,
RiotAPI::SET_USE_DUMMY_DATA => true,
RiotAPI::SET_DATADRAGON_INIT => true,
RiotAPI::SET_STATICDATA_LINKING => true,
RiotAPI::SET_CACHE_CALLS => true,
]);
Expand All @@ -48,14 +48,16 @@ public function testInit()
*
* @param RiotAPI $api
*/
public function testStaticDataLinking( RiotAPI $api )
public function testStaticDataLinking_ChampionMastery( RiotAPI $api )
{
$data = $api->getChampionMastery(30904166, 61);
/** @var ChampionMasteryDto $data */
$data = $api->getChampionMastery("KnNZNuEVZ5rZry3IyWwYSVuikRe0y3qTWSkr1wxcmV5CLJ8", 61);

$this->assertSame(61, $data->championId);
$this->assertSame($data->championId, $data->id);
$this->assertSame("61", $data->key);
$this->assertSame($data->key, $data->staticData->key);
$this->assertSame("Orianna", $data->name);
$this->assertSame("Orianna", $data->staticData->name);
$this->assertSame($data->name, $data->staticData->name);
}

public function testInvalidLinkableProperty()
Expand Down

0 comments on commit 205d6d7

Please sign in to comment.