generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Big refactoring + Add support filters from Yii Data #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8708d4a
all, any
vjik 891e48c
Apply fixes from StyleCI
StyleCIBot e86142c
refactor
vjik 9481e7c
improve
vjik 3435b93
refactor
vjik 2fb817c
refactor
vjik 5166b39
refactor
vjik 1f5bd2a
remove ParameterizedTrait
vjik dd2edc6
refactor
vjik c3f0872
refactor
vjik 0db9084
refactor
vjik e56c3dd
improve
vjik fe61e1a
Merge remote-tracking branch 'origin/filters' into filters
vjik 655a511
Apply fixes from StyleCI
StyleCIBot 2a3b03a
Apply Rector changes (CI)
vjik d67ba56
phpdoc
vjik 4809681
Update src/CriteriaHandler.php
vjik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,132 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db; | ||
|
||
use LogicException; | ||
use Yiisoft\Data\Db\FilterHandler\AllHandler; | ||
use Yiisoft\Data\Db\FilterHandler\AnyHandler; | ||
use Yiisoft\Data\Db\FilterHandler\BetweenHandler; | ||
use Yiisoft\Data\Db\FilterHandler\Context; | ||
use Yiisoft\Data\Db\FilterHandler\EqualsEmptyHandler; | ||
use Yiisoft\Data\Db\FilterHandler\EqualsHandler; | ||
use Yiisoft\Data\Db\FilterHandler\EqualsNullHandler; | ||
use Yiisoft\Data\Db\FilterHandler\ExistsHandler; | ||
use Yiisoft\Data\Db\FilterHandler\GreaterThanHandler; | ||
use Yiisoft\Data\Db\FilterHandler\GreaterThanOrEqualHandler; | ||
use Yiisoft\Data\Db\FilterHandler\InHandler; | ||
use Yiisoft\Data\Db\FilterHandler\LessThanHandler; | ||
use Yiisoft\Data\Db\FilterHandler\LessThanOrEqualHandler; | ||
use Yiisoft\Data\Db\FilterHandler\LikeHandler; | ||
use Yiisoft\Data\Db\FilterHandler\NotHandler; | ||
use Yiisoft\Data\Db\FilterHandler\QueryHandlerInterface; | ||
use Yiisoft\Data\Reader\FilterHandlerInterface; | ||
use Yiisoft\Db\Query\QueryPartsInterface; | ||
|
||
/** | ||
* `CriteriaHandler` processes filter criteria array from {@see FilterInterface::toCriteriaArray()} into condition array | ||
* that is used in {@see QueryPartsInterface::andWhere()} and {@see QueryPartsInterface::andHaving()}. | ||
*/ | ||
final class CriteriaHandler | ||
{ | ||
private Context $context; | ||
|
||
/** | ||
* @psalm-var array<string, QueryHandlerInterface> | ||
*/ | ||
private array $handlers; | ||
|
||
/** | ||
* @param QueryHandlerInterface[]|null $handlers | ||
* @param ValueNormalizerInterface|null $valueNormalizer | ||
*/ | ||
public function __construct( | ||
?array $handlers = null, | ||
ValueNormalizerInterface $valueNormalizer = null, | ||
) { | ||
if (empty($handlers)) { | ||
$handlers = [ | ||
new AllHandler(), | ||
new AnyHandler(), | ||
new EqualsHandler(), | ||
new GreaterThanHandler(), | ||
new GreaterThanOrEqualHandler(), | ||
new LessThanHandler(), | ||
new LessThanOrEqualHandler(), | ||
new LikeHandler(), | ||
new InHandler(), | ||
new ExistsHandler(), | ||
new NotHandler(), | ||
new BetweenHandler(), | ||
new EqualsNullHandler(), | ||
new EqualsEmptyHandler(), | ||
]; | ||
} | ||
|
||
$this->handlers = $this->prepareHandlers($handlers); | ||
$this->context = new Context($this, $valueNormalizer ?? new ValueNormalizer()); | ||
} | ||
|
||
public function withFilterHandlers(FilterHandlerInterface ...$handlers): self | ||
{ | ||
foreach ($handlers as $handler) { | ||
if (!$handler instanceof QueryHandlerInterface) { | ||
throw new LogicException( | ||
sprintf( | ||
'Filter handler must implement "%s".', | ||
QueryHandlerInterface::class, | ||
) | ||
); | ||
} | ||
} | ||
/** @var QueryHandlerInterface[] $handlers */ | ||
|
||
$new = clone $this; | ||
$new->handlers = array_merge( | ||
$this->handlers, | ||
$this->prepareHandlers($handlers), | ||
); | ||
return $new; | ||
} | ||
|
||
public function handle(array $criteria): ?array | ||
{ | ||
if (!isset($criteria[0])) { | ||
throw new LogicException('Incorrect criteria array.'); | ||
} | ||
|
||
$operator = $criteria[0]; | ||
if (!is_string($operator)) { | ||
throw new LogicException('Criteria operator must be a string.'); | ||
} | ||
|
||
$operands = array_slice($criteria, 1); | ||
|
||
return $this->getHandlerByOperator($operator)->getCondition($operands, $this->context); | ||
} | ||
|
||
private function getHandlerByOperator(string $operator): QueryHandlerInterface | ||
{ | ||
if (!isset($this->handlers[$operator])) { | ||
throw new LogicException(sprintf('Operator "%s" is not supported', $operator)); | ||
} | ||
|
||
return $this->handlers[$operator]; | ||
} | ||
|
||
/** | ||
* @param QueryHandlerInterface[] $handlers | ||
* | ||
* @return QueryHandlerInterface[] | ||
* @psalm-return array<string, QueryHandlerInterface> | ||
*/ | ||
private function prepareHandlers(array $handlers): array | ||
{ | ||
$result = []; | ||
foreach ($handlers as $handler) { | ||
$result[$handler->getOperator()] = $handler; | ||
} | ||
return $result; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.