Skip to content

Commit

Permalink
feat: more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 23, 2024
1 parent a89ad95 commit 168c2f4
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 79 deletions.
73 changes: 0 additions & 73 deletions src/Endpoints/Endpoints.php

This file was deleted.

80 changes: 78 additions & 2 deletions src/LiquipediaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

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

Expand Down Expand Up @@ -69,9 +70,15 @@ public function addWiki(string $wiki): self
* Set a result limit.
*
* @return $this
*
* @throws Exception
*/
public function limit(int $limit): self
{
if ($limit > 9999) {
throw new Exception('[LiquipediaBuilder] Limit cannot be greater than 9999.');
}

$this->queryParameters->limit = $limit;

return $this;
Expand All @@ -98,14 +105,83 @@ 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;

return $this;
}

/**
* The datapoints you want to query.
*
* @param array<string> |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.
*/
Expand Down
42 changes: 42 additions & 0 deletions src/Meta/Endpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Npldevfr\Liquipedia\Meta;

use Npldevfr\Liquipedia\Traits\HasConstants;

final class Endpoints
{
use HasConstants;

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';
}
14 changes: 14 additions & 0 deletions src/Meta/SortOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Npldevfr\Liquipedia\Meta;

use Npldevfr\Liquipedia\Traits\HasConstants;

final class SortOrder
{
use HasConstants;

public final const ASC = 'ASC';

public final const DESC = 'DESC';
}
6 changes: 5 additions & 1 deletion src/Wikis/Wikis.php → src/Meta/Wikis.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

namespace Npldevfr\Liquipedia\Wikis;
namespace Npldevfr\Liquipedia\Meta;

use Npldevfr\Liquipedia\Traits\HasConstants;

final class Wikis
{
use HasConstants;

final public const AGE_OF_EMPIRES = 'ageofempires';

final public const APEX_LEGENDS = 'apexlegends';
Expand Down
10 changes: 10 additions & 0 deletions src/Query/QueryParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ final class QueryParameters implements QueryParametersInterface

public int $offset;

public string $query;

public string $conditions;

public string $order;

public int $pagination;

public string $groupby;

public function __construct(array $params = [])
{
foreach ($params as $key => $value) {
Expand Down
24 changes: 24 additions & 0 deletions src/Traits/HasConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Npldevfr\Liquipedia\Traits;

trait HasConstants
{
/**
* Get all the endpoints.
*
* @return array<string, mixed>
*/
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());
}
}
Loading

0 comments on commit 168c2f4

Please sign in to comment.