Skip to content

Commit

Permalink
New filters, processors and fixes (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerych1984 committed Oct 19, 2021
1 parent ce9c392 commit 5953675
Show file tree
Hide file tree
Showing 24 changed files with 537 additions and 45 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<br>
</p>

[![Latest Stable Version](https://poser.pugx.org/yiisoft/_____/v/stable.png)](https://packagist.org/packages/yiisoft/_____)
[![Total Downloads](https://poser.pugx.org/yiisoft/_____/downloads.png)](https://packagist.org/packages/yiisoft/_____)
[![Build status](https://github.com/yiisoft/_____/workflows/build/badge.svg)](https://github.com/yiisoft/_____/actions?query=workflow%3Abuild)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiisoft/_____/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/_____/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/yiisoft/_____/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/_____/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/yiisoft/data-db/v/stable.png)](https://packagist.org/packages/yiisoft/data-db)
[![Total Downloads](https://poser.pugx.org/yiisoft/data-db/downloads.png)](https://packagist.org/packages/yiisoft/data-db)
[![Build status](https://github.com/yiisoft/data-db/workflows/build/badge.svg)](https://github.com/yiisoft/data-db/actions?query=workflow%3Abuild)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiisoft/data-db/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/data-db/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/yiisoft/data-db/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/data-db/?branch=master)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2F_____%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/_____/master)
[![static analysis](https://github.com/yiisoft/_____/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/_____/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/_____/coverage.svg)](https://shepherd.dev/github/yiisoft/_____)
[![static analysis](https://github.com/yiisoft/data-db/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/data-db/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/data-db/coverage.svg)](https://shepherd.dev/github/yiisoft/data-db)

The package provides `Yiisoft\Db\Query\Query` bindings for generic data abstractions.

Expand Down
2 changes: 1 addition & 1 deletion src/Filter/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Data\Reader\Filter\All as FilterAll;

class All extends GroupFilter
final class All extends GroupFilter
{
public static function getOperator(): string
{
Expand Down
15 changes: 15 additions & 0 deletions src/Filter/Any.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Data\Reader\Filter\Any as FilterAny;

final class Any extends GroupFilter
{
public static function getOperator(): string
{
return FilterAny::getOperator();
}
}
12 changes: 11 additions & 1 deletion src/Filter/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Db\Query\Query;
use Yiisoft\Data\Reader\Filter\Equals as FilterEquals;

class Equals extends CompareFilter
final class Equals extends CompareFilter
{
public static function getOperator(): string
{
return FilterEquals::getOperator();
}

public function toArray(): array
{
if (is_array($this->value) || $this->value instanceof Query) {
return [In::getOperator(), $this->column, $this->value];
}

return parent::toArray();
}
}
20 changes: 20 additions & 0 deletions src/Filter/Exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Db\Query\Query;

final class Exists extends CompareFilter
{
public function __construct(string $column, ?Query $value, ?string $table = null)
{
parent::__construct($column, $value, $table);
}

public static function getOperator(): string
{
return 'exists';
}
}
15 changes: 15 additions & 0 deletions src/Filter/GreaterThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Data\Reader\Filter\GreaterThan as FilterGreaterThan;

final class GreaterThan extends CompareFilter
{
public static function getOperator(): string
{
return FilterGreaterThan::getOperator();
}
}
15 changes: 15 additions & 0 deletions src/Filter/GreaterThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Data\Reader\Filter\GreaterThanOrEqual as FilterGreaterThanOrEqual;

final class GreaterThanOrEqual extends CompareFilter
{
public static function getOperator(): string
{
return FilterGreaterThanOrEqual::getOperator();
}
}
30 changes: 30 additions & 0 deletions src/Filter/In.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use InvalidArgumentException;
use Yiisoft\Data\Reader\Filter\In as FilterIn;
use Yiisoft\Db\Query\Query;
use function is_array;

final class In extends CompareFilter
{
/**
* @param mixed $value
*/
public function __construct(string $column, $value, ?string $table = null)
{
if ($value !== null && !is_array($value) && $value instanceof Query === false) {
throw new InvalidArgumentException('value must be null, array or ' . Query::class . ' instance');
}

parent::__construct($column, $value, $table);
}

public static function getOperator(): string
{
return FilterIn::getOperator();
}
}
15 changes: 15 additions & 0 deletions src/Filter/LessThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Data\Reader\Filter\LessThan as FilterLessThan;

final class LessThan extends CompareFilter
{
public static function getOperator(): string
{
return FilterLessThan::getOperator();
}
}
15 changes: 15 additions & 0 deletions src/Filter/LessThanOrEqual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Data\Reader\Filter\LessThanOrEqual as FilterLessThanOrEqual;

final class LessThanOrEqual extends CompareFilter
{
public static function getOperator(): string
{
return FilterLessThanOrEqual::getOperator();
}
}
86 changes: 86 additions & 0 deletions src/Filter/Like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Filter;

use Yiisoft\Data\Reader\Filter\Like as FilterLike;

class Like extends CompareFilter
{
private bool $start = true;
private bool $end = true;

public function __construct(string $column, ?string $value, ?string $table = null)
{
parent::__construct($column, $value, $table);
}

public static function getOperator(): string
{
return FilterLike::getOperator();
}

public function withStart(): self
{
if ($this->start === true) {
return $this;
}

$new = clone $this;
$new->start = true;

return $new;
}

public function withoutStart(): self
{
if ($this->start === false) {
return $this;
}

$new = clone $this;
$new->start = false;

return $new;
}

public function withEnd(): self
{
if ($this->end === true) {
return $this;
}

$new = clone $this;
$new->end = true;

return $new;
}

public function withoutEnd(): self
{
if ($this->end === false) {
return $this;
}

$new = clone $this;
$new->end = false;

return $new;
}

public function toArray(): array
{
if ($this->value === null || ($this->start && $this->end)) {
return parent::toArray();
}

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

$value = $this->start ? $this->value . '%' : '%' . $this->value;

return [self::getOperator(), $this->column, $value, false];
}
}
2 changes: 1 addition & 1 deletion src/Processor/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Yiisoft\Data\Db\Processor;

use Yiisoft\Data\Reader\Filter\All as FilterAll;
use Yiisoft\Data\Db\Filter\All as FilterAll;
use Yiisoft\Data\Reader\Filter\FilterInterface;
use Yiisoft\Db\Query\Query;

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

declare(strict_types=1);

namespace Yiisoft\Data\Db\Processor;

use Yiisoft\Data\Db\Filter\Any as FilterAny;
use Yiisoft\Data\Reader\Filter\FilterInterface;
use Yiisoft\Db\Query\Query;

class Any implements QueryProcessorInterface
{
public function getOperator(): string
{
return FilterAny::getOperator();
}

public function apply(Query $query, FilterInterface $filter): Query
{
return $query->orWhere($filter->toArray());
}
}
24 changes: 24 additions & 0 deletions src/Processor/CompareProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Processor;

use Yiisoft\Data\Reader\Filter\FilterInterface;
use Yiisoft\Db\Query\Query;

use function count;

abstract class CompareProcessor implements QueryProcessorInterface
{
public function apply(Query $query, FilterInterface $filter): Query
{
$array = $filter->toArray();

if (count($array) === 0) {
return $query;
}

return $query->andWhere($array);
}
}
17 changes: 2 additions & 15 deletions src/Processor/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,12 @@

namespace Yiisoft\Data\Db\Processor;

use Yiisoft\Data\Reader\Filter\Equals as FilterEquals;
use Yiisoft\Data\Reader\Filter\FilterInterface;
use Yiisoft\Db\Query\Query;
use Yiisoft\Data\Db\Filter\Equals as FilterEquals;

class Equals implements QueryProcessorInterface
class Equals extends CompareProcessor
{
public function getOperator(): string
{
return FilterEquals::getOperator();
}

public function apply(Query $query, FilterInterface $filter): Query
{
$array = $filter->toArray();

if (count($array) === 0) {
return $query;
}

return $query->andWhere($array);
}
}
15 changes: 15 additions & 0 deletions src/Processor/Exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Processor;

use Yiisoft\Data\Db\Filter\Exists as FilterExists;

final class Exists extends CompareProcessor
{
public function getOperator(): string
{
return FilterExists::getOperator();
}
}
15 changes: 15 additions & 0 deletions src/Processor/GreaterThan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Processor;

use Yiisoft\Data\Db\Filter\GreaterThan as FilterGreaterThan;

class GreaterThan extends CompareProcessor
{
public function getOperator(): string
{
return FilterGreaterThan::getOperator();
}
}

0 comments on commit 5953675

Please sign in to comment.