Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
<?php

namespace PhpOffice\PhpSpreadsheet;

> use PhpOffice\PhpSpreadsheet\Reader\IReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
> use PhpOffice\PhpSpreadsheet\Writer\IWriter;
/** * 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. */ abstract class IOFactory {
> public const READER_XLSX = 'Xlsx'; private static $readers = [ > public const READER_XLS = 'Xls'; 'Xlsx' => Reader\Xlsx::class, > public const READER_XML = 'Xml'; 'Xls' => Reader\Xls::class, > public const READER_ODS = 'Ods'; 'Xml' => Reader\Xml::class, > public const READER_SYLK = 'Slk'; 'Ods' => Reader\Ods::class, > public const READER_SLK = 'Slk'; 'Slk' => Reader\Slk::class, > public const READER_GNUMERIC = 'Gnumeric'; 'Gnumeric' => Reader\Gnumeric::class, > public const READER_HTML = 'Html'; 'Html' => Reader\Html::class, > public const READER_CSV = 'Csv'; 'Csv' => Reader\Csv::class, > ]; > public const WRITER_XLSX = 'Xlsx'; > public const WRITER_XLS = 'Xls'; private static $writers = [ > public const WRITER_ODS = 'Ods'; 'Xls' => Writer\Xls::class, > public const WRITER_CSV = 'Csv'; 'Xlsx' => Writer\Xlsx::class, > public const WRITER_HTML = 'Html'; 'Ods' => Writer\Ods::class, >
< '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,
> self::READER_XLSX => Reader\Xlsx::class, > self::READER_XLS => Reader\Xls::class, > self::READER_XML => Reader\Xml::class, > self::READER_ODS => Reader\Ods::class, > self::READER_SLK => Reader\Slk::class, > self::READER_GNUMERIC => Reader\Gnumeric::class, > self::READER_HTML => Reader\Html::class, > self::READER_CSV => Reader\Csv::class,
< 'Xls' => Writer\Xls::class, < 'Xlsx' => Writer\Xlsx::class, < 'Ods' => Writer\Ods::class, < 'Csv' => Writer\Csv::class, < 'Html' => Writer\Html::class,
> self::WRITER_XLS => Writer\Xls::class, > self::WRITER_XLSX => Writer\Xlsx::class, > self::WRITER_ODS => Writer\Ods::class, > self::WRITER_CSV => Writer\Csv::class, > self::WRITER_HTML => Writer\Html::class,
< * < * @param string $writerType Example: Xlsx < * < * @return Writer\IWriter
< public static function createWriter(Spreadsheet $spreadsheet, $writerType)
> public static function createWriter(Spreadsheet $spreadsheet, string $writerType): IWriter
} // Instantiate writer $className = self::$writers[$writerType]; return new $className($spreadsheet); } /**
< * Create Reader\IReader. < * < * @param string $readerType Example: Xlsx < * < * @return Reader\IReader
> * Create IReader.
*/
< public static function createReader($readerType)
> public static function createReader(string $readerType): IReader
{ if (!isset(self::$readers[$readerType])) { throw new Reader\Exception("No reader found for type $readerType"); } // Instantiate reader $className = self::$readers[$readerType]; return new $className(); } /** * Loads Spreadsheet from file using automatic Reader\IReader resolution. *
< * @param string $pFilename The name of the spreadsheet file < * < * @return Spreadsheet
> * @param string $filename The name of the spreadsheet file > * @param int $flags the optional second parameter flags may be used to identify specific elements > * that should be loaded, but which won't be loaded by default, using these values: > * IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file > * @param string[] $readers An array of Readers to use to identify the file type. By default, load() will try > * all possible Readers until it finds a match; but this allows you to pass in a > * list of Readers so it will only try the subset that you specify here. > * Values in this list can be any of the constant values defined in the set > * IOFactory::READER_*.
*/
< public static function load($pFilename)
> public static function load(string $filename, int $flags = 0, ?array $readers = null): Spreadsheet
{
< $reader = self::createReaderForFile($pFilename);
> $reader = self::createReaderForFile($filename, $readers);
< return $reader->load($pFilename);
> return $reader->load($filename, $flags);
} /**
< * Identify file type using automatic Reader\IReader resolution. < * < * @param string $pFilename The name of the spreadsheet file to identify < * < * @return string
> * Identify file type using automatic IReader resolution.
*/
< public static function identify($pFilename)
> public static function identify(string $filename, ?array $readers = null): string
{
< $reader = self::createReaderForFile($pFilename);
> $reader = self::createReaderForFile($filename, $readers);
$className = get_class($reader); $classType = explode('\\', $className); unset($reader); return array_pop($classType); } /**
< * Create Reader\IReader for file using automatic Reader\IReader resolution.
> * Create Reader\IReader for file using automatic IReader resolution.
*
< * @param string $filename The name of the spreadsheet file < * < * @return Reader\IReader
> * @param string[] $readers An array of Readers to use to identify the file type. By default, load() will try > * all possible Readers until it finds a match; but this allows you to pass in a > * list of Readers so it will only try the subset that you specify here. > * Values in this list can be any of the constant values defined in the set > * IOFactory::READER_*.
*/
< public static function createReaderForFile($filename)
> public static function createReaderForFile(string $filename, ?array $readers = null): IReader
{ File::assertFile($filename);
> $testReaders = self::$readers; // First, lucky guess by inspecting file extension > if ($readers !== null) { $guessedReader = self::getReaderTypeFromExtension($filename); > $readers = array_map('strtoupper', $readers); if ($guessedReader !== null) { > $testReaders = array_filter( $reader = self::createReader($guessedReader); > self::$readers, > function (string $readerType) use ($readers) { // Let's see if we are lucky > return in_array(strtoupper($readerType), $readers, true); if (isset($reader) && $reader->canRead($filename)) { > }, return $reader; > ARRAY_FILTER_USE_KEY } > ); } > } >
< if ($guessedReader !== null) {
> if (($guessedReader !== null) && array_key_exists($guessedReader, $testReaders)) {
< if (isset($reader) && $reader->canRead($filename)) {
> if ($reader->canRead($filename)) {
< // Try walking through all the options in self::$autoResolveClasses < foreach (self::$readers as $type => $class) {
> // Try walking through all the options in self::$readers (or the selected subset) > foreach ($testReaders as $readerType => $class) {
< if ($type !== $guessedReader) { < $reader = self::createReader($type);
> if ($readerType !== $guessedReader) { > $reader = self::createReader($readerType);
if ($reader->canRead($filename)) { return $reader; } } } throw new Reader\Exception('Unable to identify a reader for this file'); } /** * Guess a reader type from the file extension, if any.
< * < * @param string $filename < * < * @return null|string
*/
< private static function getReaderTypeFromExtension($filename)
> private static function getReaderTypeFromExtension(string $filename): ?string
{ $pathinfo = pathinfo($filename); if (!isset($pathinfo['extension'])) { return null; } switch (strtolower($pathinfo['extension'])) { case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded) case 'xltx': // Excel (OfficeOpenXML) Template case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded) return 'Xlsx'; case 'xls': // Excel (BIFF) Spreadsheet case 'xlt': // Excel (BIFF) Template return 'Xls'; case 'ods': // Open/Libre Offic Calc case 'ots': // Open/Libre Offic Calc Template return 'Ods'; case 'slk': return 'Slk'; case 'xml': // Excel 2003 SpreadSheetML return 'Xml'; case 'gnumeric': return 'Gnumeric'; case 'htm': case 'html': return 'Html'; case 'csv': // Do nothing // We must not try to use CSV reader since it loads // all files including Excel files etc. return null; default: return null; } } /** * Register a writer with its type and class name.
< * < * @param string $writerType < * @param string $writerClass
*/
< public static function registerWriter($writerType, $writerClass): void
> public static function registerWriter(string $writerType, string $writerClass): void
{
< if (!is_a($writerClass, Writer\IWriter::class, true)) { < throw new Writer\Exception('Registered writers must implement ' . Writer\IWriter::class);
> if (!is_a($writerClass, IWriter::class, true)) { > throw new Writer\Exception('Registered writers must implement ' . IWriter::class);
} self::$writers[$writerType] = $writerClass; } /** * Register a reader with its type and class name.
< * < * @param string $readerType < * @param string $readerClass
*/
< public static function registerReader($readerType, $readerClass): void
> public static function registerReader(string $readerType, string $readerClass): void
{
< if (!is_a($readerClass, Reader\IReader::class, true)) { < throw new Reader\Exception('Registered readers must implement ' . Reader\IReader::class);
> if (!is_a($readerClass, IReader::class, true)) { > throw new Reader\Exception('Registered readers must implement ' . IReader::class);
} self::$readers[$readerType] = $readerClass; } }