-
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.
- Loading branch information
Showing
10 changed files
with
346 additions
and
323 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,251 @@ | ||
<?php | ||
|
||
|
||
namespace Npldevfr\Liquipedia; | ||
|
||
use Exception; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use Npldevfr\Liquipedia\Query\QueryBuilder; | ||
|
||
final class Builder extends QueryBuilder | ||
{ | ||
private readonly Client $client; | ||
|
||
private string $endpoint; | ||
|
||
/** | ||
* @param array<string, mixed> $params | ||
*/ | ||
public function __construct( | ||
?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; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function addWiki(string $wiki): self | ||
{ | ||
$this->params['wiki'] .= '|'.$wiki; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function removeWiki(string $wiki): self | ||
{ | ||
$this->params['wiki'] = preg_replace( | ||
'/\|+/', | ||
'|', | ||
str_replace( | ||
$wiki, | ||
'', | ||
(string) $this->params['wiki'] | ||
) | ||
); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function limit(int $limit): self | ||
{ | ||
$this->params['limit'] = $limit; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @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 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,28 @@ | ||
<?php | ||
|
||
namespace Npldevfr\Liquipedia\Interfaces; | ||
|
||
use GuzzleHttp\Client; | ||
use Npldevfr\Liquipedia\Query\QueryParameters; | ||
|
||
interface QueryBuilderInterface | ||
{ | ||
|
||
/** | ||
* @param array<string, mixed> $params | ||
* @param QueryParameters|null $queryParameters | ||
* @param Client|null $client | ||
*/ | ||
public function __construct( | ||
array $params, | ||
?QueryParameters $queryParameters = null, | ||
?Client $client = null | ||
); | ||
|
||
|
||
/** | ||
* Build the query parameters array | ||
* @return array<string, mixed> | ||
*/ | ||
public function build(): array; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Query/QueryParametersInterface.php → src/Interfaces/QueryParametersInterface.php
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
namespace Npldevfr\Liquipedia\Query; | ||
namespace Npldevfr\Liquipedia\Interfaces; | ||
|
||
use JsonException; | ||
|
||
|
Oops, something went wrong.