ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PhpOffice\PhpSpreadsheet\IOFactory Class Reference

Factory to create readers and writers easily. More...

+ Collaboration diagram for PhpOffice\PhpSpreadsheet\IOFactory:

Static Public Member Functions

static createWriter (Spreadsheet $spreadsheet, $writerType)
 Create Writer\IWriter. More...
 
static createReader ($readerType)
 Create Reader\IReader. More...
 
static load ($pFilename)
 Loads Spreadsheet from file using automatic Reader\IReader resolution. More...
 
static identify ($pFilename)
 Identify file type using automatic Reader\IReader resolution. More...
 
static createReaderForFile ($filename)
 Create Reader\IReader for file using automatic Reader\IReader resolution. More...
 
static registerWriter ($writerType, $writerClass)
 Register a writer with its type and class name. More...
 
static registerReader ($readerType, $readerClass)
 Register a reader with its type and class name. More...
 

Static Private Member Functions

static getReaderTypeFromExtension ($filename)
 Guess a reader type from the file extension, if any. More...
 

Static Private Attributes

static $readers
 
static $writers
 

Detailed Description

Factory to create readers and writers easily.

It is not required to use this class, but it should make it easier to read and write files. Especially for reading files with an unknown format.

Definition at line 13 of file IOFactory.php.

Member Function Documentation

◆ createReader()

static PhpOffice\PhpSpreadsheet\IOFactory::createReader (   $readerType)
static

Create Reader\IReader.

Parameters
string$readerTypeExample: Xlsx
Returns
Reader\IReader

Definition at line 63 of file IOFactory.php.

64 {
65 if (!isset(self::$readers[$readerType])) {
66 throw new Reader\Exception("No reader found for type $readerType");
67 }
68
69 // Instantiate reader
70 $className = self::$readers[$readerType];
71
72 return new $className();
73 }

Referenced by PhpOffice\PhpSpreadsheet\IOFactory\createReaderForFile().

+ Here is the caller graph for this function:

◆ createReaderForFile()

static PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile (   $filename)
static

Create Reader\IReader for file using automatic Reader\IReader resolution.

Parameters
string$filenameThe name of the spreadsheet file
Returns
Reader\IReader

Definition at line 113 of file IOFactory.php.

114 {
116
117 // First, lucky guess by inspecting file extension
119 if ($guessedReader !== null) {
120 $reader = self::createReader($guessedReader);
121
122 // Let's see if we are lucky
123 if ($reader->canRead($filename)) {
124 return $reader;
125 }
126 }
127
128 // If we reach here then "lucky guess" didn't give any result
129 // Try walking through all the options in self::$autoResolveClasses
130 foreach (self::$readers as $type => $class) {
131 // Ignore our original guess, we know that won't work
132 if ($type !== $guessedReader) {
134 if ($reader->canRead($filename)) {
135 return $reader;
136 }
137 }
138 }
139
140 throw new Reader\Exception('Unable to identify a reader for this file');
141 }
$filename
Definition: buildRTE.php:89
static createReader($readerType)
Create Reader\IReader.
Definition: IOFactory.php:63
static getReaderTypeFromExtension($filename)
Guess a reader type from the file extension, if any.
Definition: IOFactory.php:150
static assertFile($filename)
Assert that given path is an existing file and is readable, otherwise throw exception.
Definition: File.php:143
$type

References $filename, $reader, $type, PhpOffice\PhpSpreadsheet\Shared\File\assertFile(), PhpOffice\PhpSpreadsheet\IOFactory\createReader(), and PhpOffice\PhpSpreadsheet\IOFactory\getReaderTypeFromExtension().

Referenced by PhpOffice\PhpSpreadsheet\IOFactory\identify(), and PhpOffice\PhpSpreadsheet\IOFactory\load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createWriter()

static PhpOffice\PhpSpreadsheet\IOFactory::createWriter ( Spreadsheet  $spreadsheet,
  $writerType 
)
static

Create Writer\IWriter.

Parameters
string$writerTypeExample: Xlsx
Returns
Writer\IWriter

Definition at line 44 of file IOFactory.php.

45 {
46 if (!isset(self::$writers[$writerType])) {
47 throw new Writer\Exception("No writer found for type $writerType");
48 }
49
50 // Instantiate writer
51 $className = self::$writers[$writerType];
52
53 return new $className($spreadsheet);
54 }

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\write().

+ Here is the caller graph for this function:

◆ getReaderTypeFromExtension()

static PhpOffice\PhpSpreadsheet\IOFactory::getReaderTypeFromExtension (   $filename)
staticprivate

Guess a reader type from the file extension, if any.

Parameters
string$filename
Returns
null|string

Definition at line 150 of file IOFactory.php.

151 {
152 $pathinfo = pathinfo($filename);
153 if (!isset($pathinfo['extension'])) {
154 return null;
155 }
156
157 switch (strtolower($pathinfo['extension'])) {
158 case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
159 case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
160 case 'xltx': // Excel (OfficeOpenXML) Template
161 case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
162 return 'Xlsx';
163 case 'xls': // Excel (BIFF) Spreadsheet
164 case 'xlt': // Excel (BIFF) Template
165 return 'Xls';
166 case 'ods': // Open/Libre Offic Calc
167 case 'ots': // Open/Libre Offic Calc Template
168 return 'Ods';
169 case 'slk':
170 return 'Slk';
171 case 'xml': // Excel 2003 SpreadSheetML
172 return 'Xml';
173 case 'gnumeric':
174 return 'Gnumeric';
175 case 'htm':
176 case 'html':
177 return 'Html';
178 case 'csv':
179 // Do nothing
180 // We must not try to use CSV reader since it loads
181 // all files including Excel files etc.
182 return null;
183 default:
184 return null;
185 }
186 }

References $filename.

Referenced by PhpOffice\PhpSpreadsheet\IOFactory\createReaderForFile().

+ Here is the caller graph for this function:

◆ identify()

static PhpOffice\PhpSpreadsheet\IOFactory::identify (   $pFilename)
static

Identify file type using automatic Reader\IReader resolution.

Parameters
string$pFilenameThe name of the spreadsheet file to identify
Returns
string

Definition at line 96 of file IOFactory.php.

97 {
99 $className = get_class($reader);
100 $classType = explode('\\', $className);
101 unset($reader);
102
103 return array_pop($classType);
104 }
static createReaderForFile($filename)
Create Reader\IReader for file using automatic Reader\IReader resolution.
Definition: IOFactory.php:113

References $reader, and PhpOffice\PhpSpreadsheet\IOFactory\createReaderForFile().

+ Here is the call graph for this function:

◆ load()

static PhpOffice\PhpSpreadsheet\IOFactory::load (   $pFilename)
static

Loads Spreadsheet from file using automatic Reader\IReader resolution.

Parameters
string$pFilenameThe name of the spreadsheet file
Returns
Spreadsheet

Definition at line 82 of file IOFactory.php.

83 {
85
86 return $reader->load($pFilename);
87 }

References $reader, and PhpOffice\PhpSpreadsheet\IOFactory\createReaderForFile().

+ Here is the call graph for this function:

◆ registerReader()

static PhpOffice\PhpSpreadsheet\IOFactory::registerReader (   $readerType,
  $readerClass 
)
static

Register a reader with its type and class name.

Parameters
string$readerType
string$readerClass

Definition at line 209 of file IOFactory.php.

209 : void
210 {
211 if (!is_a($readerClass, Reader\IReader::class, true)) {
212 throw new Reader\Exception('Registered readers must implement ' . Reader\IReader::class);
213 }
214
215 self::$readers[$readerType] = $readerClass;
216 }

◆ registerWriter()

static PhpOffice\PhpSpreadsheet\IOFactory::registerWriter (   $writerType,
  $writerClass 
)
static

Register a writer with its type and class name.

Parameters
string$writerType
string$writerClass

Definition at line 194 of file IOFactory.php.

194 : void
195 {
196 if (!is_a($writerClass, Writer\IWriter::class, true)) {
197 throw new Writer\Exception('Registered writers must implement ' . Writer\IWriter::class);
198 }
199
200 self::$writers[$writerType] = $writerClass;
201 }

Field Documentation

◆ $readers

PhpOffice\PhpSpreadsheet\IOFactory::$readers
staticprivate
Initial value:
= [
'Xlsx' => Reader\Xlsx::class,
'Xls' => Reader\Xls::class,
'Xml' => Reader\Xml::class,
'Ods' => Reader\Ods::class,
'Slk' => Reader\Slk::class,
'Gnumeric' => Reader\Gnumeric::class,
'Html' => Reader\Html::class,
'Csv' => Reader\Csv::class,
]

Definition at line 15 of file IOFactory.php.

◆ $writers

PhpOffice\PhpSpreadsheet\IOFactory::$writers
staticprivate
Initial value:
= [
'Xls' => Writer\Xls::class,
'Xlsx' => Writer\Xlsx::class,
'Ods' => Writer\Ods::class,
'Csv' => Writer\Csv::class,
'Html' => Writer\Html::class,
'Tcpdf' => Writer\Pdf\Tcpdf::class,
'Dompdf' => Writer\Pdf\Dompdf::class,
'Mpdf' => Writer\Pdf\Mpdf::class,
]

Definition at line 26 of file IOFactory.php.


The documentation for this class was generated from the following file: