Passed
Branch master (1b043d)
by compolom
01:59 queued 21s
created

Join   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

9 Methods

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