1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Infrastructure Related Agent |
5
|
|
|
* @author Max Demian <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Ticaje\FileManager\Infrastructure\Driver\Reader\File; |
9
|
|
|
|
10
|
|
|
use Iterator; |
11
|
|
|
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\Gateway\FileGatewayInterface; |
12
|
|
|
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\SourceInterface; |
13
|
|
|
use Ticaje\FileManager\Infrastructure\Driver\Traits\FileInterface as ContractorTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Base |
17
|
|
|
* @package Ticaje\FileManager\Infrastructure\Driver\Reader\File |
18
|
|
|
*/ |
19
|
|
|
abstract class Base |
20
|
|
|
{ |
21
|
|
|
use ContractorTrait; |
22
|
|
|
|
23
|
|
|
/** @var FileGatewayInterface $implementor */ |
24
|
|
|
protected $implementor; |
25
|
|
|
|
26
|
|
|
/** @var bool $hasHeader */ |
27
|
|
|
protected $hasHeader; |
28
|
|
|
|
29
|
|
|
/** @var Iterator|array $copy */ |
30
|
|
|
private $copy; |
31
|
|
|
|
32
|
|
|
/** @var Iterator|array $content */ |
33
|
|
|
private $content; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Csv constructor. |
38
|
|
|
* |
39
|
|
|
* @param FileGatewayInterface $implementor |
40
|
|
|
* @param bool $hasHeader |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
FileGatewayInterface $implementor, |
44
|
|
|
bool $hasHeader = false |
45
|
|
|
) { |
46
|
|
|
$this->implementor = $implementor; |
47
|
|
|
$this->hasHeader = $hasHeader; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function setSource(string $fileName): SourceInterface |
54
|
|
|
{ |
55
|
|
|
$this->content = $this->implementor->fetchData($fileName); |
56
|
|
|
$this->copy = $this->implementor->fetchData($fileName); |
57
|
|
|
$this->setHeader(); |
58
|
|
|
|
59
|
|
|
return $this; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function getData(): Iterator |
66
|
|
|
{ |
67
|
|
|
$content = $this->getContent(); |
68
|
|
|
if ($content instanceof Iterator) { |
|
|
|
|
69
|
|
|
return $content; |
70
|
|
|
} |
71
|
|
|
if (is_array($content)) { |
72
|
|
|
foreach ($this->getContent() as $item) { |
73
|
|
|
yield $item; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function getImplementor(): FileGatewayInterface |
82
|
|
|
{ |
83
|
|
|
return $this->implementor; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getContentCopy(): Iterator |
87
|
|
|
{ |
88
|
|
|
return $this->copy; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|