1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Reader; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Creator\HelperFactory; |
6
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException; |
7
|
|
|
use Box\Spout\Common\Helper\GlobalFunctionsHelper; |
8
|
|
|
use Box\Spout\Common\Type; |
9
|
|
|
use Box\Spout\Reader\XLSX\Helper\SharedStringsCaching\CachingStrategyFactory; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ReaderFactory |
13
|
|
|
* This factory is used to create readers, based on the type of the file to be read. |
14
|
|
|
* It supports CSV and XLSX formats. |
15
|
|
|
* |
16
|
|
|
* @package Box\Spout\Reader |
17
|
|
|
*/ |
18
|
|
|
class ReaderFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* This creates an instance of the appropriate reader, given the type of the file to be read |
22
|
|
|
* |
23
|
|
|
* @api |
24
|
|
|
* @param string $readerType Type of the reader to instantiate |
25
|
|
|
* @return ReaderInterface |
26
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
27
|
|
|
*/ |
28
|
74 |
|
public static function create($readerType) |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
switch ($readerType) { |
32
|
74 |
|
case Type::CSV: return self::getCSVReader(); |
33
|
73 |
|
case Type::XLSX: return self::getXLSXReader(); |
34
|
34 |
|
case Type::ODS: return self::getODSReader(); |
35
|
|
|
default: |
36
|
1 |
|
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return CSV\Reader |
42
|
|
|
*/ |
43
|
1 |
|
private static function getCSVReader() |
44
|
|
|
{ |
45
|
1 |
|
$optionsManager = new CSV\Manager\OptionsManager(); |
46
|
1 |
|
$helperFactory = new HelperFactory(); |
47
|
1 |
|
$entityFactory = new CSV\Creator\EntityFactory($helperFactory); |
48
|
1 |
|
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); |
49
|
|
|
|
50
|
1 |
|
return new CSV\Reader($optionsManager, $globalFunctionsHelper, $entityFactory); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return XLSX\Reader |
55
|
|
|
*/ |
56
|
39 |
|
private static function getXLSXReader() |
57
|
|
|
{ |
58
|
39 |
|
$optionsManager = new XLSX\Manager\OptionsManager(); |
59
|
39 |
|
$cachingStrategyFactory = new CachingStrategyFactory(); |
60
|
39 |
|
$helperFactory = new XLSX\Creator\HelperFactory($cachingStrategyFactory); |
61
|
39 |
|
$entityFactory = new XLSX\Creator\EntityFactory($helperFactory); |
62
|
39 |
|
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); |
63
|
|
|
|
64
|
39 |
|
return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $helperFactory); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return ODS\Reader |
69
|
|
|
*/ |
70
|
33 |
|
private static function getODSReader() |
71
|
|
|
{ |
72
|
33 |
|
$optionsManager = new ODS\Manager\OptionsManager(); |
73
|
33 |
|
$helperFactory = new ODS\Creator\HelperFactory(); |
74
|
33 |
|
$entityFactory = new ODS\Creator\EntityFactory($helperFactory); |
75
|
33 |
|
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper(); |
76
|
|
|
|
77
|
33 |
|
return new ODS\Reader($optionsManager, $globalFunctionsHelper, $entityFactory); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|