Join::join()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Compolomus\LSQLQueryBuilder\Parts;
4
5
use Compolomus\LSQLQueryBuilder\BuilderException;
6
use Compolomus\LSQLQueryBuilder\System\{
7
    Traits\Helper,
8
    Traits\Caller
9
};
10
11
class Join
12
{
13
    use Caller, Helper;
14
15
    private $table;
16
17
    private $onPairs;
18
19
    private $using;
20
21
    private $alias;
22
23
    private $joinType;
24
25
    private $joinTypes = ['left', 'right', 'cross', 'inner'];
26
27
    private $counter = 0;
28
29
    public function __construct(string $table, ?string $alias = null, array $onPairs = [], string $joinType = 'left')
30
    {
31
        $this->join($table, $alias, $onPairs, $joinType);
32
    }
33
34
    public function join(string $table, ?string $alias = null, array $onPairs = [], string $joinType = 'left'): Join
35
    {
36
        $this->counter++;
37
        $this->table[$this->counter] = $table;
38
        $this->addOn($onPairs);
39
        $this->setAlias($alias);
40
        $this->setType($joinType);
41
        return $this;
42
    }
43
44
    public function setType(string $joinType): Join
45
    {
46
        if (!\in_array($joinType, $this->joinTypes, true)) {
47
            throw new BuilderException('DIE |JOIN construct|');
48
        }
49
        $this->joinType[$this->counter] = $joinType;
50
        return $this;
51
    }
52
53
    public function setAlias(?string $alias): Join
54
    {
55
        if (null !== $alias) {
56
            $this->alias[$this->counter] = $alias;
57
        }
58
        return $this;
59
    }
60
61
    public function getTable(int $counter): string
62
    {
63
        return null !== $this->alias[$counter]
64
            ? $this->escapeField($this->alias[$counter])
65
            : $this->escapeField($this->table[$counter]);
66
    }
67
68
    public function using(string $field): Join
69
    {
70
        $this->using = $this->escapeField($field);
71
        return $this;
72
    }
73
74
    public function addOn(array $onPairs): Join
75
    {
76
        if (\count($onPairs)) {
77
            $this->onPairs[$this->counter] = $onPairs;
78
        }
79
        return $this;
80
    }
81
82
    private function onMap(): string
83
    {
84
        if (!empty($this->onPairs)) {
85
            $result = [];
86
            foreach ($this->table as $counter => $join) {
87
                $result[] = $this->concatWhere(array_map(function ($item) use ($join) {
88
                    return $this->base->table() . '.' . $this->escapeField($item[0]) . ' = ' . $this->escapeField($join) . '.' . $this->escapeField($item[1]);
89
                }, $this->onPairs[$counter]));
90
            }
91
            return 'ON ' . $this->concatWhere($result);
92
        } else {
93
            return 'USING(' . $this->using . ')';
94
        }
95
    }
96
97
    public function result(): ?string
98
    {
99
        $result = [];
100
        foreach ($this->table as $counter => $join) {
101
            $result[] = ' ' . strtoupper($this->joinType[$counter]) . ' JOIN '
102
                . $this->escapeField($join)
103
                . (null !== $this->alias[$counter] ? ' AS ' . $this->escapeField($this->alias[$counter]) : '');
104
        }
105
        $result[] = $this->onMap();
106
        return implode(' ', $result);
107
    }
108
}
109