Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $g1 = FilterGroup::create()
$criteria = Criteria::default()
->withFilterGroup($g1)
->withOrderBy('surname')
->withOrderType(Order::TYPE_ASC)
->withOrderType('asc')
->withPageLimit(10)
->withPageOffset(5);

Expand All @@ -44,7 +44,7 @@ $criteria = Criteria::default()
->addFilterGreaterThan('followers', 7000)
->addFilterIn('country', ['es', 'fr']))
->withOrderBy('surname')
->withOrderType(Order::TYPE_ASC)
->withOrderType('asc')
->withPageLimit(10)
->withPageOffset(5);

Expand All @@ -55,7 +55,7 @@ $users = $repository->match($criteria);
```

A `FilterGroup` is a set of filters or conditions that must match all together (`AND`). To match one group or another
(OR), just add more `FilterGroup`.
(`OR`), just add more `FilterGroup`.

```php

Expand Down
21 changes: 17 additions & 4 deletions src/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,23 @@ public function withOrder(Order $order): self
return self::create($this->groups, $order, $this->page);
}

public function withOrderRandom(): self
{
return self::create($this->groups, Order::random(), $this->page);
}

public function withOrderBy(string $field): self
{
return self::create($this->groups, Order::create($field, $this->orderType()), $this->page);
return self::create($this->groups, Order::create($field, $this->order->type()), $this->page);
}

public function withOrderType(string $type): self
{
return self::create($this->groups, Order::create($this->orderBy(), $type), $this->page);
return self::create(
$this->groups,
Order::create($this->orderBy(), OrderType::make($type)),
$this->page
);
}

public function withPage(Page $page): self
Expand All @@ -98,7 +107,11 @@ public function withPage(Page $page): self

public function withPageOffset(int $offset): self
{
return self::create($this->groups, $this->order, Page::create($this->pageLimit(), $offset));
return self::create(
$this->groups,
$this->order,
Page::create($this->pageLimit(), $offset)
);
}

public function withPageLimit(int $limit): self
Expand Down Expand Up @@ -128,7 +141,7 @@ public function orderBy(): string

public function orderType(): string
{
return $this->order->type();
return $this->order->type()->value;
}

public function page(): Page
Expand Down
24 changes: 12 additions & 12 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,62 +70,62 @@ public static function createFromArray(array $filter): self

public static function createEqual(string $field, mixed $value): self
{
return self::create($field, Operator::equal(), $value);
return self::create($field, Operator::EQUAL, $value);
}

public static function createNotEqual(string $field, mixed $value): self
{
return self::create($field, Operator::notEqual(), $value);
return self::create($field, Operator::NOT_LIKE, $value);
}

public static function createGreaterThan(string $field, mixed $value): self
{
return self::create($field, Operator::gt(), $value);
return self::create($field, Operator::GT, $value);
}

public static function createGreaterOrEqualThan(string $field, mixed $value): self
{
return self::create($field, Operator::gte(), $value);
return self::create($field, Operator::GTE, $value);
}

public static function createLessThan(string $field, mixed $value): self
{
return self::create($field, Operator::lt(), $value);
return self::create($field, Operator::LT, $value);
}

public static function createLessOrEqualThan(string $field, mixed $value): self
{
return self::create($field, Operator::lte(), $value);
return self::create($field, Operator::LTE, $value);
}

public static function createIn(string $field, mixed $value): self
{
return self::create($field, Operator::in(), $value);
return self::create($field, Operator::IN, $value);
}

public static function createNotIn(string $field, mixed $value): self
{
return self::create($field, Operator::notIn(), $value);
return self::create($field, Operator::NOT_IN, $value);
}

public static function createLike(string $field, mixed $value): self
{
return self::create($field, Operator::like(), $value);
return self::create($field, Operator::LIKE, $value);
}

public static function createNotLike(string $field, mixed $value): self
{
return self::create($field, Operator::notLike(), $value);
return self::create($field, Operator::NOT_LIKE, $value);
}

public static function createContains(string $field, mixed $value): self
{
return self::create($field, Operator::contains(), $value);
return self::create($field, Operator::CONTAINS, $value);
}

public static function createNotContains(string $field, mixed $value): self
{
return self::create($field, Operator::notContains(), $value);
return self::create($field, Operator::NOT_CONTAINS, $value);
}

/**
Expand Down
60 changes: 0 additions & 60 deletions src/Operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,64 +29,4 @@ public static function make(string $value): self
{
return self::from($value);
}

public static function equal(): self
{
return self::EQUAL;
}

public static function notEqual(): self
{
return self::NOT_EQUAL;
}

public static function gt(): self
{
return self::GT;
}

public static function gte(): self
{
return self::GTE;
}

public static function lt(): self
{
return self::LT;
}

public static function lte(): self
{
return self::LTE;
}

public static function in(): self
{
return self::IN;
}

public static function notIn(): self
{
return self::NOT_IN;
}

public static function like(): self
{
return self::LIKE;
}

public static function notLike(): self
{
return self::NOT_LIKE;
}

public static function contains(): self
{
return self::CONTAINS;
}

public static function notContains(): self
{
return self::NOT_CONTAINS;
}
}
32 changes: 19 additions & 13 deletions src/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ final class Order implements ValueObject
public const TYPE_ASC = 'asc';
public const TYPE_DESC = 'desc';
public const TYPE_NONE = 'none';
public const TYPE_RANDOM = 'random';

/**
* Order constructor.
*
* @param string $by
* @param string $type
* @param OrderType $type
*/
public function __construct(
private readonly string $by,
private readonly string $type = self::TYPE_ASC,
private readonly OrderType $type,
) {
$this->check();
}
Expand All @@ -39,12 +40,7 @@ protected function invariantOrderByValueMustContainOnlyAlphanumericalCharacters(
return preg_match('/\w*/', $this->by) === 1;
}

protected function invariantOrderTypeValueMustBeOneOfAscDescOrNone(): bool
{
return in_array($this->type, [self::TYPE_ASC, self::TYPE_DESC, self::TYPE_NONE]);
}

public static function create(string $by, string $type = self::TYPE_ASC): self
public static function create(string $by, OrderType $type = OrderType::ASC): self
{
return new self($by, $type);
}
Expand All @@ -56,31 +52,41 @@ public static function createAscBy(string $by): self

public static function createDescBy(string $by): self
{
return self::create($by, self::TYPE_DESC);
return self::create($by, OrderType::DESC);
}

public static function none(): Order
{
return self::create('', self::TYPE_NONE);
return self::create('', OrderType::NONE);
}

public static function random(): Order
{
return self::create('', OrderType::RANDOM);
}

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

public function type(): string
public function type(): OrderType
{
return $this->type;
}

public function isNone(): bool
{
return $this->type === self::TYPE_NONE;
return $this->type === OrderType::NONE;
}

public function isRandom(): bool
{
return $this->type === OrderType::RANDOM;
}

public function __toString(): string
{
return sprintf('%s.%s', $this->by(), $this->type());
return sprintf('%s.%s', $this->by(), $this->type()->value);
}
}
24 changes: 24 additions & 0 deletions src/OrderType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace ComplexHeart\Domain\Criteria;

/**
* Class OrderType
*
* @author Unay Santisteban <usantisteban@othercode.io>
* @package ComplexHeart\Domain\Criteria
*/
enum OrderType: string
{
case ASC = 'asc';
case DESC = 'desc';
case NONE = 'none';
case RANDOM = 'random';

public static function make(string $value): self
{
return self::from($value);
}
}
12 changes: 9 additions & 3 deletions tests/CriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@

expect($c->orderBy())->toBe('name')
->and($c->orderType())->toBe('desc')
->and($c->order()->isNone())->toBe(false);
->and($c->order()->isNone())->toBeFalse();

$c = $c->withOrder(Order::createAscBy('name'));
expect($c->orderType())->toBe('asc');

$c = $c->withOrder(Order::random());
expect($c->order()->isRandom())->toBeTrue();

$c = $c->withOrderRandom();
expect($c->order()->isRandom())->toBeTrue();

$c = $c->withOrderBy('surname');
expect($c->orderBy())->toBe('surname');

$c = $c->withOrderType(Order::TYPE_ASC);
$c = $c->withOrderType('asc');
expect($c->orderType())->toBe('asc')
->and($c->order())->toBeInstanceOf(Order::class);
});
Expand Down Expand Up @@ -84,7 +90,7 @@
->withPageLimit(100)
->withPageOffset(0)
->withOrderBy('name')
->withOrderType(Order::TYPE_ASC);
->withOrderType('asc');

expect($c->__toString())->toBe('name.=.Vincent+age.>=.35#name.asc#100.0');
});
4 changes: 2 additions & 2 deletions tests/FilterGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
];

$g = FilterGroup::createFromArray($filters)
->addFilter(Filter::create('field', Operator::equal(), 'value'))
->addFilter(Filter::create('name', Operator::equal(), 'Vega'));
->addFilter(Filter::create('field', Operator::EQUAL, 'value'))
->addFilter(Filter::create('name', Operator::EQUAL, 'Vega'));

expect($g)
->toHaveCount(2);
Expand Down