1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Reader\Common\Creator; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Creator\HelperFactory; |
6
|
|
|
use Box\Spout\Common\Exception\IOException; |
7
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException; |
8
|
|
|
use Box\Spout\Common\Type; |
9
|
|
|
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory as CSVInternalEntityFactory; |
10
|
|
|
use Box\Spout\Reader\CSV\Manager\OptionsManager as CSVOptionsManager; |
11
|
|
|
use Box\Spout\Reader\CSV\Reader as CSVReader; |
12
|
|
|
use Box\Spout\Reader\ODS\Creator\HelperFactory as ODSHelperFactory; |
13
|
|
|
use Box\Spout\Reader\ODS\Creator\InternalEntityFactory as ODSInternalEntityFactory; |
14
|
|
|
use Box\Spout\Reader\ODS\Creator\ManagerFactory as ODSManagerFactory; |
15
|
|
|
use Box\Spout\Reader\ODS\Manager\OptionsManager as ODSOptionsManager; |
16
|
|
|
use Box\Spout\Reader\ODS\Reader as ODSReader; |
17
|
|
|
use Box\Spout\Reader\ReaderInterface; |
18
|
|
|
use Box\Spout\Reader\XLSX\Creator\HelperFactory as XLSXHelperFactory; |
19
|
|
|
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory as XLSXInternalEntityFactory; |
20
|
|
|
use Box\Spout\Reader\XLSX\Creator\ManagerFactory as XLSXManagerFactory; |
21
|
|
|
use Box\Spout\Reader\XLSX\Manager\OptionsManager as XLSXOptionsManager; |
22
|
|
|
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory; |
23
|
|
|
use Box\Spout\Reader\XLSX\Reader as XLSXReader; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class ReaderFactory |
27
|
|
|
* This factory is used to create readers, based on the type of the file to be read. |
28
|
|
|
* It supports CSV, XLSX and ODS formats. |
29
|
|
|
*/ |
30
|
|
|
class ReaderFactory |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Map file extensions to reader types |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private static $extensionReaderMap = [ |
37
|
|
|
'csv' => Type::CSV, |
38
|
80 |
|
'ods' => Type::ODS, |
39
|
|
|
'xlsx' => Type::XLSX, |
40
|
|
|
]; |
41
|
80 |
|
|
42
|
79 |
|
/** |
43
|
35 |
|
* This creates an instance of the appropriate reader, given the type of the file to be read |
44
|
|
|
* |
45
|
1 |
|
* @param string $readerType Type of the reader to instantiate |
46
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
47
|
|
|
* @return ReaderInterface |
48
|
|
|
*/ |
49
|
|
|
public static function create($readerType) |
50
|
|
|
{ |
51
|
|
|
switch ($readerType) { |
52
|
1 |
|
case Type::CSV: return self::getCSVReader(); |
53
|
|
|
case Type::XLSX: return self::getXLSXReader(); |
54
|
1 |
|
case Type::ODS: return self::getODSReader(); |
55
|
1 |
|
default: |
56
|
1 |
|
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType); |
57
|
1 |
|
} |
58
|
|
|
} |
59
|
1 |
|
|
60
|
|
|
/** |
61
|
|
|
* Creates a reader by file extension |
62
|
|
|
* |
63
|
|
|
* @param string The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx |
64
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
65
|
44 |
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
66
|
|
|
* @return ReaderInterface |
67
|
44 |
|
*/ |
68
|
44 |
|
public static function createFromFile(string $path) |
69
|
44 |
|
{ |
70
|
44 |
|
if (!\is_file($path)) { |
71
|
44 |
|
throw new IOException( |
72
|
|
|
sprintf('Could not open "%s" for reading! File does not exist.', $path) |
73
|
44 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$ext = \pathinfo($path, PATHINFO_EXTENSION); |
77
|
|
|
$ext = \strtolower($ext); |
78
|
|
|
$readerType = self::$extensionReaderMap[$ext] ?? null; |
79
|
34 |
|
if ($readerType === null) { |
80
|
|
|
throw new UnsupportedTypeException( |
81
|
34 |
|
sprintf('No readers supporting the file extension "%s".', $ext) |
82
|
34 |
|
); |
83
|
34 |
|
} |
84
|
34 |
|
|
85
|
34 |
|
return self::create($readerType); |
86
|
|
|
} |
87
|
34 |
|
|
88
|
|
|
/** |
89
|
|
|
* @return CSVReader |
90
|
|
|
*/ |
91
|
|
|
private static function getCSVReader() |
92
|
|
|
{ |
93
|
|
|
$optionsManager = new CSVOptionsManager(); |
94
|
|
|
$helperFactory = new HelperFactory(); |
95
|
|
|
$entityFactory = new CSVInternalEntityFactory($helperFactory); |
96
|
|
|
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); |
97
|
|
|
|
98
|
|
|
return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return XLSXReader |
103
|
|
|
*/ |
104
|
|
|
private static function getXLSXReader() |
105
|
|
|
{ |
106
|
|
|
$optionsManager = new XLSXOptionsManager(); |
107
|
|
|
$helperFactory = new XLSXHelperFactory(); |
108
|
|
|
$managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory()); |
109
|
|
|
$entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory); |
110
|
|
|
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); |
111
|
|
|
|
112
|
|
|
return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return ODSReader |
117
|
|
|
*/ |
118
|
|
|
private static function getODSReader() |
119
|
|
|
{ |
120
|
|
|
$optionsManager = new ODSOptionsManager(); |
121
|
|
|
$helperFactory = new ODSHelperFactory(); |
122
|
|
|
$managerFactory = new ODSManagerFactory(); |
123
|
|
|
$entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory); |
124
|
|
|
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); |
125
|
|
|
|
126
|
|
|
return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|