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