JoinCondition   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 92
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A stringify() 0 8 2
A __construct() 0 16 1
1
<?php
2
3
namespace Smoren\QueryRelationManager\Base\Structs;
4
5
/**
6
 * Class JoinCondition
7
 * @author Smoren <[email protected]>
8
 */
9
class JoinCondition
10
{
11
    /**
12
     * Condition type "one to one"
13
     */
14
    public const TYPE_SINGLE = 1;
15
16
    /**
17
     * Condition type "one to many"
18
     */
19
    public const TYPE_MULTIPLE = 2;
20
21
    /**
22
     * @var int Condition type
23
     * (1 — "one to one" or 2 — "one to many")
24
     */
25
    public int $type;
26
27
    /**
28
     * @var Table joined table
29
     */
30
    public Table $table;
31
32
    /**
33
     * @var Table table to join to
34
     */
35
    public Table $joinTo;
36
37
    /**
38
     * @var array<string, string> main join condition
39
     * ["field of joined table" => "field of table to join to", ...]
40
     */
41
    public array $joinCondition;
42
43
    /**
44
     * @var string join type ("inner", "left", "right")
45
     */
46
    public string $joinType;
47
48
    /**
49
     * @var string|null extra join condition
50
     * e.g. "and some_field = :some_value"
51
     */
52
    public ?string $extraJoinCondition;
53
54
    /**
55
     * @var array<string, scalar> dynamic params values of the extra join condition
56
     * e.g. [":some_value" => "123", ...]
57
     */
58
    public array $extraJoinParams;
59
60
    /**
61
     * JoinCondition constructor.
62
     * @param int $type condition type (1 — "one to one" или 2 — "one to many")
63
     * @param Table $table joined table
64
     * @param Table $joinTo table to join to
65
     * @param array<string, string> $joinCondition main join condition
66
     * @param string $joinType join type ("inner", "left", "right")
67
     * @param string|null $extraJoinCondition string extra join condition (e.g. "and some_field = :some_value")
68
     * @param array<string, scalar> $extraJoinParams dynamic params values of the extra join condition
69
     * (e.g. [":some_value" => "123", ...])
70
     */
71
    public function __construct(
72
        int $type,
73
        Table $table,
74
        Table $joinTo,
75
        array $joinCondition,
76
        string $joinType = 'left',
77
        ?string $extraJoinCondition = null,
78
        array $extraJoinParams = []
79
    ) {
80
        $this->type = $type;
81
        $this->table = $table;
82
        $this->joinTo = $joinTo;
83
        $this->joinCondition = $joinCondition;
84
        $this->joinType = $joinType;
85
        $this->extraJoinCondition = $extraJoinCondition;
86
        $this->extraJoinParams = $extraJoinParams;
87
    }
88
89
    /**
90
     * Returns SQL query part which has join conditions of the current table
91
     * @return string
92
     */
93
    public function stringify(): string
94
    {
95
        $joins = [];
96
        foreach($this->joinCondition as $linkBy => $linkTo) {
97
            $joins[] = "{$this->table->alias}.{$linkBy} = {$this->joinTo->alias}.{$linkTo}";
98
        }
99
100
        return implode(' AND ', $joins).' '.$this->extraJoinCondition;
101
    }
102
}
103