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 static static createWriter (Spreadsheet $spreadsheet, $writerType)
 Create Writer. More...
 
static createReader ($readerType)
 Create Reader. More...
 
static load ($pFilename)
 Loads Spreadsheet from file using automatic Reader resolution. More...
 
static identify ($pFilename)
 Identify file type using automatic Reader resolution. More...
 
static createReaderForFile ($filename)
 Create Reader for file using automatic Reader 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 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.

Parameters
string$readerTypeExample: Xlsx
Returns
Reader

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  }

◆ createReaderForFile()

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

Create Reader for file using automatic Reader resolution.

Parameters
string$filenameThe name of the spreadsheet file
Returns
Reader

Definition at line 113 of file IOFactory.php.

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

114  {
116 
117  // First, lucky guess by inspecting file extension
118  $guessedReader = self::getReaderTypeFromExtension($filename);
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) {
133  $reader = self::createReader($type);
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  }
$type
$filename
Definition: buildRTE.php:89
static assertFile($filename)
Assert that given path is an existing file and is readable, otherwise throw exception.
Definition: File.php:143
+ Here is the call graph for this function:

◆ createWriter()

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

Create Writer.

Parameters
string$writerTypeExample: Xlsx
Returns
Writer

Definition at line 44 of file IOFactory.php.

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

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  }
+ 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.

References $filename.

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  }
$filename
Definition: buildRTE.php:89

◆ identify()

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

Identify file type using automatic Reader resolution.

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

Definition at line 96 of file IOFactory.php.

References $reader.

97  {
98  $reader = self::createReaderForFile($pFilename);
99  $className = get_class($reader);
100  $classType = explode('\\', $className);
101  unset($reader);
102 
103  return array_pop($classType);
104  }

◆ load()

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

Loads Spreadsheet from file using automatic Reader resolution.

Parameters
string$pFilenameThe name of the spreadsheet file
Returns
Spreadsheet

Definition at line 82 of file IOFactory.php.

References $reader.

83  {
84  $reader = self::createReaderForFile($pFilename);
85 
86  return $reader->load($pFilename);
87  }

◆ 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

Definition at line 15 of file IOFactory.php.

◆ $writers

static PhpOffice\PhpSpreadsheet\IOFactory::$writers
staticprivate
Initial value:
= [
'Xls' => Writer\Xls::class

Definition at line 26 of file IOFactory.php.


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