Skip to content

Commit

Permalink
Fixes (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerych1984 committed Dec 22, 2021
1 parent fe97ddd commit 32a0bc0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/Filter/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function getOperator(): string
*/
private static function isEmpty($value): bool
{
return $value !== null && $value !== '';
return $value === null || $value === '';
}

public function toArray(): array
Expand All @@ -39,16 +39,18 @@ public function toArray(): array
$value = $this->value;
$start = array_shift($value);
$end = array_pop($value);
$isStartEmpty = self::isEmpty($start);
$isEndEmpty = self::isEmpty($end);

if (!self::isEmpty($start) && !self::isEmpty($end)) {
if (!$isStartEmpty && !$isEndEmpty) {
return [self::getOperator(), $this->column, $start, $end];
}

if (!self::isEmpty($start)) {
if (!$isStartEmpty) {
return [GreaterThanOrEqual::getOperator(), $this->column, $start];
}

if (!self::isEmpty($end)) {
if (!$isEndEmpty) {
return [LessThanOrEqual::getOperator(), $this->column, $end];
}

Expand Down
29 changes: 29 additions & 0 deletions src/ParameterizedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db;

trait ParameterizedTrait
{
private ?string $paramName = null;

public function withParamName(?string $name): self
{
$new = clone $this;
$new->paramName = $name;

return $new;
}

public function getParamName(): string
{
if (!empty($this->paramName)) {
return $this->paramName;
}

$explode = explode('\\', __CLASS__);

return $explode[count($explode) - 1];
}
}
4 changes: 2 additions & 2 deletions src/QueryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class QueryDataReader implements DataReaderInterface
private ?int $count = null;
private ?array $data = null;

private array $filterProcessors = [];
protected array $filterProcessors = [];

public function __construct(Query $query)
{
Expand Down Expand Up @@ -193,7 +193,7 @@ public function withFilter(FilterInterface $filter): self
/**
* @psalm-mutation-free
*/
public function withFilterProcessors(FilterProcessorInterface ...$filterProcessors): self
public function withFilterProcessors(FilterProcessorInterface ...$filterProcessors): static
{
$new = clone $this;

Expand Down
12 changes: 12 additions & 0 deletions tests/DataReaderFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Yiisoft\Data\Db\Processor\Like as ProcessorLike;
use Yiisoft\Data\Db\Processor\In as ProcessorIn;
use Yiisoft\Data\Db\Processor\QueryProcessorInterface;
use Yiisoft\Data\Db\Filter\Between as FilterBetween;

class DataReaderFilterTest extends TestCase
{
Expand Down Expand Up @@ -186,4 +187,15 @@ public function testNotFilter()

$this->assertSame(['IS not', 'column', null], $notNull->toArray());
}

public function testBetweenFilter()
{
$startEnd = new FilterBetween('column', [100, 200]);
$start = new FilterBetween('column', [150, null]);
$end = new FilterBetween('column', [null, 300]);

$this->assertSame([FilterBetween::getOperator(), 'column', 100, 200], $startEnd->toArray());
$this->assertSame([FilterGreaterThanOrEqual::getOperator(), 'column', 150], $start->toArray());
$this->assertSame([FilterLessThanOrEqual::getOperator(), 'column', 300], $end->toArray());
}
}

0 comments on commit 32a0bc0

Please sign in to comment.