Completed
Push — master ( 95929c...411a88 )
by Craig
13:17
created

RepositoryGetResultsTrait::doGetPaginatedResults()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 11
c 1
b 1
f 0
nc 4
nop 5
dl 0
loc 25
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\SearchModule\Entity\Repository;
15
16
use Doctrine\ORM\QueryBuilder;
17
use Zikula\Bundle\CoreBundle\Doctrine\Paginator;
18
use Zikula\Bundle\CoreBundle\Doctrine\PaginatorInterface;
19
20
trait RepositoryGetResultsTrait
21
{
22
    public function doGetPaginatedResults(
23
        QueryBuilder $qb,
24
        array $filters = [],
25
        array $sorting = [],
26
        int $page = 1,
27
        int $pageSize = 25
28
    ): PaginatorInterface {
29
        $alias = $qb->getRootAliases()[0];
30
        // add clauses for where
31
        if (count($filters) > 0) {
32
            $i = 1;
33
            foreach ($filters as $w_key => $w_value) {
34
                $qb->andWhere($qb->expr()->eq($alias . '.' . $w_key, '?' . $i))
35
                    ->setParameter($i, $w_value);
36
                $i++;
37
            }
38
        }
39
        // add clause for ordering
40
        if (count($sorting) > 0) {
41
            foreach ($sorting as $sort => $sortdir) {
42
                $qb->addOrderBy($alias . '.' . $sort, $sortdir);
43
            }
44
        }
45
46
        return (new Paginator($qb, $pageSize))->paginate($page);
47
    }
48
}
49