Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: client instance #1

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Interfaces/LiquipediaBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Npldevfr\Liquipedia\Interfaces;

interface LiquipediaBuilderInterface
{
/**
* Set wikis you want to query.
*
* @param array<string> | 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<string, mixed>
*/
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;
}
39 changes: 39 additions & 0 deletions src/Liquipedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Npldevfr\Liquipedia;

use GuzzleHttp\Client;

final class Liquipedia
{
private readonly string $apiKey;

private readonly LiquipediaBuilder $builder;

public function __construct(
string $apiKey,
?LiquipediaBuilder $builder = null
) {

if ($apiKey === '') {
throw new \InvalidArgumentException('[Liquipedia] Api key is required');
}

$this->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;
}
}
152 changes: 30 additions & 122 deletions src/LiquipediaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<string> $params
*/
public static function query(array $params = [], ?QueryParameters $queryParameters = null): self
{
return new self($params, $queryParameters);
parent::__construct($params, $queryParameters, $client);
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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<string> $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<string> $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<string>
*
* @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 ?? [];
}
}
1 change: 1 addition & 0 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
}

$this->client = $client ?? new Client();

}

public function build(): array
Expand Down
Loading
Loading