-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
187 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Class Ibexa\\\\ProductCatalog\\\\Local\\\\Persistence\\\\Legacy\\\\Common\\\\CriterionMapper\\\\AbstractCompositeCriterionMapper not found\\.$#" | ||
count: 1 | ||
path: src/contracts/Persistence/CriterionMapper/AbstractCompositeCriterionMapper.php | ||
|
||
- | ||
message: "#^Class Ibexa\\\\ProductCatalog\\\\Local\\\\Persistence\\\\Legacy\\\\Common\\\\CriterionMapper\\\\AbstractFieldCriterionMapper not found\\.$#" | ||
count: 1 | ||
path: src/contracts/Persistence/CriterionMapper/AbstractFieldCriterionMapper.php | ||
|
||
- | ||
message: "#^Class Ibexa\\\\Contracts\\\\ProductCatalog\\\\Values\\\\Common\\\\Query\\\\Criterion\\\\CriterionInterface not found\\.$#" | ||
count: 1 | ||
path: src/contracts/Values/Query/Criterion/CriterionInterface.php | ||
|
||
- | ||
message: "#^Class Ibexa\\\\Contracts\\\\ProductCatalog\\\\Values\\\\Common\\\\Query\\\\Criterion\\\\FieldValueCriterion not found\\.$#" | ||
count: 1 | ||
path: src/contracts/Values/Query/Criterion/FieldValueCriterion.php |
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
63 changes: 63 additions & 0 deletions
63
src/contracts/Persistence/CriterionMapper/AbstractCompositeCriterionMapper.php
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,63 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\CoreSearch\Persistence\CriterionMapper; | ||
|
||
use Doctrine\Common\Collections\Expr\CompositeExpression; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapper; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface; | ||
|
||
/** | ||
* @implements \Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface< | ||
* \Ibexa\Contracts\CoreSearch\Values\Query\Criterion\AbstractCompositeCriterion, | ||
* > | ||
*/ | ||
abstract class AbstractCompositeCriterionMapper implements CriterionMapperInterface | ||
{ | ||
/** | ||
* @return class-string<\Ibexa\Contracts\CoreSearch\Values\Query\Criterion\AbstractCompositeCriterion> | ||
*/ | ||
abstract protected function getHandledClass(): string; | ||
|
||
/** | ||
* @phpstan-return \Doctrine\Common\Collections\Expr\CompositeExpression::TYPE_* | ||
*/ | ||
abstract protected function getType(): string; | ||
|
||
final public function canHandle(CriterionInterface $criterion): bool | ||
{ | ||
$handledClass = $this->getHandledClass(); | ||
|
||
return $criterion instanceof $handledClass; | ||
} | ||
|
||
final public function handle(CriterionInterface $criterion, CriterionMapper $mapper): CompositeExpression | ||
{ | ||
$expressions = $this->getExpressions($criterion->getCriteria(), $mapper); | ||
|
||
return new CompositeExpression($this->getType(), $expressions); | ||
} | ||
|
||
/** | ||
* @param iterable<\Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface> $criteria | ||
* | ||
* @return array<\Doctrine\Common\Collections\Expr\Expression> | ||
*/ | ||
private function getExpressions(iterable $criteria, CriterionMapper $mapper): array | ||
{ | ||
$expressions = []; | ||
foreach ($criteria as $criterion) { | ||
$expressions[] = $mapper->handle($criterion); | ||
} | ||
|
||
return $expressions; | ||
} | ||
} | ||
|
||
class_alias(AbstractCompositeCriterionMapper::class, \Ibexa\ProductCatalog\Local\Persistence\Legacy\Common\CriterionMapper\AbstractCompositeCriterionMapper::class); |
91 changes: 91 additions & 0 deletions
91
src/contracts/Persistence/CriterionMapper/AbstractFieldCriterionMapper.php
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,91 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\CoreSearch\Persistence\CriterionMapper; | ||
|
||
use Doctrine\Common\Collections\Expr\Comparison; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\Criterion\FieldValueCriterion; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapper; | ||
use Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface; | ||
use LogicException; | ||
|
||
/** | ||
* @template T of \Ibexa\Contracts\CoreSearch\Values\Query\Criterion\FieldValueCriterion | ||
* | ||
* @template-implements \Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface<T> | ||
*/ | ||
abstract class AbstractFieldCriterionMapper implements CriterionMapperInterface | ||
{ | ||
/** | ||
* @var array< | ||
* \Ibexa\Contracts\CoreSearch\Values\Query\Criterion\FieldValueCriterion::COMPARISON_*, | ||
* \Doctrine\Common\Collections\Expr\Comparison::* | ||
* > | ||
*/ | ||
private static array $comparisonMap = [ | ||
FieldValueCriterion::COMPARISON_EQ => Comparison::EQ, | ||
FieldValueCriterion::COMPARISON_NEQ => Comparison::NEQ, | ||
FieldValueCriterion::COMPARISON_LT => Comparison::LT, | ||
FieldValueCriterion::COMPARISON_LTE => Comparison::LTE, | ||
FieldValueCriterion::COMPARISON_GT => Comparison::GT, | ||
FieldValueCriterion::COMPARISON_GTE => Comparison::GTE, | ||
FieldValueCriterion::COMPARISON_IN => Comparison::IN, | ||
FieldValueCriterion::COMPARISON_NIN => Comparison::NIN, | ||
FieldValueCriterion::COMPARISON_CONTAINS => Comparison::CONTAINS, | ||
FieldValueCriterion::COMPARISON_MEMBER_OF => Comparison::MEMBER_OF, | ||
FieldValueCriterion::COMPARISON_STARTS_WITH => Comparison::STARTS_WITH, | ||
FieldValueCriterion::COMPARISON_ENDS_WITH => Comparison::ENDS_WITH, | ||
]; | ||
|
||
/** | ||
* @phpstan-param T $criterion | ||
*/ | ||
final public function handle(CriterionInterface $criterion, CriterionMapper $mapper): Comparison | ||
{ | ||
assert($criterion instanceof FieldValueCriterion); | ||
|
||
return new Comparison( | ||
$this->getComparisonField($criterion), | ||
$this->getComparisonOperator($criterion), | ||
$this->getComparisonValue($criterion) | ||
); | ||
} | ||
|
||
protected function getComparisonField(FieldValueCriterion $criterion): string | ||
{ | ||
return $criterion->getField(); | ||
} | ||
|
||
/** | ||
* @phpstan-return \Doctrine\Common\Collections\Expr\Comparison::* | ||
*/ | ||
protected function getComparisonOperator(FieldValueCriterion $criterion): string | ||
{ | ||
$operator = $criterion->getOperator(); | ||
if (isset(self::$comparisonMap[$operator])) { | ||
return self::$comparisonMap[$operator]; | ||
} | ||
|
||
throw new LogicException(sprintf( | ||
'Unable to map %s operator %s to a valid DBAL operator', | ||
get_class($criterion), | ||
$operator, | ||
)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
protected function getComparisonValue(FieldValueCriterion $criterion) | ||
{ | ||
return $criterion->getValue(); | ||
} | ||
} | ||
|
||
class_alias(AbstractFieldCriterionMapper::class, \Ibexa\ProductCatalog\Local\Persistence\Legacy\Common\CriterionMapper\AbstractFieldCriterionMapper::class); |
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
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