-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing repository interface is not mandatory anymore
- Loading branch information
Showing
16 changed files
with
292 additions
and
89 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ResourceBundle\Doctrine\ORM; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\QueryBuilder; | ||
use Pagerfanta\Adapter\ArrayAdapter; | ||
use Pagerfanta\Doctrine\ORM\QueryAdapter; | ||
use Pagerfanta\Pagerfanta; | ||
use Sylius\Resource\Model\ResourceInterface; | ||
|
||
/** | ||
* @property EntityManagerInterface $_em | ||
* @property ClassMetadata $_class | ||
* | ||
* @method QueryBuilder createQueryBuilder(string $alias, string $indexBy = null) | ||
*/ | ||
trait PaginatedRepositoryTrait | ||
{ | ||
/** | ||
* @return iterable<int, ResourceInterface> | ||
*/ | ||
public function createPaginator(array $criteria = [], array $sorting = []): iterable | ||
{ | ||
$queryBuilder = $this->createQueryBuilder('o'); | ||
|
||
$this->applyCriteria($queryBuilder, $criteria); | ||
$this->applySorting($queryBuilder, $sorting); | ||
|
||
return $this->getPaginator($queryBuilder); | ||
} | ||
|
||
protected function getPaginator(QueryBuilder $queryBuilder): Pagerfanta | ||
{ | ||
if (!class_exists(QueryAdapter::class)) { | ||
throw new \LogicException('You can not use the "paginator" if Pargefanta Doctrine ORM Adapter is not available. Try running "composer require pagerfanta/doctrine-orm-adapter".'); | ||
} | ||
|
||
// Use output walkers option in the query adapter should be false as it affects performance greatly (see sylius/sylius#3775) | ||
return new Pagerfanta(new QueryAdapter($queryBuilder, false, false)); | ||
} | ||
|
||
/** | ||
* @param array $objects | ||
*/ | ||
protected function getArrayPaginator($objects): Pagerfanta | ||
{ | ||
return new Pagerfanta(new ArrayAdapter($objects)); | ||
} | ||
|
||
protected function applyCriteria(QueryBuilder $queryBuilder, array $criteria = []): void | ||
{ | ||
foreach ($criteria as $property => $value) { | ||
if (!in_array($property, array_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) { | ||
continue; | ||
} | ||
|
||
$name = $this->getPropertyName($property); | ||
|
||
if (null === $value) { | ||
$queryBuilder->andWhere($queryBuilder->expr()->isNull($name)); | ||
} elseif (is_array($value)) { | ||
$queryBuilder->andWhere($queryBuilder->expr()->in($name, $value)); | ||
} elseif ('' !== $value) { | ||
$parameter = str_replace('.', '_', $property); | ||
$queryBuilder | ||
->andWhere($queryBuilder->expr()->eq($name, ':' . $parameter)) | ||
->setParameter($parameter, $value) | ||
; | ||
} | ||
} | ||
} | ||
|
||
protected function applySorting(QueryBuilder $queryBuilder, array $sorting = []): void | ||
{ | ||
foreach ($sorting as $property => $order) { | ||
if (!in_array($property, array_merge($this->_class->getAssociationNames(), $this->_class->getFieldNames()), true)) { | ||
continue; | ||
} | ||
|
||
if (!empty($order)) { | ||
$queryBuilder->addOrderBy($this->getPropertyName($property), $order); | ||
} | ||
} | ||
} | ||
|
||
protected function getPropertyName(string $name): string | ||
{ | ||
if (!str_contains($name, '.')) { | ||
return 'o' . '.' . $name; | ||
} | ||
|
||
return $name; | ||
} | ||
} |
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
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
Oops, something went wrong.