1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Infrastructure Related Agent |
5
|
|
|
* @author Max Demian <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Ticaje\FileManager\Implementors\Reader\File\BoxSpout; |
9
|
|
|
|
10
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException; |
11
|
|
|
use Box\Spout\Reader\Common\Creator\ReaderFactory; |
12
|
|
|
use Box\Spout\Reader\Exception\ReaderNotOpenedException; |
13
|
|
|
use Box\Spout\Reader\ReaderInterface; |
14
|
|
|
use Box\Spout\Reader\SheetInterface; |
15
|
|
|
use Exception; |
16
|
|
|
use Generator; |
17
|
|
|
use Iterator; |
18
|
|
|
use Ticaje\FileManager\Infrastructure\Driver\Reader\Interfaces\Gateway\FileGatewayInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Base |
22
|
|
|
* @package Ticaje\FileManager\Implementors\Reader\File\BoxSpout |
23
|
|
|
*/ |
24
|
|
|
abstract class Base implements FileGatewayInterface |
25
|
|
|
{ |
26
|
|
|
/** @var ReaderInterface $concretion */ |
27
|
|
|
protected $concretion; |
28
|
|
|
|
29
|
|
|
/** @var string $type */ |
30
|
|
|
protected $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Base constructor. |
34
|
|
|
* @throws Exception |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
$this->concretion = ReaderFactory::createFromType($this->type); |
40
|
|
|
} catch (UnsupportedTypeException $exception) { |
41
|
|
|
throw new Exception("Reader Agent Class does not exist"); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public function fetchData(string $fileName): Iterator |
49
|
|
|
{ |
50
|
|
|
$this->concretion->open($fileName); |
51
|
|
|
$sheet = $this->getActiveSheet(); |
52
|
|
|
|
53
|
|
|
return $this->yieldData($sheet); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return SheetInterface |
58
|
|
|
* @throws ReaderNotOpenedException |
59
|
|
|
*/ |
60
|
|
|
private function getActiveSheet(): SheetInterface |
61
|
|
|
{ |
62
|
|
|
$sheets = $this->concretion->getSheetIterator(); |
63
|
|
|
/** @var Sheet $sheet */ |
64
|
|
|
foreach ($sheets as $sheet) { |
65
|
|
|
if ($sheet->isActive()) { |
66
|
|
|
return $sheet; |
67
|
|
|
} |
68
|
|
|
} |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param SheetInterface $sheet |
73
|
|
|
* |
74
|
|
|
* @return Generator |
75
|
|
|
*/ |
76
|
|
|
private function yieldData(SheetInterface $sheet) |
77
|
|
|
{ |
78
|
|
|
foreach ($sheet->getRowIterator() as $row) { |
79
|
|
|
yield $row; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritDoc |
85
|
|
|
*/ |
86
|
|
|
public function asArray($object) |
87
|
|
|
{ |
88
|
|
|
return is_array($object) ? $object : $object->toArray(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: