Skip to content

Commit

Permalink
Moved Criteria, SortClauses and CriterionMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Sep 24, 2024
1 parent f65c108 commit 1fc0d0a
Show file tree
Hide file tree
Showing 12 changed files with 481 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/bundle/Resources/config/services/persistence.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Ibexa\CoreSearch\Repository\CriterionMapper:
arguments:
$mappers: !tagged_iterator ibexa.core_search.criterion_mapper
111 changes: 111 additions & 0 deletions src/contracts/Values/Query/AbstractCriterionQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?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\Values\Query;

use Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface;

/**
* @template TSortClause of \Ibexa\Contracts\CoreSearch\Values\Query\AbstractSortClause
* @template TCriterion of \Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface
*/
abstract class AbstractCriterionQuery
{
public const DEFAULT_LIMIT = 25;

/** @var TCriterion|null */
private ?CriterionInterface $query;

/** @var TSortClause[] */
private array $sortClauses;

private ?int $limit;

private int $offset;

/**
* @param TSortClause[]|null $sortClauses
* @param TCriterion|null $query
*/
public function __construct(
?CriterionInterface $query = null,
?array $sortClauses = [],
?int $limit = self::DEFAULT_LIMIT,
int $offset = 0
) {
$this->query = $query;
$this->sortClauses = $sortClauses ?? [];
$this->offset = $offset;
$this->limit = $limit;
}

/**
* @param TCriterion|null $criterion
*/
final public function setQuery(?CriterionInterface $criterion): void
{
$this->query = $criterion;
}

/**
* @return TCriterion|null
*/
final public function getQuery(): ?CriterionInterface
{
return $this->query;
}

final public function hasQuery(): bool
{
return $this->query !== null;
}

final public function getOffset(): int
{
return $this->offset;
}

final public function setOffset(int $offset): void
{
$this->offset = $offset;
}

final public function getLimit(): ?int
{
return $this->limit;
}

final public function setLimit(?int $limit): void
{
$this->limit = $limit;
}

/**
* @return TSortClause[]
*/
final public function getSortClauses(): array
{
return $this->sortClauses;
}

/**
* @phpstan-param TSortClause $sortClause
*/
final public function addSortClause(AbstractSortClause $sortClause): void
{
$this->sortClauses[] = $sortClause;
}

/**
* @phpstan-param TSortClause[] $sortClauses
*/
final public function setSortClauses(array $sortClauses): void
{
$this->sortClauses = $sortClauses;
}
}
59 changes: 59 additions & 0 deletions src/contracts/Values/Query/AbstractSortClause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Values\Query;

use InvalidArgumentException;

abstract class AbstractSortClause
{
/** @final */
public const SORT_ASC = SortDirection::ASC;

/** @final */
public const SORT_DESC = SortDirection::DESC;

/**
* Sort direction.
*
* @phpstan-var SortDirection::*
*/
private string $direction = SortDirection::ASC;

/**
* Constructs a new SortClause on $sortTarget in direction $sortDirection.
*
* @param string $sortDirection one of SortDirection::ASC or SortDirection::DESC
*/
public function __construct(
string $sortDirection = self::SORT_ASC
) {
$this->setDirection($sortDirection);
}

final public function getDirection(): string
{
return $this->direction;
}

/**
* @throws \InvalidArgumentException if the given sort direction is invalid
*/
final public function setDirection(string $direction): void
{
if (!SortDirection::isValid($direction)) {
throw new InvalidArgumentException(sprintf(
'Sort direction must be one of %1$s::ASC or %1$s::DESC',
SortDirection::class,
));
}

/** @var SortDirection::* $direction */
$this->direction = $direction;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Values\Query\Criterion;

abstract class AbstractCompositeCriterion implements CriterionInterface
{
/** @var array<\Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface> */
private array $criteria;

public function __construct(CriterionInterface ...$criteria)
{
$this->criteria = $criteria;
}

public function add(CriterionInterface ...$criteria): void
{
$this->setCriteria(
...$this->criteria,
...$criteria,
);
}

public function remove(CriterionInterface ...$criteria): void
{
$this->setCriteria(...array_filter(
$this->criteria,
static function (CriterionInterface $criterion) use ($criteria): bool {
return !in_array($criterion, $criteria, true);
},
));
}

public function setCriteria(CriterionInterface ...$criteria): void
{
$this->criteria = $criteria;
}

/**
* @return array<\Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface>
*/
final public function getCriteria(): array
{
return $this->criteria;
}
}
13 changes: 13 additions & 0 deletions src/contracts/Values/Query/Criterion/CriterionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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\Values\Query\Criterion;

interface CriterionInterface
{
}
85 changes: 85 additions & 0 deletions src/contracts/Values/Query/Criterion/FieldValueCriterion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\Values\Query\Criterion;

class FieldValueCriterion implements CriterionInterface
{
/** @final */
public const COMPARISON_EQ = '=';
/** @final */
public const COMPARISON_NEQ = '<>';
/** @final */
public const COMPARISON_LT = '<';
/** @final */
public const COMPARISON_LTE = '<=';
/** @final */
public const COMPARISON_GT = '>';
/** @final */
public const COMPARISON_GTE = '>=';
/** @final */
public const COMPARISON_IN = 'IN';
/** @final */
public const COMPARISON_NIN = 'NIN';
/** @final */
public const COMPARISON_CONTAINS = 'CONTAINS';
/** @final */
public const COMPARISON_MEMBER_OF = 'MEMBER_OF';
/** @final */
public const COMPARISON_STARTS_WITH = 'STARTS_WITH';
/** @final */
public const COMPARISON_ENDS_WITH = 'ENDS_WITH';

private string $field;

/** @var mixed */
private $value;

private string $operator;

/**
* @param mixed $value
*/
public function __construct(string $field, $value, ?string $operator = null)
{
$this->field = $field;
$this->value = $value;
$this->operator = $operator ?? (is_array($value) ? self::COMPARISON_IN : self::COMPARISON_EQ);
}

public function getField(): string
{
return $this->field;
}

/**
* @param mixed $value
*/
public function setValue($value): void
{
$this->value = $value;
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}

public function setOperator(string $operator): void
{
$this->operator = $operator;
}

public function getOperator(): string
{
return $this->operator;
}
}
13 changes: 13 additions & 0 deletions src/contracts/Values/Query/Criterion/LogicalAnd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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\Values\Query\Criterion;

class LogicalAnd extends AbstractCompositeCriterion
{
}
13 changes: 13 additions & 0 deletions src/contracts/Values/Query/Criterion/LogicalOr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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\Values\Query\Criterion;

class LogicalOr extends AbstractCompositeCriterion
{
}
26 changes: 26 additions & 0 deletions src/contracts/Values/Query/CriterionMapperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Values\Query;

use Doctrine\Common\Collections\Expr\Expression;
use Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface;
use Ibexa\CoreSearch\Repository\CriterionMapper;

Check failure on line 13 in src/contracts/Values/Query/CriterionMapperInterface.php

View workflow job for this annotation

GitHub Actions / Deptrac

Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface must not depend on Ibexa\CoreSearch\Repository\CriterionMapper (Contracts on Library)

/**
* @template C of \Ibexa\Contracts\CoreSearch\Values\Query\Criterion\CriterionInterface
*/
interface CriterionMapperInterface
{
public function canHandle(CriterionInterface $criterion): bool;

/**
* @param C $criterion
*/
public function handle(CriterionInterface $criterion, CriterionMapper $mapper): Expression;

Check failure on line 25 in src/contracts/Values/Query/CriterionMapperInterface.php

View workflow job for this annotation

GitHub Actions / Deptrac

Ibexa\Contracts\CoreSearch\Values\Query\CriterionMapperInterface must not depend on Ibexa\CoreSearch\Repository\CriterionMapper (Contracts on Library)
}
Loading

0 comments on commit 1fc0d0a

Please sign in to comment.