Completed
Push — master ( 7c2cc0...12c942 )
by Beniamin
02:37
created

TableCompiler::compileRootTables()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
c 1
b 0
f 0
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
crap 5
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\SQLBuilder\QueryCompiler;
13
14
use Phuria\SQLBuilder\QueryBuilder\AbstractBuilder;
15
use Phuria\SQLBuilder\QueryBuilder\Component;
16
use Phuria\SQLBuilder\QueryBuilder\DeleteBuilder;
17
use Phuria\SQLBuilder\QueryBuilder\SelectBuilder;
18
use Phuria\SQLBuilder\Table\AbstractTable;
19
20
/**
21
 * @author Beniamin Jonatan Šimko <[email protected]>
22
 */
23
class TableCompiler
24
{
25
    /**
26
     * @param AbstractTable $table
27
     *
28
     * @return string
29
     */
30 30
    private function compileTableDeclaration(AbstractTable $table)
31
    {
32 30
        $declaration = '';
33
34 30
        if ($table->isJoin()) {
35 4
            $declaration .= $table->getJoinType() . ' ';
36 4
        }
37
38 30
        $declaration .= $table->getTableName();
39
40 30
        if ($alias = $table->getAlias()) {
41 10
            $declaration .= ' AS ' . $alias;
42 10
        }
43
44 30
        if ($joinOn = $table->getJoinOn()) {
45 3
            $declaration .= ' ON ' . $joinOn;
46 3
        }
47
48 30
        return $declaration;
49
    }
50
51
    /**
52
     * @param AbstractBuilder $qb
53
     *
54
     * @return string
55
     */
56 31
    public function compileRootTables(AbstractBuilder $qb)
57
    {
58 31
        $rootTables = '';
59
60 31
        if ($qb instanceof SelectBuilder || $qb instanceof DeleteBuilder) {
61 26
            $rootTables .= 'FROM ';
62 26
        }
63
64 31
        if ($qb instanceof Component\TableComponentInterface && $qb->getRootTables()) {
65 30
            $rootTables .= implode(', ', array_map([$this, 'compileTableDeclaration'], $qb->getRootTables()));
66 30
        } else {
67 1
            return '';
68
        }
69
70 30
        return $rootTables;
71
    }
72
73
    /**
74
     * @param AbstractBuilder $qb
75
     *
76
     * @return string
77
     */
78 31
    public function compileJoinTables(AbstractBuilder $qb)
79
    {
80 31
        if ($qb instanceof Component\JoinComponentInterface) {
81 26
            return implode(' ', array_map([$this, 'compileTableDeclaration'], $qb->getJoinTables()));
82
        }
83
84 5
        return '';
85
    }
86
}