|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Oliverde8\Component\PhpEtl\ChainOperation; |
|
6
|
|
|
|
|
7
|
|
|
use Oliverde8\Component\PhpEtl\Item\DataItemInterface; |
|
8
|
|
|
use Oliverde8\Component\PhpEtl\Item\ItemInterface; |
|
9
|
|
|
use Oliverde8\Component\PhpEtl\Item\StopItem; |
|
10
|
|
|
use Oliverde8\Component\PhpEtl\Model\ExecutionContext; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractChainOperation |
|
14
|
|
|
* |
|
15
|
|
|
* @author de Cramer Oliver<[email protected]> |
|
16
|
|
|
* @copyright 2018 Oliverde8 |
|
17
|
|
|
* @package Oliverde8\Component\PhpEtl\ChainOperation |
|
18
|
13 |
|
*/ |
|
19
|
|
|
abstract class AbstractChainOperation implements ChainOperationInterface |
|
20
|
13 |
|
{ |
|
21
|
|
|
private $methodResolutionCache = []; |
|
22
|
13 |
|
|
|
23
|
13 |
|
/** |
|
24
|
|
|
* @inheritdoc |
|
25
|
|
|
*/ |
|
26
|
4 |
|
final public function process(ItemInterface $item, ExecutionContext $context): ItemInterface |
|
27
|
|
|
{ |
|
28
|
|
|
if (!isset($this->methodResolutionCache[get_class($item)])) { |
|
29
|
|
|
$this->methodResolutionCache[get_class($item)] = $this->resolveMethodName($item); |
|
30
|
|
|
} |
|
31
|
|
|
$method = $this->methodResolutionCache[get_class($item)]; |
|
32
|
|
|
|
|
33
|
|
|
if (!is_null($method)) { |
|
34
|
|
|
return $this->$method($item, $context); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $item; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function resolveMethodName(ItemInterface $item): ?string |
|
41
|
|
|
{ |
|
42
|
|
|
$processReflection = new \ReflectionClass($this); |
|
43
|
|
|
foreach ($processReflection->getMethods() as $method) { |
|
44
|
|
|
if ($this->validateMethod($method)) { |
|
45
|
|
|
$firstParameter = $method->getParameters()[0]; |
|
46
|
|
|
$expecting = $firstParameter->getType()->getName(); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
if (interface_exists($expecting)) { |
|
49
|
|
|
$itemReflection = new \ReflectionClass($item); |
|
50
|
|
|
if ($itemReflection->implementsInterface($expecting)) { |
|
51
|
|
|
return $method->getName(); |
|
52
|
|
|
} |
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if ($this->checkIsA(get_class($item), $expecting)) { |
|
57
|
|
|
return $method->getName(); |
|
58
|
|
|
} |
|
59
|
|
|
}; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function checkIsA(string $class, string $targetClass): bool |
|
66
|
|
|
{ |
|
67
|
|
|
if ($class == $targetClass) { |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
while($class = get_parent_class($class)) { |
|
72
|
|
|
if ($class == $targetClass) { |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function validateMethod(\ReflectionMethod $method) |
|
81
|
|
|
{ |
|
82
|
|
|
if (count($method->getParameters()) != 2) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
if (in_array($method->getName(), ['process', 'resolveMethodName'])) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
if (strpos($method->getName(), "process") !== 0) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|