-
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.
Moved Criteria, SortClauses and CriterionMapper
- Loading branch information
1 parent
f65c108
commit 1fc0d0a
Showing
12 changed files
with
481 additions
and
0 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,9 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
Ibexa\CoreSearch\Repository\CriterionMapper: | ||
arguments: | ||
$mappers: !tagged_iterator ibexa.core_search.criterion_mapper |
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/contracts/Values/Query/Criterion/AbstractCompositeCriterion.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,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
13
src/contracts/Values/Query/Criterion/CriterionInterface.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,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
85
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
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; | ||
} | ||
} |
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,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 | ||
{ | ||
} |
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,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 | ||
{ | ||
} |
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,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; | ||
|
||
/** | ||
* @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; | ||
} |
Oops, something went wrong.