Skip to content

Commit

Permalink
feat: liquipediabuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 23, 2024
1 parent 54ba522 commit 5052ec0
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 323 deletions.
27 changes: 4 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,11 @@ composer require npldevfr/liquipedia-client
// todo
```




🧹 Keep a modern codebase with **Pint**:
```bash
composer lint
```

✅ Run refactors using **Rector**
## 🛠️ Development
```bash
composer refacto
```
git clone https://github.com/npldevfr/liquipedia-client
composer install

⚗️ Run static analysis using **PHPStan**:
```bash
composer test:types
```

✅ Run unit tests using **PEST**
```bash
composer test:unit
```

🚀 Run the entire test suite:
```bash
composer test
```

251 changes: 251 additions & 0 deletions src/Builder.php
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 ?? [];
}
}
28 changes: 28 additions & 0 deletions src/Interfaces/QueryBuilderInterface.php
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;
}
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;

Expand Down
Loading

0 comments on commit 5052ec0

Please sign in to comment.