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\NodalFlow\Nodes\NodeAbstract; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ExtractorAbstract |
16
|
|
|
*/ |
17
|
|
|
abstract class ExtractorAbstract extends NodeAbstract implements ExtractorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* This is a Traversable |
21
|
|
|
* |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $isATraversable = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This is a returning value |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $isAReturningVal = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* This is not a Flow |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $isAFlow = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $nodeIncrements = [ |
44
|
|
|
'num_records' => 'num_iterate', |
45
|
|
|
'num_extract' => 0, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var iterable |
50
|
|
|
*/ |
51
|
|
|
protected $extracted; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $numExtracts = 0; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
protected $numRecords = 0; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* get the traversable to traverse within the Flow |
65
|
|
|
* |
66
|
|
|
* @param mixed $param |
67
|
|
|
* |
68
|
|
|
* @return iterable |
69
|
|
|
*/ |
70
|
|
|
public function getTraversable($param = null): iterable |
71
|
|
|
{ |
72
|
|
|
$this->bootNumExtracts(); |
73
|
|
|
while ($this->extract($param)) { |
74
|
|
|
++$this->numExtracts; |
75
|
|
|
foreach ($this->getExtracted() as $record) { |
76
|
|
|
++$this->numRecords; |
77
|
|
|
yield $record; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get number of records (at the end of the Flow's execution) |
84
|
|
|
* |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
|
|
public function getNumRecords(): int |
88
|
|
|
{ |
89
|
|
|
return $this->numRecords; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get number of records (at the end of the Flow's execution) |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function getNumExtracts(): int |
98
|
|
|
{ |
99
|
|
|
return $this->numExtracts; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function bootNumExtracts(): self |
106
|
|
|
{ |
107
|
|
|
$this->numExtracts = 0; |
108
|
|
|
$this->numRecords = 0; |
109
|
|
|
/** @var ExtractorInterface $this */ |
110
|
|
|
if ($carrier = $this->getCarrier()) { |
111
|
|
|
$nodeMap = &$carrier->getFlowMap()->getNodeStat($this->getId()); |
112
|
|
|
$nodeMap['num_extract'] = &$this->numExtracts; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* return what was extracted during last call to extract |
120
|
|
|
* As single record must be a collection of one record |
121
|
|
|
* it can be more elegant to: |
122
|
|
|
* ` yield $record;` |
123
|
|
|
* rather than to: |
124
|
|
|
* ` return [$record];` |
125
|
|
|
* |
126
|
|
|
* @return iterable |
127
|
|
|
*/ |
128
|
|
|
protected function getExtracted(): iterable |
129
|
|
|
{ |
130
|
|
|
return $this->extracted ?: []; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* set current extraction result |
135
|
|
|
* |
136
|
|
|
* @param iterable|null $extracted |
137
|
|
|
* |
138
|
|
|
* @return static |
139
|
|
|
*/ |
140
|
|
|
protected function setExtractedCollection(?iterable $extracted = null): self |
141
|
|
|
{ |
142
|
|
|
$this->extracted = $extracted; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed|null $extracted |
149
|
|
|
* |
150
|
|
|
* @return static |
151
|
|
|
*/ |
152
|
|
|
protected function setExtractedRecord($extracted = null): self |
153
|
|
|
{ |
154
|
|
|
$this->extracted = $extracted !== null ? [$extracted] : null; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|