diff --git a/src/Endpoints/Endpoints.php b/src/Endpoints/Endpoints.php deleted file mode 100644 index 94337c7..0000000 --- a/src/Endpoints/Endpoints.php +++ /dev/null @@ -1,73 +0,0 @@ - 9999) { + throw new Exception('[LiquipediaBuilder] Limit cannot be greater than 9999.'); + } + $this->queryParameters->limit = $limit; return $this; @@ -98,7 +105,7 @@ public function offset(int $offset): self */ public function endpoint(string $endpoint): self { - if (! Endpoints::fromArray($endpoint)) { + if (! Endpoints::fromValue($endpoint)) { throw new Exception('[LiquipediaBuilder] Endpoint '.$endpoint.' is not valid.'); } $this->endpoint = $endpoint; @@ -106,6 +113,75 @@ public function endpoint(string $endpoint): self return $this; } + /** + * The datapoints you want to query. + * + * @param array |string $fields + * @return $this + */ + public function select(array|string $fields): self + { + $fields = is_string($fields) ? explode(',', str_replace(' ', '', $fields)) : $fields; + + $existingFields = $this->queryParameters->query ?? ''; + $newFields = array_unique(array_filter($fields)); + + $allFields = array_merge(explode(',', $existingFields), $newFields); + $this->queryParameters->query = implode(',', array_unique(array_filter($allFields))); + + return $this; + } + + /** + * Set the pagination of the results. + * + * @return $this + */ + public function pagination(int|string $pagination): self + { + $this->queryParameters->pagination = (int) $pagination; + + return $this; + } + + /** + * Order by a field. + * + * @throws Exception + */ + public function orderBy(string $field, string $direction = 'ASC'): self + { + + $direction = strtoupper($direction); + + if (! SortOrder::fromValue($direction)) { + throw new Exception('[LiquipediaBuilder] Direction '.$direction.' is not valid.'); + } + + $this->queryParameters->order = "{$field} ".$direction; + + return $this; + } + + /** + * Group by a field. + * + * @throws Exception + */ + public function groupBy(string $field, string $direction = 'ASC'): self + { + + $direction = strtoupper($direction); + + if (! SortOrder::fromValue($direction)) { + throw new Exception('[LiquipediaBuilder] Direction '.$direction.' is not valid.'); + } + + $this->queryParameters->groupby = "{$field} ".$direction; + + return $this; + } + /** * Get the endpoint you want to query. */ diff --git a/src/Meta/Endpoints.php b/src/Meta/Endpoints.php new file mode 100644 index 0000000..0c1edec --- /dev/null +++ b/src/Meta/Endpoints.php @@ -0,0 +1,42 @@ + $value) { diff --git a/src/Traits/HasConstants.php b/src/Traits/HasConstants.php new file mode 100644 index 0000000..09efe3c --- /dev/null +++ b/src/Traits/HasConstants.php @@ -0,0 +1,24 @@ + + */ + public static function all(): array + { + return (new \ReflectionClass(self::class))->getConstants(); + } + + /** + * Check if the value is valid. + */ + public static function fromValue(string $value): bool + { + return in_array($value, self::all()); + } +} diff --git a/tests/LiquipediaBuilderTest.php b/tests/LiquipediaBuilderTest.php index 742bd8a..1187c69 100644 --- a/tests/LiquipediaBuilderTest.php +++ b/tests/LiquipediaBuilderTest.php @@ -1,11 +1,12 @@ Wikis::LEAGUE_OF_LEGENDS, ]); @@ -13,6 +14,7 @@ expect($builder->build())->toBe([ 'wiki' => 'leagueoflegends', ]); + }); it('can build a query with a query parameter object', function () { @@ -150,6 +152,7 @@ }); it('can set an endpoint and a wiki', function () { + $builder = LiquipediaBuilder::query() ->endpoint(Endpoints::MATCHES) ->wikis(Wikis::LEAGUE_OF_LEGENDS); @@ -162,3 +165,147 @@ ->toBe(Endpoints::MATCHES); }); + +it('can select only some fields', function () { + $builder = LiquipediaBuilder::query() + ->select([ + 'field1', + 'field2', + ]); + + expect($builder->build())->toBe([ + 'query' => 'field1,field2', + ]); +}); + +it('can select only some fields with a string', function () { + $builder = LiquipediaBuilder::query() + ->select('field1, field2,field3'); + + expect($builder->build())->toBe([ + 'query' => 'field1,field2,field3', + ]); +}); + +it('can select only some fields with a string and an array', function () { + $builder = LiquipediaBuilder::query() + ->select([ + 'field1', + 'field2', + ]) + ->select('field3, field4'); + + expect($builder->build())->toBe([ + 'query' => 'field1,field2,field3,field4', + ]); +}); + +it('can select only some fields with a string and an array with duplicates', function () { + $builder = LiquipediaBuilder::query() + ->select([ + 'field1', + 'field2', + ]) + ->select('field3, field4,,') + ->select('field3, field4'); + + expect($builder->build())->toBe([ + 'query' => 'field1,field2,field3,field4', + ]); +}); + +it('can use pagination', function () { + $builder = LiquipediaBuilder::query() + ->pagination(1); + + expect($builder->build())->toBe([ + 'pagination' => 1, + ]); + +}); + +it('can use pagination with a string', function () { + $builder = LiquipediaBuilder::query() + ->pagination('1'); + + expect($builder->build())->toBe([ + 'pagination' => 1, + ]); + +}); + +it('can use pagination with a string and an int', function () { + $builder = LiquipediaBuilder::query() + ->pagination('1') + ->pagination(2); + + expect($builder->build())->toBe([ + 'pagination' => 2, + ]); + +}); + +it('can order by a field', function ($order) { + $builder = LiquipediaBuilder::query() + ->orderBy('field1', $order); + + expect($builder->build())->toBe([ + 'order' => 'field1 '.$order, + ]); +})->with(SortOrder::all()); + +it('cannot order by a field with an invalid order', function () { + expect( + fn () => LiquipediaBuilder::query() + ->orderBy('field1', 'invalid') + )->toThrow(Exception::class); +}); + +it('can order by a field with a string', function ($order) { + $builder = LiquipediaBuilder::query() + ->orderBy('field1', $order) + ->orderBy('field2', 'asc'); + + expect($builder->build())->toBe([ + 'order' => 'field2 ASC', + ]); +})->with(SortOrder::all()); + +it('can group by a field', function () { + $builder = LiquipediaBuilder::query() + ->groupBy('field1'); + + expect($builder->build())->toBe([ + 'groupby' => 'field1 ASC', + ]); +}); + +it('can group by a field with a string', function () { + $builder = LiquipediaBuilder::query() + ->groupBy('field1', 'DESC'); + + expect($builder->build())->toBe([ + 'groupby' => 'field1 DESC', + ]); +}); + +it('can group by a field with a string and an array', function () { + $builder = LiquipediaBuilder::query() + ->groupBy('field1', 'DESC') + ->groupBy('field2', 'ASC'); + + expect($builder->build())->toBe([ + 'groupby' => 'field2 ASC', + ]); +}); + +it('can group by a field with a string and an array with duplicates', function () { + $builder = LiquipediaBuilder::query() + ->groupBy('field1', 'DESC') + ->groupBy('field2', 'ASC') + ->groupBy('field2', 'ASC'); + + expect($builder->build())->toBe([ + 'groupby' => 'field2 ASC', + ]); +});