1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oliverde8\Component\PhpEtl\ChainOperation\Extract; |
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\Extract\File\Csv; |
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\Model\ExecutionContext; |
13
|
|
|
|
14
|
|
|
class CsvExtractOperation extends AbstractChainOperation implements DataChainOperationInterface |
15
|
|
|
{ |
16
|
|
|
protected string $delimiter; |
17
|
|
|
|
18
|
|
|
protected string $enclosure; |
19
|
|
|
|
20
|
|
|
protected string $escape; |
21
|
|
|
|
22
|
|
|
protected string $fileKey; |
23
|
|
|
|
24
|
|
|
public function __construct(string $delimiter, string $enclosure, string $escape, string $fileKey) |
25
|
|
|
{ |
26
|
|
|
$this->delimiter = $delimiter; |
27
|
|
|
$this->enclosure = $enclosure; |
28
|
|
|
$this->escape = $escape; |
29
|
|
|
$this->fileKey = $fileKey; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface |
34
|
|
|
{ |
35
|
|
|
$filename = $item->getData(); |
36
|
|
|
if (is_array($filename)) { |
37
|
|
|
$filename = AssociativeArray::getFromKey($filename, $this->fileKey); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$fileIterator = new Csv($filename, $this->delimiter, $this->enclosure, $this->escape); |
41
|
|
|
return new GroupedItem($fileIterator); |
42
|
|
|
} |
43
|
|
|
} |