Skip to content

Commit

Permalink
feat: Add Endpoints and Query classes
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 22, 2024
1 parent 624e3c7 commit bf90f59
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
}
],
"require": {
"php": "^8.2.0"
"php": "^8.2.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"laravel/pint": "^1.13.7",
"pestphp/pest": "^2.28.1",
"phpstan/phpstan": "^1.10.50",
"rector/rector": "^0.19.2",
"symfony/var-dumper": "^6.4.0|^7.0.0"
},
"autoload": {
Expand Down
23 changes: 23 additions & 0 deletions src/Endpoints/Endpoints.php
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';
}
221 changes: 221 additions & 0 deletions src/Query.php
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 ?? [];
}
}
21 changes: 21 additions & 0 deletions src/Query/QueryBuilder.php
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;
}
}
17 changes: 17 additions & 0 deletions src/Query/QueryBuilderInterface.php
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;
}
31 changes: 31 additions & 0 deletions tests/QueryBuilderTest.php
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']);
});


0 comments on commit bf90f59

Please sign in to comment.