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 int |
50
|
|
|
*/ |
51
|
|
|
protected $numExtracts = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $numRecords = 0; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* get the traversable to traverse within the Flow |
60
|
|
|
* |
61
|
|
|
* @param mixed $param |
62
|
|
|
* |
63
|
|
|
* @return iterable |
64
|
|
|
*/ |
65
|
|
|
public function getTraversable($param = null): iterable |
66
|
|
|
{ |
67
|
|
|
$this->bootNumExtracts(); |
68
|
|
|
while ($this->extract($param)) { |
69
|
|
|
++$this->numExtracts; |
70
|
|
|
foreach ($this->getExtracted() as $record) { |
71
|
|
|
++$this->numRecords; |
72
|
|
|
yield $record; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get number of records (at the end of the Flow's execution) |
79
|
|
|
* |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function getNumRecords(): int |
83
|
|
|
{ |
84
|
|
|
return $this->numRecords; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get number of records (at the end of the Flow's execution) |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getNumExtracts(): int |
93
|
|
|
{ |
94
|
|
|
return $this->numExtracts; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function bootNumExtracts(): self |
101
|
|
|
{ |
102
|
|
|
$this->numExtracts = 0; |
103
|
|
|
$this->numRecords = 0; |
104
|
|
|
/** @var ExtractorInterface $this */ |
105
|
|
|
if ($carrier = $this->getCarrier()) { |
106
|
|
|
$nodeMap = &$carrier->getFlowMap()->getNodeStat($this->getId()); |
107
|
|
|
$nodeMap['num_extract'] = &$this->numExtracts; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* return what was extracted during last call to extract |
115
|
|
|
* As single record must be a collection of one record |
116
|
|
|
* it can be more elegant to: |
117
|
|
|
* ` yield $record;` |
118
|
|
|
* rather than to: |
119
|
|
|
* ` return [$record];` |
120
|
|
|
* |
121
|
|
|
* @return iterable |
122
|
|
|
*/ |
123
|
|
|
abstract protected function getExtracted(): iterable; |
124
|
|
|
} |
125
|
|
|
|