diff --git a/src/Endpoints/Endpoints.php b/src/Endpoints/Endpoints.php index 23f8de5..94337c7 100644 --- a/src/Endpoints/Endpoints.php +++ b/src/Endpoints/Endpoints.php @@ -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, + ]; + } } diff --git a/src/LiquipediaBuilder.php b/src/LiquipediaBuilder.php index ecff288..bac4de4 100644 --- a/src/LiquipediaBuilder.php +++ b/src/LiquipediaBuilder.php @@ -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 = [], @@ -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. */ diff --git a/tests/LiquipediaBuilderTest.php b/tests/LiquipediaBuilderTest.php index e0b5bad..742bd8a 100644 --- a/tests/LiquipediaBuilderTest.php +++ b/tests/LiquipediaBuilderTest.php @@ -1,5 +1,6 @@ 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); + +});