| 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 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 |
public static function create($readerType) |
| 27 |
{ |
| 28 |
$reader = null; |
| 29 |
|
| 30 |
switch ($readerType) { |
| 31 |
case Type::CSV: |
| 32 |
$reader = new CSV\Reader(); |
| 33 |
break; |
| 34 |
case Type::XLSX: |
| 35 |
$reader = new XLSX\Reader(); |
| 36 |
break; |
| 37 |
case Type::ODS: |
| 38 |
$reader = new ODS\Reader(); |
| 39 |
break; |
| 40 |
default: |
| 41 |
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType); |
| 42 |
} |
| 43 |
|
| 44 |
$reader->setGlobalFunctionsHelper(new GlobalFunctionsHelper()); |
| 45 |
|
| 46 |
return $reader; |
| 47 |
} |
| 48 |
} |
| 49 |
|