|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Oliverde8\Component\PhpEtl\ChainOperation\Grouping; |
|
6
|
|
|
|
|
7
|
|
|
use oliverde8\AssociativeArraySimplified\AssociativeArray; |
|
8
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\AbstractChainOperation; |
|
9
|
|
|
use Oliverde8\Component\PhpEtl\ChainOperation\DataChainOperationInterface; |
|
10
|
|
|
use Oliverde8\Component\PhpEtl\Item\ChainBreakItem; |
|
11
|
|
|
use Oliverde8\Component\PhpEtl\Item\DataItemInterface; |
|
12
|
|
|
use Oliverde8\Component\PhpEtl\Item\GroupedItem; |
|
13
|
|
|
use Oliverde8\Component\PhpEtl\Item\ItemInterface; |
|
14
|
|
|
use Oliverde8\Component\PhpEtl\Item\StopItem; |
|
15
|
|
|
use Oliverde8\Component\PhpEtl\Model\ExecutionContext; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class SimpleGrouping |
|
19
|
|
|
* |
|
20
|
|
|
* @author de Cramer Oliver<[email protected]> |
|
21
|
|
|
* @copyright 2018 Oliverde8 |
|
22
|
|
|
* @package Oliverde8\Component\PhpEtl\ChainOperation\Grouping |
|
23
|
|
|
*/ |
|
24
|
|
|
class SimpleGroupingOperation extends AbstractChainOperation implements DataChainOperationInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var string[] Key to use for grouping, if array it will be used to read recursively inside the array. */ |
|
27
|
|
|
protected $groupKey = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string[] Key to identify each individual data inside the group. */ |
|
30
|
|
|
protected $groupIdentifierKey = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Grouped data kept in memory. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $data = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* SimpleGroupingOperation constructor. |
|
41
|
|
|
* |
|
42
|
2 |
|
* @param string[] $groupKey Key to use for grouping, if array it will be used to read recursively inside the array. |
|
43
|
|
|
* @param string[] $groupIdentifierKey key to identify each individual data inside the group. |
|
44
|
2 |
|
*/ |
|
45
|
2 |
|
public function __construct(array $groupKey, array $groupIdentifierKey = []) |
|
46
|
2 |
|
{ |
|
47
|
|
|
$this->groupKey = $groupKey; |
|
48
|
|
|
$this->groupIdentifierKey = $groupIdentifierKey; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
2 |
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
2 |
|
*/ |
|
55
|
|
|
public function processData(DataItemInterface $item, ExecutionContext $context): ChainBreakItem |
|
56
|
2 |
|
{ |
|
57
|
2 |
|
$groupingValue = AssociativeArray::getFromKey($item->getData(), $this->groupKey); |
|
58
|
2 |
|
|
|
59
|
|
|
if (!empty($this->groupIdentifierKey)) { |
|
60
|
1 |
|
$groupIdValue = AssociativeArray::getFromKey($item->getData(), $this->groupIdentifierKey); |
|
61
|
|
|
$this->data[$groupingValue][$groupIdValue] = $item->getData(); |
|
62
|
|
|
} else { |
|
63
|
|
|
$this->data[$groupingValue][] = $item->getData(); |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
return new ChainBreakItem(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
2 |
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function processStop(StopItem $stopItem, ExecutionContext $context): ItemInterface |
|
|
|
|
|
|
73
|
2 |
|
{ |
|
74
|
|
|
if (empty($this->data)) { |
|
75
|
|
|
return $stopItem; |
|
76
|
2 |
|
} |
|
77
|
2 |
|
|
|
78
|
|
|
$data = $this->data; |
|
79
|
2 |
|
$this->data = []; |
|
80
|
|
|
|
|
81
|
|
|
return new GroupedItem(new \ArrayIterator($data)); |
|
82
|
|
|
} |
|
83
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.