forked from Sylius/SyliusResourceBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreatePaginatorTrait.php
More file actions
104 lines (87 loc) · 3.35 KB
/
Copy pathCreatePaginatorTrait.php
File metadata and controls
104 lines (87 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?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\EntityRepository as DoctrineEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Doctrine\ORM\QueryAdapter;
use Pagerfanta\Pagerfanta;
use Pagerfanta\PagerfantaInterface;
use Sylius\Resource\Model\ResourceInterface;
/**
* @mixin DoctrineEntityRepository
*/
trait CreatePaginatorTrait
{
/**
* @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): PagerfantaInterface
{
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): PagerfantaInterface
{
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->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->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->getClassMetadata()->getAssociationNames(), $this->getClassMetadata()->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;
}
}