-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Endpoints and Query classes
- Loading branch information
Showing
6 changed files
with
315 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace KCNetwork\Liquipedia\Endpoints; | ||
|
||
enum Endpoints: string | ||
{ | ||
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
<?php | ||
|
||
namespace KCNetwork\Liquipedia; | ||
|
||
use Exception; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use KCNetwork\Liquipedia\Query\QueryBuilder; | ||
|
||
class Query extends QueryBuilder | ||
{ | ||
protected Client $client; | ||
|
||
protected string $endpoint; | ||
|
||
/** | ||
* @param array<string, mixed> $params | ||
* @param Client|null $client | ||
*/ | ||
public function __construct(array $params = [], ?Client $client = null) | ||
{ | ||
parent::__construct(array_merge([ | ||
'wiki' => '', | ||
'limit' => 100, | ||
'conditions' => '', | ||
], $params)); | ||
|
||
$this->client = $client ?? new Client([ | ||
'base_uri' => 'https://api.liquipedia.net/api/', | ||
]); | ||
} | ||
|
||
/** | ||
* @param array<string> $wikis | ||
*/ | ||
public function wikis(array $wikis): self | ||
{ | ||
|
||
$wikis = implode('|', $wikis); | ||
$wikis = preg_replace('/\|+/', '|', $wikis); | ||
|
||
$this->params['wiki'] = $wikis; | ||
|
||
return $this; | ||
} | ||
|
||
public function addWiki(string $wiki): self | ||
{ | ||
$this->params['wiki'] .= '|'.$wiki; | ||
|
||
return $this; | ||
} | ||
|
||
public function removeWiki(string $wiki): self | ||
{ | ||
$this->params['wiki'] = preg_replace( | ||
'/\|+/', | ||
'|', | ||
str_replace( | ||
$wiki, | ||
'', | ||
$this->params['wiki'] | ||
) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
public function limit(int $limit): self | ||
{ | ||
$this->params['limit'] = $limit; | ||
|
||
return $this; | ||
} | ||
|
||
public function rawConditions(string $conditions): self | ||
{ | ||
$this->params['conditions'] = trim($this->params['conditions'].' '.$conditions); | ||
|
||
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<string> $values | ||
*/ | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
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 select(array $fields): self | ||
{ | ||
if (! isset($this->params['query'])) { | ||
$this->params['query'] = implode(',', $fields); | ||
} else { | ||
$this->params['query'] .= ','.implode(',', $fields); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* What you want your results grouped by (this can be helpful when using aggregate functions). | ||
* Example: pagename ASC | ||
* | ||
* @throws Exception | ||
*/ | ||
public function groupBy(string $field, string $direction = 'ASC'): self | ||
{ | ||
|
||
if (! in_array(strtoupper($direction), ['ASC', 'DESC'])) { | ||
throw new Exception('Direction must be ASC or DESC'); | ||
} | ||
|
||
$this->params['groupby'] = "{$field} {$direction}"; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* The order you want your result in. | ||
* Example: pagename ASC | ||
* | ||
* @throws Exception | ||
*/ | ||
public function orderBy(string $field, string $direction = 'ASC'): self | ||
{ | ||
|
||
if (! in_array(strtoupper($direction), ['ASC', 'DESC'])) { | ||
throw new Exception('Direction must be ASC or DESC'); | ||
} | ||
|
||
$this->params['order'] = "{$field} {$direction}"; | ||
|
||
return $this; | ||
} | ||
|
||
public function setEndpoint(string $endpoint): self | ||
{ | ||
$this->endpoint = $endpoint; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @throws GuzzleException | ||
* @throws Exception | ||
*/ | ||
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 ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace KCNetwork\Liquipedia\Query; | ||
|
||
abstract class QueryBuilder implements QueryBuilderInterface | ||
{ | ||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
protected array $params; | ||
|
||
public function __construct(array $params = []) | ||
{ | ||
$this->params = $params; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return $this->params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace KCNetwork\Liquipedia\Query; | ||
|
||
interface QueryBuilderInterface | ||
{ | ||
|
||
/** | ||
* @param array<string, mixed> $params | ||
*/ | ||
public function __construct(array $params = []); | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function build(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
|
||
use KCNetwork\Liquipedia\Query\QueryBuilder; | ||
|
||
it('can be instantiated', function () { | ||
$queryBuilder = new class extends QueryBuilder { | ||
}; | ||
|
||
expect($queryBuilder)->toBeInstanceOf(QueryBuilder::class); | ||
}); | ||
|
||
it('can be instantiated with params', function () { | ||
$queryBuilder = new class(['foo' => 'bar']) extends QueryBuilder { | ||
}; | ||
|
||
expect($queryBuilder->build())->toBe(['foo' => 'bar']); | ||
}); | ||
|
||
it('can be instantiated with params and build', function () { | ||
$queryBuilder = new class(['foo' => 'bar']) extends QueryBuilder { | ||
public function build(): array | ||
{ | ||
return ['bar' => 'foo']; | ||
} | ||
}; | ||
|
||
expect($queryBuilder->build())->toBe(['bar' => 'foo']); | ||
}); | ||
|
||
|