Skip to content

Commit

Permalink
refactor(Query): Refactor Query constructor and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 22, 2024
1 parent bf90f59 commit 21c71b3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
22 changes: 0 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
<p align="center">
<img src="https://raw.githubusercontent.com/nunomaduro/skeleton-php/master/docs/example.png" height="300" alt="Skeleton Php">
<p align="center">
<a href="https://github.com/nunomaduro/skeleton-php/actions"><img alt="GitHub Workflow Status (master)" src="https://github.com/nunomaduro/skeleton-php/actions/workflows/tests.yml/badge.svg"></a>
<a href="https://packagist.org/packages/nunomaduro/skeleton-php"><img alt="Total Downloads" src="https://img.shields.io/packagist/dt/nunomaduro/skeleton-php"></a>
<a href="https://packagist.org/packages/nunomaduro/skeleton-php"><img alt="Latest Version" src="https://img.shields.io/packagist/v/nunomaduro/skeleton-php"></a>
<a href="https://packagist.org/packages/nunomaduro/skeleton-php"><img alt="License" src="https://img.shields.io/packagist/l/nunomaduro/skeleton-php"></a>
</p>
</p>

------
This package provides a wonderful **PHP Skeleton** to start building your next package idea.

> **Requires [PHP 8.2+](https://php.net/releases/)**
⚡️ Create your package using [Composer](https://getcomposer.org):

```bash
composer create-project nunomaduro/skeleton-php --prefer-source PackageName
```

🧹 Keep a modern codebase with **Pint**:
```bash
Expand All @@ -43,5 +23,3 @@ composer test:unit
```bash
composer test
```

**Skeleton PHP** was created by **[Nuno Maduro](https://twitter.com/enunomaduro)** under the **[MIT license](https://opensource.org/licenses/MIT)**.
53 changes: 42 additions & 11 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ class Query extends QueryBuilder
protected string $endpoint;

/**
* @param array<string, mixed> $params
* @param Client|null $client
* @param array<string, mixed> $params
*/
public function __construct(array $params = [], ?Client $client = null)
public function __construct(
array $params = [],
?Client $client = null
)
{
parent::__construct(array_merge([
'wiki' => '',
'limit' => 100,
'conditions' => '',
], $params));
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
* @param array<string> $wikis
*/
public function wikis(array $wikis): self
{
Expand All @@ -44,13 +48,19 @@ public function wikis(array $wikis): self
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(
Expand All @@ -66,13 +76,19 @@ public function removeWiki(string $wiki): self
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);
Expand All @@ -81,6 +97,8 @@ public function rawConditions(string $conditions): self
}

/**
* @return $this
*
* @throws Exception
*/
public function andCondition(string $key, string $operator, string $value): self
Expand All @@ -96,7 +114,10 @@ public function andCondition(string $key, string $operator, string $value): self
}

/**
* @param array<string> $values
* @param array<string> $values
* @return $this
*
* @throws Exception
*/
public function andConditions(string $key, string $operator, array $values): self
{
Expand All @@ -115,6 +136,11 @@ public function andConditions(string $key, string $operator, array $values): sel
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).
Expand All @@ -127,6 +153,12 @@ public function orCondition(string $key, string $operator, string $value): self
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).
Expand Down Expand Up @@ -207,7 +239,6 @@ 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);
Expand Down
3 changes: 1 addition & 2 deletions src/Query/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

interface QueryBuilderInterface
{

/**
* @param array<string, mixed> $params
* @param array<string, mixed> $params
*/
public function __construct(array $params = []);

Expand Down
12 changes: 6 additions & 6 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<?php


use KCNetwork\Liquipedia\Query\QueryBuilder;

it('can be instantiated', function () {
$queryBuilder = new class extends QueryBuilder {
$queryBuilder = new class extends QueryBuilder
{
};

expect($queryBuilder)->toBeInstanceOf(QueryBuilder::class);
});

it('can be instantiated with params', function () {
$queryBuilder = new class(['foo' => 'bar']) extends QueryBuilder {
$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 {
$queryBuilder = new class(['foo' => 'bar']) extends QueryBuilder
{
public function build(): array
{
return ['bar' => 'foo'];
Expand All @@ -27,5 +29,3 @@ public function build(): array

expect($queryBuilder->build())->toBe(['bar' => 'foo']);
});


0 comments on commit 21c71b3

Please sign in to comment.