diff --git a/src/Interfaces/LiquipediaBuilderInterface.php b/src/Interfaces/LiquipediaBuilderInterface.php new file mode 100644 index 0000000..7bf056a --- /dev/null +++ b/src/Interfaces/LiquipediaBuilderInterface.php @@ -0,0 +1,48 @@ + | string $wikis + */ + public function wikis(array|string $wikis): self; + + /** + * Set the endpoint you want to query. + */ + public function endpoint(string $endpoint): self; + + /** + * Set the conditions you want to query. + */ + public function rawConditions(string $conditions): self; + + /** + * Limit the number of results. + */ + public function limit(int $limit): self; + + /** + * @return array + */ + public function get(): array; + + /** + * Add a wiki to the wikis you want to query. + */ + public function addWiki(string $wikis): self; + + /** + * Set a result offset. + */ + public function offset(int $offset): self; + + /** + * Order the results. + */ + public function orderBy(string $orderBy): self; +} diff --git a/src/Liquipedia.php b/src/Liquipedia.php new file mode 100644 index 0000000..b2c061b --- /dev/null +++ b/src/Liquipedia.php @@ -0,0 +1,39 @@ +apiKey = $apiKey; + $this->builder = $builder ?? new LiquipediaBuilder( + params: [], + client: new Client([ + 'base_uri' => 'https://api.liquipedia.net/api/v3/', + 'headers' => [ + 'Authorization' => "Apikey {$this->apiKey}", + ], + ] + )); + + } + + public function query(): LiquipediaBuilder + { + return (new self($this->apiKey))->builder; + } +} diff --git a/src/LiquipediaBuilder.php b/src/LiquipediaBuilder.php index 6fc4385..52a1d08 100644 --- a/src/LiquipediaBuilder.php +++ b/src/LiquipediaBuilder.php @@ -4,13 +4,15 @@ use Exception; use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; +use JsonException; +use Npldevfr\Liquipedia\Interfaces\LiquipediaBuilderInterface; use Npldevfr\Liquipedia\Meta\Endpoint; -use Npldevfr\Liquipedia\Meta\Operator; use Npldevfr\Liquipedia\Meta\SortOrder; use Npldevfr\Liquipedia\Query\QueryBuilder; use Npldevfr\Liquipedia\Query\QueryParameters; -final class LiquipediaBuilder extends QueryBuilder +final class LiquipediaBuilder extends QueryBuilder implements LiquipediaBuilderInterface { private string $endpoint; @@ -19,20 +21,9 @@ final class LiquipediaBuilder extends QueryBuilder public function __construct( ?array $params = [], ?QueryParameters $queryParameters = null, - ?Client $client = null + ?Client $client = new Client() ) { - - parent::__construct($params, $queryParameters, $client ?? new Client([ - 'base_uri' => 'https://api.liquipedia.net/api/v3', - ])); - } - - /** - * @param array $params - */ - public static function query(array $params = [], ?QueryParameters $queryParameters = null): self - { - return new self($params, $queryParameters); + parent::__construct($params, $queryParameters, $client); } /** @@ -152,7 +143,7 @@ public function pagination(int|string $pagination): self * * @throws Exception */ - public function orderBy(string $field, string $direction = 'ASC'): self + public function orderBy(string $orderBy, string $direction = 'ASC'): self { $direction = strtoupper($direction); @@ -161,7 +152,7 @@ public function orderBy(string $field, string $direction = 'ASC'): self throw new Exception('[LiquipediaBuilder] Direction '.$direction.' is not valid.'); } - $this->queryParameters->order = "{$field} ".$direction; + $this->queryParameters->order = "{$orderBy} ".$direction; return $this; } @@ -260,109 +251,26 @@ private function avoidWikiDuplicates(): void ); } - // /** - // * @return $this - // */ - // public function rawConditions(string $conditions): self - // { - // $this->params['conditions'] = trim($this->params['conditions'].' '.$conditions); - // - // return $this; - // } - // - // /** - // * @return $this - // * - // * @throws Exception - // */ - // public function andCondition(string $key, string $operator, string $value): self - // { - // // operator are : :: (equals), ::! (not equals), ::< (lower than) or ::> (greater than). - // if (! in_array($operator, ['::', '::!', '::<', '::>'])) { - // throw new Exception('Operator must be ::, ::!, ::< or ::>'); - // } - // - // $this->params['conditions'] = trim($this->params['conditions'].' AND '."[[{$key}{$operator}{$value}]]"); - // - // return $this; - // } - // - // /** - // * @param array $values - // * @return $this - // * - // * @throws Exception - // */ - // public function andConditions(string $key, string $operator, array $values): self - // { - // // operator are : :: (equals), ::! (not equals), ::< (lower than) or ::> (greater than). - // if (! in_array($operator, ['::', '::!', '::<', '::>'])) { - // throw new Exception('Operator must be ::, ::!, ::< or ::>'); - // } - // - // $conditions = []; - // foreach ($values as $value) { - // $conditions[] = "[[{$key}{$operator}{$value}]]"; - // } - // - // $this->params['conditions'] = trim($this->params['conditions'].' '.implode(' AND ', $conditions)); - // - // return $this; - // } - // - // /** - // * @return $this - // * - // * @throws Exception - // */ - // public function orCondition(string $key, string $operator, string $value): self - // { - // // operator are : :: (equals), ::! (not equals), ::< (lower than) or ::> (greater than). - // if (! in_array($operator, ['::', '::!', '::<', '::>'])) { - // throw new Exception('Operator must be ::, ::!, ::< or ::>'); - // } - // - // $this->params['conditions'] = trim($this->params['conditions'].' OR '."[[{$key}{$operator}{$value}]]"); - // - // return $this; - // } - // - // /** - // * @param array $values - // * @return $this - // * - // * @throws Exception - // */ - // public function orConditions(string $key, string $operator, array $values): self - // { - // // operator are : :: (equals), ::! (not equals), ::< (lower than) or ::> (greater than). - // if (! in_array($operator, ['::', '::!', '::<', '::>'])) { - // throw new Exception('Operator must be ::, ::!, ::< or ::>'); - // } - // - // $conditions = []; - // foreach ($values as $value) { - // $conditions[] = "[[{$key}{$operator}{$value}]]"; - // } - // - // $this->params['conditions'] = trim($this->params['conditions'].' OR '.implode(' OR ', $conditions)); - // $this->params['conditions'] = trim(preg_replace('/^OR/', '', $this->params['conditions'])); - // - // return $this; - // } - // - // public function get(?string $endpoint = null): array - // { - // - // $customEndpoint = $endpoint ?? $this->endpoint; - // $response = json_decode($this->client->get($customEndpoint, [ - // 'query' => $this->params, - // ])->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); - // - // if (isset($response->error)) { - // throw new Exception($response->error); - // } - // - // return $response->result ?? []; - // } + /** + * @return array + * + * @throws JsonException | Exception | GuzzleException + */ + public function get(?string $endpoint = null): array + { + + $customEndpoint = $endpoint ?? $this->endpoint; + $response = json_decode($this->client->get($customEndpoint, [ + 'query' => $this->queryParameters->toArray(), + ])->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR); + + /** + * @var object $response + */ + if (isset($response->error)) { + throw new Exception($response->error); + } + + return $response->result ?? []; + } } diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 621954e..d0099ca 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -33,6 +33,7 @@ public function __construct( } $this->client = $client ?? new Client(); + } public function build(): array diff --git a/tests/LiquipediaBuilderTest.php b/tests/LiquipediaBuilderTest.php index e289f84..f631149 100644 --- a/tests/LiquipediaBuilderTest.php +++ b/tests/LiquipediaBuilderTest.php @@ -1,46 +1,13 @@ Wiki::LEAGUE_OF_LEGENDS, - ]); - - expect($builder->build())->toBe([ - 'wiki' => 'leagueoflegends', - ]); - -}); - -it('can build a query with a query parameter object', function () { - $builder = LiquipediaBuilder::query([], new QueryParameters([ - 'wiki' => Wiki::LEAGUE_OF_LEGENDS, - ])); - - expect($builder->build())->toBe([ - 'wiki' => 'leagueoflegends', - ]); -}); - -it('can build a query with a query parameter object and params', function () { - $builder = LiquipediaBuilder::query([ - 'wiki' => Wiki::LEAGUE_OF_LEGENDS, - ], new QueryParameters([ - 'limit' => 1, - ])); - - expect($builder->build())->toBe([ - 'limit' => 1, - ]); -}); it('can set one wiki', function () { - $builder = LiquipediaBuilder::query()->wikis(Wiki::LEAGUE_OF_LEGENDS); + $builder = (new LiquipediaBuilder())->wikis(Wiki::LEAGUE_OF_LEGENDS); expect($builder->build())->toBe([ 'wiki' => 'leagueoflegends', @@ -48,7 +15,7 @@ }); it('can set multiple wikis', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis([ Wiki::LEAGUE_OF_LEGENDS, Wiki::OVERWATCH, @@ -60,7 +27,7 @@ }); it('can set multiple wikis with duplicates', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis([ Wiki::LEAGUE_OF_LEGENDS, Wiki::LEAGUE_OF_LEGENDS, @@ -72,7 +39,7 @@ }); it('can set multiple wikis with duplicates and a string', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis([ Wiki::LEAGUE_OF_LEGENDS, 'overwatch', @@ -84,7 +51,7 @@ }); it('can add a wiki', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis(Wiki::LEAGUE_OF_LEGENDS) ->addWiki(Wiki::OVERWATCH); @@ -94,7 +61,7 @@ }); it('can add a wiki with duplicates', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis(Wiki::LEAGUE_OF_LEGENDS) ->addWiki(Wiki::LEAGUE_OF_LEGENDS); @@ -104,7 +71,7 @@ }); it('can add a wiki with duplicates and a string', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis(Wiki::LEAGUE_OF_LEGENDS) ->addWiki(Wiki::LEAGUE_OF_LEGENDS) ->addWiki(Wiki::OVERWATCH) @@ -116,7 +83,7 @@ }); it('can set a limit', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->limit(1); expect($builder->build())->toBe([ @@ -125,7 +92,7 @@ }); it('can set an offset', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->offset(1); expect($builder->build())->toBe([ @@ -134,7 +101,7 @@ }); it('can set a limit and an offset', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->limit(1) ->offset(1); @@ -145,7 +112,7 @@ }); it('can set an endpoint', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->endpoint(Endpoint::MATCHES); expect($builder->getEndpoint())->toBe(Endpoint::MATCHES); @@ -153,7 +120,7 @@ it('can set an endpoint and a wiki', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->endpoint(Endpoint::MATCHES) ->wikis(Wiki::LEAGUE_OF_LEGENDS); @@ -167,7 +134,7 @@ }); it('can select only some fields', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->select([ 'field1', 'field2', @@ -179,7 +146,7 @@ }); it('can select only some fields with a string', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->select('field1, field2,field3'); expect($builder->build())->toBe([ @@ -188,7 +155,7 @@ }); it('can select only some fields with a string and an array', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->select([ 'field1', 'field2', @@ -201,7 +168,7 @@ }); it('can select only some fields with a string and an array with duplicates', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->select([ 'field1', 'field2', @@ -215,7 +182,7 @@ }); it('can use pagination', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->pagination(1); expect($builder->build())->toBe([ @@ -225,7 +192,7 @@ }); it('can use pagination with a string', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->pagination('1'); expect($builder->build())->toBe([ @@ -235,7 +202,7 @@ }); it('can use pagination with a string and an int', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->pagination('1') ->pagination(2); @@ -246,7 +213,7 @@ }); it('can order by a field', function ($order) { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->orderBy('field1', $order); expect($builder->build())->toBe([ @@ -256,13 +223,13 @@ it('cannot order by a field with an invalid order', function () { expect( - fn () => LiquipediaBuilder::query() + fn () => (new LiquipediaBuilder()) ->orderBy('field1', 'invalid') )->toThrow(Exception::class); }); it('can order by a field with a string', function ($order) { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->orderBy('field1', $order) ->orderBy('field2', 'asc'); @@ -272,7 +239,7 @@ })->with(SortOrder::all()); it('can group by a field', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->groupBy('field1'); expect($builder->build())->toBe([ @@ -281,7 +248,7 @@ }); it('can group by a field with a string', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->groupBy('field1', 'DESC'); expect($builder->build())->toBe([ @@ -290,7 +257,7 @@ }); it('can group by a field with a string and an array', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->groupBy('field1', 'DESC') ->groupBy('field2', 'ASC'); @@ -300,7 +267,7 @@ }); it('can group by a field with a string and an array with duplicates', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->groupBy('field1', 'DESC') ->groupBy('field2', 'ASC') ->groupBy('field2', 'ASC'); @@ -311,7 +278,7 @@ }); it('can set template', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->template('template1'); expect($builder->build())->toBe([ @@ -320,7 +287,7 @@ }); it('can add date', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->date('2020-01-01'); expect($builder->build())->toBe([ @@ -330,13 +297,13 @@ it('cannot add date with wrong format', function () { expect( - fn () => LiquipediaBuilder::query() + fn () => (new LiquipediaBuilder()) ->date('2020-01-01 00:00:00') )->toThrow(Exception::class); }); it('can add date with a string', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->date('2020-01-01') ->date('2020-01-02'); @@ -346,7 +313,7 @@ }); it('can set raw conditions', function () { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->rawConditions('[[pagename::value]]'); expect($builder->build())->toBe([ @@ -356,14 +323,14 @@ it('cannot set 2 raw conditions', function () { expect( - fn () => LiquipediaBuilder::query() + fn () => (new LiquipediaBuilder()) ->rawConditions('[[pagename::value]]') ->rawConditions('[[pagename::value]]') )->toThrow(Exception::class); }); it('can build a complex query', function ($wiki) { - $builder = LiquipediaBuilder::query() + $builder = (new LiquipediaBuilder()) ->wikis($wiki) ->endpoint(Endpoint::MATCHES) ->limit(1) @@ -376,14 +343,17 @@ ->orderBy('field1', SortOrder::ASC) ->groupBy('field1', SortOrder::DESC) ->date('2020-01-01') - ->rawConditions('[[pagename::value]]'); + ->rawConditions( + ConditionsBuilder::build('pagename', '::', 'value') + ->toValue() + ); expect($builder->build())->toBe([ 'wiki' => $wiki, 'limit' => 1, 'offset' => 1, 'query' => 'field1,field2', - 'conditions' => '[[pagename::value]]', + 'conditions' => '([[pagename::value]])', 'order' => 'field1 ASC', 'pagination' => 1, 'groupby' => 'field1 DESC',