Skip to content

Commit

Permalink
feat: builder
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 30, 2024
1 parent 114ab68 commit c5b3d4c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 10 deletions.
41 changes: 41 additions & 0 deletions src/Liquipedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Npldevfr\Liquipedia;

use GuzzleHttp\Client;
use Npldevfr\Liquipedia\Query\QueryParameters;

final class Liquipedia
{
private readonly string $apiKey;

private readonly LiquipediaBuilder $builder;

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

if ($apiKey === '' || $apiKey === '0') {
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(array $params = [], ?QueryParameters $queryParameters = null, ?Client $client = null): LiquipediaBuilder

Check failure on line 36 in src/Liquipedia.php

View workflow job for this annotation

GitHub Actions / Formats P8.2 - ubuntu-latest - prefer-lowest

Method Npldevfr\Liquipedia\Liquipedia::query() has parameter $params with no value type specified in iterable type array.

Check failure on line 36 in src/Liquipedia.php

View workflow job for this annotation

GitHub Actions / Formats P8.2 - ubuntu-latest - prefer-stable

Method Npldevfr\Liquipedia\Liquipedia::query() has parameter $params with no value type specified in iterable type array.
{

return (new self($this->apiKey))->builder->query($params, $queryParameters, $client);
}
}
36 changes: 28 additions & 8 deletions src/LiquipediaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use JsonException;
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;
Expand All @@ -19,20 +20,17 @@ 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',
]));
parent::__construct($params, $queryParameters, $client);
}

/**
* @param array<string> $params
*/
public static function query(array $params = [], ?QueryParameters $queryParameters = null): self
public static function query(array $params = [], ?QueryParameters $queryParameters = null, ?Client $client = null): self
{
return new self($params, $queryParameters);
return new self($params, $queryParameters, $client);
}

/**
Expand Down Expand Up @@ -260,6 +258,28 @@ private function avoidWikiDuplicates(): void
);
}

/**
* @return array<string>
*
* @throws JsonException | Exception | GuzzleException
*/
public function get(?string $endpoint = null): array
{

dd($this->client->getConfig());

$customEndpoint = $endpoint ?? $this->endpoint;

Check failure on line 271 in src/LiquipediaBuilder.php

View workflow job for this annotation

GitHub Actions / Formats P8.2 - ubuntu-latest - prefer-lowest

Unreachable statement - code above always terminates.

Check failure on line 271 in src/LiquipediaBuilder.php

View workflow job for this annotation

GitHub Actions / Formats P8.2 - ubuntu-latest - prefer-stable

Unreachable statement - code above always terminates.
$response = json_decode($this->client->get($customEndpoint, [
'query' => $this->queryParameters->toArray(),
])->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);

if (isset($response->error)) {
throw new Exception($response->error);
}

// $this->queryParameters = new QueryParameters();
return $response->result ?? [];
}
// /**
// * @return $this
// */
Expand Down
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
8 changes: 6 additions & 2 deletions tests/LiquipediaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Npldevfr\Liquipedia\ConditionsBuilder;
use Npldevfr\Liquipedia\LiquipediaBuilder;
use Npldevfr\Liquipedia\Meta\Endpoint;
use Npldevfr\Liquipedia\Meta\SortOrder;
Expand Down Expand Up @@ -376,14 +377,17 @@
->orderBy('field1', SortOrder::ASC)
->groupBy('field1', SortOrder::DESC)
->date('2020-01-01')
->rawConditions('[[pagename::value]]');
->rawConditions(
ConditionsBuilder::build('pagename', '::', 'value')
->toValue()
);

expect($builder->build())->toBe([
'wiki' => $wiki,
'limit' => 1,
'offset' => 1,
'query' => 'field1,field2',
'conditions' => '[[pagename::value]]',
'conditions' => '([[pagename::value]])',
'order' => 'field1 ASC',
'pagination' => 1,
'groupby' => 'field1 DESC',
Expand Down
23 changes: 23 additions & 0 deletions tests/LiquipediaQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Npldevfr\Liquipedia\ConditionsBuilder;
use Npldevfr\Liquipedia\Liquipedia;
use Npldevfr\Liquipedia\Meta\Endpoint;

it('can query liquipedia api', function () {

$liquipedia = new Liquipedia('myapikey');
dd($liquipedia
->query()
->endpoint(Endpoint::MATCHES)
->rawConditions(
ConditionsBuilder::build(
'opponent',
'::',
'Karmine Corp'
)->toValue()
)
->limit(10)
->get());

})->only();
Binary file added xdebug-3.3.1.tgz
Binary file not shown.

0 comments on commit c5b3d4c

Please sign in to comment.