Completed
Push — master ( abc7fc...ef6c42 )
by Anton
02:25
created

ConstrainTrait::configureWindow()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 3
1
<?php
2
/**
3
 * orm
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Loaders\Traits;
9
10
use Spiral\Database\Builders\SelectQuery;
11
use Spiral\ORM\Helpers\AliasDecorator;
12
13
/**
14
 * Provides ability to set LIMIT and ORDER BY in relation loaders.
15
 */
16
trait ConstrainTrait
17
{
18
    /**
19
     * @param \Spiral\Database\Builders\SelectQuery $query
20
     *
21
     * @param array                                 $orderBy
22
     * @param int                                   $limit 0 when no selection.
23
     */
24
    private function configureWindow(SelectQuery $query, array $orderBy, int $limit = 0)
25
    {
26
        if (!empty($orderBy)) {
27
            $decorator = new AliasDecorator($query, 'where', $this->getAlias());
28
            $decorator->orderBy($orderBy);
29
        }
30
31
        if ($limit !== 0) {
32
            $query->limit($limit);
33
        }
34
    }
35
36
    /**
37
     * Joined table alias.
38
     *
39
     * @return string
40
     */
41
    abstract protected function getAlias(): string;
42
}