Skip to content

Commit

Permalink
feat: conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
npldevfr committed Jan 29, 2024
1 parent 14522c2 commit 4839d01
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 18 deletions.
89 changes: 74 additions & 15 deletions src/ConditionsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Npldevfr\Liquipedia;

use Exception;
use Npldevfr\Liquipedia\Interfaces\ConditionsBuilderInterface;
use Npldevfr\Liquipedia\Meta\Conditions;

final class ConditionsBuilder
final class ConditionsBuilder implements ConditionsBuilderInterface
{
private string $raw;

Expand All @@ -14,14 +15,22 @@ public function __construct(?string $raw = null)
$this->raw = $raw ?? '';
}

/**
* @throws Exception
*/
public static function build(string $key, string $operator, string $value): self
{
self::ensureValidOperator($operator);
public static function build(
?string $key = null,
?string $operator = null,
?string $value = null
): self {
if (is_null($key) && is_null($operator) && is_null($value)) {
return new self();
}

if (isset($key) && isset($operator) && isset($value)) {
self::ensureValidOperator($operator);

return new self("([[$key$operator$value]])");
return new self("([[$key$operator$value]])");
}

throw new Exception('[LiquipediaBuilder] Invalid arguments, you have to set all or none of them.');
}

/**
Expand All @@ -45,16 +54,66 @@ public function and(string $key, string $operator, string $value): self
return $this;
}

// public function andMany(array $conditions): self
// {
// foreach ($conditions as $key => $condition) {
// $this->and($key, $condition[0], $condition[1]);
// }
// return $this;
// }
public function andManyAnd(string $key, string $operator, array $values): self
{
self::ensureValidOperator($operator);

$raws = array_map(fn ($value): string => "[[$key$operator$value]]", $values);

$this->raw .= ($this->isEmpty() ? '' : ' AND ').'('.implode(' AND ', $raws).')';

return $this;
}

private function isEmpty(): bool
{
return $this->raw === '';
}

public function toValue(): string
{
return $this->raw;
}

public function andManyOr(string $key, string $operator, array $values): ConditionsBuilderInterface
{
self::ensureValidOperator($operator);

$raws = array_map(fn ($value): string => "[[$key$operator$value]]", $values);

$this->raw .= ($this->isEmpty() ? '' : ' AND ').'('.implode(' OR ', $raws).')';

return $this;
}

public function or(string $key, string $operator, string $value): ConditionsBuilderInterface
{
self::ensureValidOperator($operator);

$this->raw .= ' OR ([['.$key.$operator.$value.']])';

return $this;
}

public function orManyAnd(string $key, string $operator, array $values): ConditionsBuilderInterface
{
self::ensureValidOperator($operator);

$raws = array_map(fn ($value): string => "[[$key$operator$value]]", $values);

$this->raw .= ($this->isEmpty() ? '' : ' OR ').'('.implode(' AND ', $raws).')';

return $this;
}

public function orManyOr(string $key, string $operator, array $values): ConditionsBuilderInterface
{
self::ensureValidOperator($operator);

$raws = array_map(fn ($value): string => "[[$key$operator$value]]", $values);

$this->raw .= ($this->isEmpty() ? '' : ' OR ').'('.implode(' OR ', $raws).')';

return $this;
}
}
55 changes: 55 additions & 0 deletions src/Interfaces/ConditionsBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Npldevfr\Liquipedia\Interfaces;

use Exception;

interface ConditionsBuilderInterface
{
/**
* @throws Exception
*/
public function and(string $key, string $operator, string $value): self;

/**
* @param array<string> $values
*
* @throws Exception
*/
public function andManyAnd(string $key, string $operator, array $values): self;

/**
* @param array<string> $values
*
* @throws Exception
*/
public function andManyOr(string $key, string $operator, array $values): self;

/**
* @throws Exception
*/
public function or(string $key, string $operator, string $value): self;

/**
* @param array<string> $values
*
* @throws Exception
*/
public function orManyAnd(string $key, string $operator, array $values): self;

/**
* @param array<string> $values
*
* @throws Exception
*/
public function orManyOr(string $key, string $operator, array $values): self;

public function toValue(): string;

/**
* @throws Exception
*/
public static function build(?string $key = null,
?string $operator = null,
?string $value = null): self;
}
114 changes: 111 additions & 3 deletions tests/ConditionsBuilderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Npldevfr\Liquipedia\ConditionsBuilder;
use Npldevfr\Liquipedia\Meta\Conditions;

it('can build a with all conditions', function ($condition) {

Expand All @@ -10,13 +11,120 @@

expect($builder->toValue())
->toBe('([[my_key'.$condition.'my_value]])');
})->with(\Npldevfr\Liquipedia\Meta\Conditions::all());
})->with(Conditions::all());

it('', function () {
it('can build with default constructor & and method', function () {

expect(
ConditionsBuilder::build('build_key', '::!', 'build_value')
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->and('my_key', '::', 'my_value')
->toValue()
)->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]])');
});

it('can build without default constructor & andManyAnd method', function () {
$builder = ConditionsBuilder::build()
->andManyAnd('my_key', '::', ['my_value', 'my_value2']);

expect($builder->toValue())
->toBe('([[my_key::my_value]] AND [[my_key::my_value2]])');

});

it('can build with default constructor & andManyA d method', function () {

expect(
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->andManyAnd('my_key', '::', ['my_value', 'my_value2'])
->toValue()
)->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] AND [[my_key::my_value2]])');
});

it('can build with default constructor & andManyOr method', function () {

expect(
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->andManyOr('my_key', '::', ['my_value', 'my_value2'])
->toValue()
)->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] OR [[my_key::my_value2]])');
});

it('can build with default constructor & or method', function () {

expect(
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->or('my_key', '::', 'my_value')
->toValue()
)->toBe('([[build_key::!build_value]]) OR ([[my_key::my_value]])');
});

it('can build with default constructor & orManyAnd method', function () {

expect(
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->orManyAnd('my_key', '::', ['my_value', 'my_value2'])
->toValue()
)->toBe('([[build_key::!build_value]]) OR ([[my_key::my_value]] AND [[my_key::my_value2]])');
});

it('can build with default constructor & orManyOr method', function () {

expect(
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->orManyOr('my_key', '::', ['my_value', 'my_value2'])
->toValue()
)->toBe('([[build_key::!build_value]]) OR ([[my_key::my_value]] OR [[my_key::my_value2]])');
});

it('can build with default constructor & andManyAnd method', function () {

expect(
ConditionsBuilder::build('build_key', Conditions::NOT_EQUAL, 'build_value')
->andManyAnd('my_key', '::', ['my_value', 'my_value2'])
->toValue()
)->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] AND [[my_key::my_value2]])');
});

it('can build with default constructor & andManyAnd & orManyAnd methods', function () {

$builder = ConditionsBuilder::build('build_key', '::!', 'build_value')
->andManyAnd('my_key', '::', ['my_value', 'my_value2'])
->orManyAnd('my_key2', '::', ['my_value', 'my_value2']);

expect($builder->toValue())
->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] AND [[my_key::my_value2]]) OR ([[my_key2::my_value]] AND [[my_key2::my_value2]])');

});

it('can build with default constructor & andManyAnd & orManyOr methods', function () {

$builder = ConditionsBuilder::build('build_key', '::!', 'build_value')
->andManyAnd('my_key', '::', ['my_value', 'my_value2'])
->orManyOr('my_key2', '::', ['my_value', 'my_value2']);

expect($builder->toValue())
->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] AND [[my_key::my_value2]]) OR ([[my_key2::my_value]] OR [[my_key2::my_value2]])');

});

it('can build with default constructor & andManyOr & orManyAnd methods', function () {

$builder = ConditionsBuilder::build('build_key', '::!', 'build_value')
->andManyOr('my_key', '::', ['my_value', 'my_value2'])
->orManyAnd('my_key2', '::', ['my_value', 'my_value2']);

expect($builder->toValue())
->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] OR [[my_key::my_value2]]) OR ([[my_key2::my_value]] AND [[my_key2::my_value2]])');

});

it('can build with default constructor & andManyOr & orManyOr methods', function () {

$builder = ConditionsBuilder::build('build_key', '::!', 'build_value')
->andManyOr('my_key', '::', ['my_value', 'my_value2'])
->orManyOr('my_key2', '::', ['my_value', 'my_value2']);

expect($builder->toValue())
->toBe('([[build_key::!build_value]]) AND ([[my_key::my_value]] OR [[my_key::my_value2]]) OR ([[my_key2::my_value]] OR [[my_key2::my_value2]])');

});

0 comments on commit 4839d01

Please sign in to comment.