Skip to content

Commit

Permalink
feat(Endpoints): add method to check if endpoint is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 23, 2024
1 parent 95ce106 commit a89ad95
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 18 deletions.
84 changes: 67 additions & 17 deletions src/Endpoints/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,72 @@

namespace Npldevfr\Liquipedia\Endpoints;

enum Endpoints: string
final class Endpoints
{
case BROADCASTERS = '/v3/broadcasters';
case COMPANIES = '/v3/company';
case DATAPOINTS = '/v3/datapoint';
case EXTERNAL_MEDIA_LINKS = '/v3/externalmedialink';
case MATCHES = '/v3/match';
case PLACEMENTS = '/v3/placement';
case PLAYERS = '/v3/player';
case SERIES = '/v3/series';
case SQUAD_PLAYERS = '/v3/squadplayer';
case STANDINGS_ENTRY = '/v3/standingsentry';
case STANDINGS_TABLE = '/v3/standingstable';
case TEAMS = '/v3/team';
case TOURNAMENTS = '/v3/tournament';
case TRANSFERS = '/v3/transfer';
case TEAM_TEMPLATES = '/v3/teamtemplate';
case TEAM_TEMPLATE_LIST = '/v3/teamtemplatelist';
final public const BROADCASTERS = 'broadcasters';

final public const COMPANIES = 'company';

final public const DATAPOINTS = 'datapoint';

final public const EXTERNAL_MEDIA_LINKS = 'externalmedialink';

final public const MATCHES = 'match';

final public const PLACEMENTS = 'placement';

final public const PLAYERS = 'player';

final public const SERIES = 'series';

final public const SQUAD_PLAYERS = 'squadplayer';

final public const STANDINGS_ENTRY = 'standingsentry';

final public const STANDINGS_TABLE = 'standingstable';

final public const TEAMS = 'team';

final public const TOURNAMENTS = 'tournament';

final public const TRANSFERS = 'transfer';

final public const TEAM_TEMPLATES = 'teamtemplate';

final public const TEAM_TEMPLATE_LIST = 'teamtemplatelist';

/**
* Check if the endpoint is valid.
*/
public static function fromArray(string $endpoint): bool
{
return in_array($endpoint, self::all());
}

/**
* Get all the endpoints.
*
* @return string[]
*/
public static function all(): array
{
return [
self::BROADCASTERS,
self::COMPANIES,
self::DATAPOINTS,
self::EXTERNAL_MEDIA_LINKS,
self::MATCHES,
self::PLACEMENTS,
self::PLAYERS,
self::SERIES,
self::SQUAD_PLAYERS,
self::STANDINGS_ENTRY,
self::STANDINGS_TABLE,
self::TEAMS,
self::TOURNAMENTS,
self::TRANSFERS,
self::TEAM_TEMPLATES,
self::TEAM_TEMPLATE_LIST,
];
}
}
29 changes: 28 additions & 1 deletion src/LiquipediaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Npldevfr\Liquipedia;

use Exception;
use GuzzleHttp\Client;
use Npldevfr\Liquipedia\Endpoints\Endpoints;
use Npldevfr\Liquipedia\Query\QueryBuilder;
use Npldevfr\Liquipedia\Query\QueryParameters;

final class LiquipediaBuilder extends QueryBuilder
{
// private string $endpoint;
private string $endpoint;

public function __construct(
?array $params = [],
Expand Down Expand Up @@ -87,6 +89,31 @@ public function offset(int $offset): self
return $this;
}

/**
* Set the endpoint you want to query.
*
* @return $this
*
* @throws Exception
*/
public function endpoint(string $endpoint): self
{
if (! Endpoints::fromArray($endpoint)) {
throw new Exception('[LiquipediaBuilder] Endpoint '.$endpoint.' is not valid.');
}
$this->endpoint = $endpoint;

return $this;
}

/**
* Get the endpoint you want to query.
*/
public function getEndpoint(): string
{
return $this->endpoint;
}

/**
* Filter the wikis and remove the duplicates.
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/LiquipediaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Npldevfr\Liquipedia\Endpoints\Endpoints;
use Npldevfr\Liquipedia\LiquipediaBuilder;
use Npldevfr\Liquipedia\Query\QueryParameters;
use Npldevfr\Liquipedia\Wikis\Wikis;
Expand Down Expand Up @@ -140,3 +141,24 @@
'offset' => 1,
]);
});

it('can set an endpoint', function () {
$builder = LiquipediaBuilder::query()
->endpoint(Endpoints::MATCHES);

expect($builder->getEndpoint())->toBe(Endpoints::MATCHES);
});

it('can set an endpoint and a wiki', function () {
$builder = LiquipediaBuilder::query()
->endpoint(Endpoints::MATCHES)
->wikis(Wikis::LEAGUE_OF_LEGENDS);

expect($builder->build())
->toBe([
'wiki' => 'leagueoflegends',
])
->and($builder->getEndpoint())
->toBe(Endpoints::MATCHES);

});

0 comments on commit a89ad95

Please sign in to comment.