IOFactory.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. use PhpOffice\PhpWord\Reader\ReaderInterface;
  20. use PhpOffice\PhpWord\Writer\WriterInterface;
  21. abstract class IOFactory
  22. {
  23. /**
  24. * Create new writer
  25. *
  26. * @param PhpWord $phpWord
  27. * @param string $name
  28. *
  29. * @throws \PhpOffice\PhpWord\Exception\Exception
  30. *
  31. * @return WriterInterface
  32. */
  33. public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
  34. {
  35. if ($name !== 'WriterInterface' && !in_array($name, array('ODText', 'RTF', 'Word2007', 'HTML', 'PDF'), true)) {
  36. throw new Exception("\"{$name}\" is not a valid writer.");
  37. }
  38. $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
  39. return new $fqName($phpWord);
  40. }
  41. /**
  42. * Create new reader
  43. *
  44. * @param string $name
  45. *
  46. * @throws Exception
  47. *
  48. * @return ReaderInterface
  49. */
  50. public static function createReader($name = 'Word2007')
  51. {
  52. return self::createObject('Reader', $name);
  53. }
  54. /**
  55. * Create new object
  56. *
  57. * @param string $type
  58. * @param string $name
  59. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  60. *
  61. * @throws \PhpOffice\PhpWord\Exception\Exception
  62. *
  63. * @return \PhpOffice\PhpWord\Writer\WriterInterface|\PhpOffice\PhpWord\Reader\ReaderInterface
  64. */
  65. private static function createObject($type, $name, $phpWord = null)
  66. {
  67. $class = "PhpOffice\\PhpWord\\{$type}\\{$name}";
  68. if (class_exists($class) && self::isConcreteClass($class)) {
  69. return new $class($phpWord);
  70. }
  71. throw new Exception("\"{$name}\" is not a valid {$type}.");
  72. }
  73. /**
  74. * Loads PhpWord from file
  75. *
  76. * @param string $filename The name of the file
  77. * @param string $readerName
  78. * @return \PhpOffice\PhpWord\PhpWord $phpWord
  79. */
  80. public static function load($filename, $readerName = 'Word2007')
  81. {
  82. /** @var \PhpOffice\PhpWord\Reader\ReaderInterface $reader */
  83. $reader = self::createReader($readerName);
  84. return $reader->load($filename);
  85. }
  86. /**
  87. * Check if it's a concrete class (not abstract nor interface)
  88. *
  89. * @param string $class
  90. * @return bool
  91. */
  92. private static function isConcreteClass($class)
  93. {
  94. $reflection = new \ReflectionClass($class);
  95. return !$reflection->isAbstract() && !$reflection->isInterface();
  96. }
  97. }