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