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