Passed
Push — master ( 0ea13e...268b05 )
by Anton
01:37
created

PivotLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Cycle\ORM\Select\Loader;
11
12
use Cycle\ORM\Parser\AbstractNode;
13
use Cycle\ORM\Parser\ArrayNode;
14
use Cycle\ORM\Parser\Typecast;
15
use Cycle\ORM\Relation;
16
use Cycle\ORM\Schema;
17
use Cycle\ORM\Select\JoinableLoader;
18
use Cycle\ORM\Select\Traits\WhereTrait;
19
use Spiral\Database\Query\SelectQuery;
20
21
/**
22
 * Loads given entity table without any specific condition.
23
 */
24
class PivotLoader extends JoinableLoader
25
{
26
    use WhereTrait;
27
28
    /**
29
     * Default set of relation options. Child implementation might defined their of default options.
30
     *
31
     * @var array
32
     */
33
    protected $options = [
34
        'constrain' => true,
35
        'method'    => self::JOIN,
36
        'minify'    => true,
37
        'as'        => null,
38
        'using'     => null
39
    ];
40
41
    /**
42
     * @return string
43
     */
44
    public function getTable(): string
45
    {
46
        return $this->define(Schema::TABLE);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function configureQuery(SelectQuery $query, array $outerKeys = []): SelectQuery
53
    {
54
        // user specified WHERE conditions
55
        $this->setWhere(
56
            $query,
57
            $this->getAlias(),
58
            $this->isJoined() ? 'onWhere' : 'where',
59
            $this->options['where'] ?? $this->schema[Relation::THOUGH_WHERE] ?? []
60
        );
61
62
        return parent::configureQuery($query, $outerKeys);
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    protected function initNode(): AbstractNode
69
    {
70
        $node = new ArrayNode(
71
            $this->columnNames(),
72
            $this->define(Schema::PRIMARY_KEY),
73
            $this->schema[Relation::THOUGH_INNER_KEY],
74
            $this->schema[Relation::INNER_KEY]
75
        );
76
77
        $typecast = $this->define(Schema::TYPECAST);
78
        if ($typecast !== null) {
79
            $node->setTypecast(new Typecast($typecast, $this->getSource()->getDatabase()));
80
        }
81
82
        return $node;
83
    }
84
}