Skip to content

Commit

Permalink
Merge branch '4.6'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Oct 12, 2024
2 parents 44c240c + 3b6ddf6 commit 0ab4552
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 2 deletions.
21 changes: 21 additions & 0 deletions phpstan-baseline.neon
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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- phpstan-baseline.neon

parameters:
level: 8
Expand Down
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);
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);
2 changes: 2 additions & 0 deletions src/contracts/Values/Query/Criterion/CriterionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
interface CriterionInterface
{
}

class_alias(CriterionInterface::class, \Ibexa\Contracts\ProductCatalog\Values\Common\Query\Criterion\CriterionInterface::class);
2 changes: 2 additions & 0 deletions src/contracts/Values/Query/Criterion/FieldValueCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ public function getOperator(): string
return $this->operator;
}
}

class_alias(FieldValueCriterion::class, \Ibexa\Contracts\ProductCatalog\Values\Common\Query\Criterion\FieldValueCriterion::class);
4 changes: 3 additions & 1 deletion src/contracts/Values/Query/CriterionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

/**
* Converts Criterion instances into objects that underlying Handler can understand.
*
* @final
*/
final class CriterionMapper
class CriterionMapper
{
/**
* @var iterable<\Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface<
Expand Down
5 changes: 4 additions & 1 deletion src/contracts/Values/Query/SortDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

namespace Ibexa\Contracts\CoreSearch\Values\Query;

final class SortDirection
/**
* @final
*/
class SortDirection
{
public const ASC = 'ascending';
public const DESC = 'descending';
Expand Down

0 comments on commit 0ab4552

Please sign in to comment.