Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
/ criteria Public archive

Commit

Permalink
Merge pull request #1 from spaceonfire/rename-adapter-to-bridge
Browse files Browse the repository at this point in the history
Rename Adapter namespace to Bridge
  • Loading branch information
tntrex authored Sep 27, 2020
2 parents bbad846 + 4a47ba8 commit 7bc2c1c
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 256 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Nothing
-->

## [1.1.0] - 2020-09-27
### Deprecated
- Namespace `spaceonfire\Criteria\Adapter` renamed to `spaceonfire\Criteria\Bridge`.
Class aliases provided for backwards compatibility, but will be removed in next major release.

## [1.0.0] - 2020-05-27
### Added
- First stable release
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "1.1-dev"
}
},
"config": {
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ parameters:
- src/Expression/ExpressionFactory.php
- src/Expression/AbstractExpressionDecorator.php
- '/^Method (.*) should return static\((.*)\) but returns (.*)\.$/'
-
message: '/^If condition is always false\.$/'
path: src/Adapter/*
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
<exclude>
<directory>src/Adapter/</directory>
</exclude>
</whitelist>
</filter>
<logging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,19 @@

namespace spaceonfire\Criteria\Adapter\DoctrineCollections;

use Doctrine\Common\Collections\Criteria as DoctrineCriteria;
use spaceonfire\Criteria\Criteria;
use spaceonfire\Criteria\CriteriaInterface;
use function class_alias;

class DoctrineCollectionsCriteriaConverter
{
class_alias(
\spaceonfire\Criteria\Bridge\DoctrineCollections\DoctrineCollectionsCriteriaConverter::class,
__NAMESPACE__ . '\DoctrineCollectionsCriteriaConverter'
);

if (false) {
/**
* Converts Doctrine criteria to spaceonfire criteria
* @param DoctrineCriteria $doctrineCriteria
* @param string $comparisonMethod
* @return CriteriaInterface
* @deprecated Will be dropped in next major release.
* Use \spaceonfire\Criteria\Bridge\DoctrineCollections\DoctrineCollectionsCriteriaConverter instead.
*/
public function convert(
DoctrineCriteria $doctrineCriteria,
string $comparisonMethod = 'property'
): CriteriaInterface {
$criteria = new Criteria();

if (null !== $doctrineExpression = $doctrineCriteria->getWhereExpression()) {
$expression = (new DoctrineCollectionsExpressionConverter($comparisonMethod))
->dispatch($doctrineExpression);

$criteria->where($expression);
}

$orderBy = array_map(static function (string $ordering): int {
return $ordering === DoctrineCriteria::ASC ? SORT_ASC : SORT_DESC;
}, $doctrineCriteria->getOrderings());

$criteria->orderBy($orderBy);

if (null !== $offset = $doctrineCriteria->getFirstResult()) {
$criteria->offset($offset);
}

if (null !== $limit = $doctrineCriteria->getMaxResults()) {
$criteria->limit($limit);
}

return $criteria;
class DoctrineCollectionsCriteriaConverter extends \spaceonfire\Criteria\Bridge\DoctrineCollections\DoctrineCollectionsCriteriaConverter
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,139 +4,20 @@

namespace spaceonfire\Criteria\Adapter\DoctrineCollections;

use Doctrine\Common\Collections\Expr\Comparison;
use Doctrine\Common\Collections\Expr\CompositeExpression;
use Doctrine\Common\Collections\Expr\ExpressionVisitor;
use Doctrine\Common\Collections\Expr\Value;
use InvalidArgumentException;
use spaceonfire\Criteria\Criteria;
use spaceonfire\Criteria\Expression\ExpressionFactory;
use Webmozart\Assert\Assert;
use Webmozart\Expression\Expression;
use function class_alias;

/**
* Converts Expressions from Doctrine collections to webmozart expressions
* @package spaceonfire\Criteria\Adapter\DoctrineCollections
*/
class DoctrineCollectionsExpressionConverter extends ExpressionVisitor
{
/**
* @var string
*/
private $comparisonMethod;

/**
* DoctrineCollectionsExpressionConverter constructor.
* @param string $comparisonMethod `property` or `key`
*/
public function __construct(string $comparisonMethod = 'property')
{
Assert::oneOf($comparisonMethod, ['property', 'key']);
$this->comparisonMethod = $comparisonMethod;
}

private function getExpressionFactory(): ExpressionFactory
{
return Criteria::expr();
}

/**
* @inheritDoc
* @return Expression
*/
public function walkCompositeExpression(CompositeExpression $expr): Expression
{
$expressionList = [];
foreach ($expr->getExpressionList() as $child) {
$expressionList[] = $this->dispatch($child);
}

switch ($expr->getType()) {
case CompositeExpression::TYPE_AND:
return $this->getExpressionFactory()->andX($expressionList);

case CompositeExpression::TYPE_OR:
return $this->getExpressionFactory()->orX($expressionList);

default:
throw new InvalidArgumentException(sprintf(
'Unknown composite expression type: "%s"',
$expr->getType()
));
}
}

/**
* @inheritDoc
* @return Expression
*/
public function walkComparison(Comparison $comparison): Expression
{
$field = $comparison->getField();
$value = $this->walkValue($comparison->getValue());

switch ($comparison->getOperator()) {
case Comparison::EQ:
case Comparison::IS:
$innerExpression = $this->getExpressionFactory()->same($value);
break;

case Comparison::NEQ:
$innerExpression = $this->getExpressionFactory()->notSame($value);
break;

case Comparison::IN:
$innerExpression = $this->getExpressionFactory()->in($value);
break;

case Comparison::NIN:
$innerExpression = $this->getExpressionFactory()->not($this->getExpressionFactory()->in($value));
break;

case Comparison::CONTAINS:
$innerExpression = $this->getExpressionFactory()->contains($value);
break;

case Comparison::STARTS_WITH:
$innerExpression = $this->getExpressionFactory()->startsWith($value);
break;

case Comparison::ENDS_WITH:
$innerExpression = $this->getExpressionFactory()->endsWith($value);
break;

case Comparison::LT:
$innerExpression = $this->getExpressionFactory()->lessThan($value);
break;

case Comparison::LTE:
$innerExpression = $this->getExpressionFactory()->lessThanEqual($value);
break;

case Comparison::GT:
$innerExpression = $this->getExpressionFactory()->greaterThan($value);
break;

case Comparison::GTE:
$innerExpression = $this->getExpressionFactory()->greaterThanEqual($value);
break;
}

if (!isset($innerExpression)) {
throw new InvalidArgumentException(sprintf(
'Unknown comparison operator: "%s"',
$comparison->getOperator()
));
}

return $this->getExpressionFactory()->{$this->comparisonMethod}($field, $innerExpression);
}
class_alias(
\spaceonfire\Criteria\Bridge\DoctrineCollections\DoctrineCollectionsExpressionConverter::class,
__NAMESPACE__ . '\DoctrineCollectionsExpressionConverter'
);

if (false) {
/**
* @inheritDoc
* Converts Expressions from Doctrine collections to webmozart expressions
* @deprecated Will be dropped in next major release.
* Use \spaceonfire\Criteria\Bridge\DoctrineCollections\DoctrineCollectionsExpressionConverter instead.
*/
public function walkValue(Value $value)
class DoctrineCollectionsExpressionConverter extends \spaceonfire\Criteria\Bridge\DoctrineCollections\DoctrineCollectionsExpressionConverter
{
return $value->getValue();
}
}
95 changes: 9 additions & 86 deletions src/Adapter/SpiralPagination/PaginableCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,19 @@

namespace spaceonfire\Criteria\Adapter\SpiralPagination;

use spaceonfire\Criteria\AbstractCriteriaDecorator;
use spaceonfire\Criteria\Criteria;
use spaceonfire\Criteria\CriteriaInterface;
use Spiral\Pagination\PaginableInterface;
use Spiral\Pagination\Paginator;
use Spiral\Pagination\PaginatorInterface;
use Webmozart\Assert\Assert;
use function class_alias;

class PaginableCriteria extends AbstractCriteriaDecorator implements PaginableInterface
{
/**
* @var PaginatorInterface|Paginator|null
*/
private $paginator;

/**
* PaginableCriteria constructor.
* @param CriteriaInterface|null $criteria original criteria to proxy
* @param PaginatorInterface|null $paginator
*/
public function __construct(?CriteriaInterface $criteria = null, ?PaginatorInterface $paginator = null)
{
parent::__construct($criteria ?? new Criteria());

if ($paginator !== null) {
$this->paginator = $paginator;
$paginator->paginate($this);
} else {
$this->resetPaginator();
}
}

/**
* Getter for `paginator` property
* @return PaginatorInterface|Paginator
*/
public function getPaginator(): PaginatorInterface
{
Assert::notNull($this->paginator);
return $this->paginator;
}

/**
* @return PaginatorInterface|Paginator
*/
private function makePaginator(): PaginatorInterface
{
$paginator = $this->paginator ?? new Paginator();

Assert::isInstanceOf($paginator, Paginator::class);

$limit = $this->getLimit() ?? 25;
$tmpCount = $limit + $this->getOffset();
$page = (int)($this->getOffset() / $limit) + 1;

return $paginator->withCount(max($tmpCount, $paginator->count()))->withLimit($limit)->withPage($page);
}

private function resetPaginator(): void
{
$this->paginator = $this->makePaginator();
}

/**
* @inheritDoc
*/
public function limit(?int $limit): CriteriaInterface
{
parent::limit($limit);
$this->resetPaginator();
return $this;
}

/**
* @inheritDoc
*/
public function offset(?int $offset): CriteriaInterface
{
parent::offset($offset);
$this->resetPaginator();
return $this;
}
class_alias(
\spaceonfire\Criteria\Bridge\SpiralPagination\PaginableCriteria::class,
__NAMESPACE__ . '\PaginableCriteria'
);

if (false) {
/**
* Clone criteria
* @deprecated Will be dropped in next major release.
* Use \spaceonfire\Criteria\Bridge\SpiralPagination\PaginableCriteria instead.
*/
public function __clone()
class PaginableCriteria extends \spaceonfire\Criteria\Bridge\SpiralPagination\PaginableCriteria
{
if ($this->paginator !== null) {
$this->paginator = clone $this->paginator;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace spaceonfire\Criteria\Bridge\DoctrineCollections;

use Doctrine\Common\Collections\Criteria as DoctrineCriteria;
use spaceonfire\Criteria\Criteria;
use spaceonfire\Criteria\CriteriaInterface;

class DoctrineCollectionsCriteriaConverter
{
/**
* Converts Doctrine criteria to spaceonfire criteria
* @param DoctrineCriteria $doctrineCriteria
* @param string $comparisonMethod
* @return CriteriaInterface
*/
public function convert(
DoctrineCriteria $doctrineCriteria,
string $comparisonMethod = 'property'
): CriteriaInterface {
$criteria = new Criteria();

if (null !== $doctrineExpression = $doctrineCriteria->getWhereExpression()) {
$expression = (new DoctrineCollectionsExpressionConverter($comparisonMethod))
->dispatch($doctrineExpression);

$criteria->where($expression);
}

$orderBy = array_map(static function (string $ordering): int {
return $ordering === DoctrineCriteria::ASC ? SORT_ASC : SORT_DESC;
}, $doctrineCriteria->getOrderings());

$criteria->orderBy($orderBy);

if (null !== $offset = $doctrineCriteria->getFirstResult()) {
$criteria->offset($offset);
}

if (null !== $limit = $doctrineCriteria->getMaxResults()) {
$criteria->limit($limit);
}

return $criteria;
}
}
Loading

0 comments on commit 7bc2c1c

Please sign in to comment.