|
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\FileExtractedItem; |
|
11
|
|
|
use Oliverde8\Component\PhpEtl\Item\GroupedItem; |
|
12
|
|
|
use Oliverde8\Component\PhpEtl\Item\ItemInterface; |
|
13
|
|
|
use Oliverde8\Component\PhpEtl\Item\MixItem; |
|
14
|
|
|
use Oliverde8\Component\PhpEtl\Model\ExecutionContext; |
|
15
|
|
|
|
|
16
|
|
|
class CsvExtractOperation extends AbstractChainOperation implements DataChainOperationInterface |
|
17
|
|
|
{ |
|
18
|
|
|
protected string $delimiter; |
|
19
|
|
|
|
|
20
|
|
|
protected string $enclosure; |
|
21
|
|
|
|
|
22
|
|
|
protected string $escape; |
|
23
|
|
|
|
|
24
|
|
|
protected string $fileKey; |
|
25
|
|
|
|
|
26
|
|
|
protected bool $scoped; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(string $delimiter, string $enclosure, string $escape, string $fileKey, bool $scoped) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->delimiter = $delimiter; |
|
31
|
|
|
$this->enclosure = $enclosure; |
|
32
|
|
|
$this->escape = $escape; |
|
33
|
|
|
$this->fileKey = $fileKey; |
|
34
|
|
|
$this->scoped = $scoped; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
public function processData(DataItemInterface $item, ExecutionContext $context): ItemInterface |
|
39
|
|
|
{ |
|
40
|
|
|
$filename = $item->getData(); |
|
41
|
|
|
if (is_array($filename)) { |
|
42
|
|
|
$filename = AssociativeArray::getFromKey($filename, $this->fileKey); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$fileIterator = new Csv($context->getFileSystem()->readStream($filename), $this->delimiter, $this->enclosure, $this->escape); |
|
46
|
|
|
|
|
47
|
|
|
return new MixItem([new GroupedItem($fileIterator), new FileExtractedItem($filename)]); |
|
48
|
|
|
} |
|
49
|
|
|
} |