1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of YaEtl |
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
6
|
|
|
* This source file is licensed under the MIT license which you will |
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace fab2s\YaEtl\Extractors; |
11
|
|
|
|
12
|
|
|
use fab2s\YaEtl\YaEtlException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* class OnClause |
16
|
|
|
*/ |
17
|
|
|
class OnClause implements OnClauseInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The single unique key alias as exposed in the original extractor's records |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $fromKeyAlias; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The single unique key alias we are joining on |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $joinKeyAlias; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The merger to use to merge joined data to the record |
35
|
|
|
* |
36
|
|
|
* @var callable |
37
|
|
|
*/ |
38
|
|
|
protected $merger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Indicate if we are left or just joining |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $leftJoin = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The default record to return in left join mode |
49
|
|
|
* will be set to true in order to break the branch exec |
50
|
|
|
* in join mode or this value in left join mode |
51
|
|
|
* |
52
|
|
|
* @var mixed |
53
|
|
|
*/ |
54
|
|
|
protected $defaultRecord; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Instantiate an OnClose |
58
|
|
|
* |
59
|
|
|
* @param string $fromKeyAlias The from unique key name in record |
60
|
|
|
* @param string $joinKeyAlias The join unique key name in record |
61
|
|
|
* @param callable $merger |
62
|
|
|
* @param null|mixed $defaultRecord null for a regular join, |
63
|
|
|
* mixed a default record to be |
64
|
|
|
* used each time there is no match |
65
|
|
|
* just like a left join would |
66
|
|
|
* |
67
|
|
|
* @throws YaEtlException |
68
|
|
|
*/ |
69
|
|
|
public function __construct(string $fromKeyAlias, string $joinKeyAlias, callable $merger, $defaultRecord = null) |
70
|
|
|
{ |
71
|
|
|
$this->fromKeyAlias = \trim($fromKeyAlias); |
72
|
|
|
$this->joinKeyAlias = \trim($joinKeyAlias); |
73
|
|
|
|
74
|
|
|
if ($this->fromKeyAlias === '' || $this->joinKeyAlias === '') { |
75
|
|
|
throw new YaEtlException('From and Join are required'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->merger = $merger; |
79
|
|
|
|
80
|
|
|
if ($defaultRecord !== null) { |
81
|
|
|
$this->leftJoin = true; |
82
|
|
|
$this->defaultRecord = $defaultRecord; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the from key alias |
88
|
|
|
* |
89
|
|
|
* @return string The From extractor unique key name as exposed in each record |
90
|
|
|
*/ |
91
|
|
|
public function getFromKeyAlias(): string |
92
|
|
|
{ |
93
|
|
|
return $this->fromKeyAlias; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the join key alias |
98
|
|
|
* |
99
|
|
|
* @return string The Join extractor unique key name as exposed in each record |
100
|
|
|
*/ |
101
|
|
|
public function getJoinKeyAlias(): string |
102
|
|
|
{ |
103
|
|
|
return $this->joinKeyAlias; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Merge Joined data into the original record |
108
|
|
|
* |
109
|
|
|
* @param mixed $upstreamRecord |
110
|
|
|
* @param mixed $record |
111
|
|
|
* |
112
|
|
|
* @return mixed The somehow merged record |
113
|
|
|
*/ |
114
|
|
|
public function merge($upstreamRecord, $record) |
115
|
|
|
{ |
116
|
|
|
// don't worry too much @SEE https://github.com/fab2s/call_user_func |
117
|
|
|
return \call_user_func($this->merger, $upstreamRecord, $record); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Indicate if we are left joining |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isLeftJoin(): bool |
126
|
|
|
{ |
127
|
|
|
return $this->leftJoin; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the default record to use when no matching |
132
|
|
|
* record can be joined in left join mode |
133
|
|
|
* |
134
|
|
|
* @return mixed the default record |
135
|
|
|
*/ |
136
|
|
|
public function getDefaultRecord() |
137
|
|
|
{ |
138
|
|
|
return $this->defaultRecord; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|