generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New filters, processors and fixes (#10)
- Loading branch information
1 parent
ce9c392
commit 5953675
Showing
24 changed files
with
537 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.