AbstractReader.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. *
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Reader;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. /**
  20. * Reader abstract class.
  21. *
  22. * @since 0.8.0
  23. *
  24. * @codeCoverageIgnore Abstract class
  25. */
  26. abstract class AbstractReader implements ReaderInterface
  27. {
  28. /**
  29. * Read data only?
  30. *
  31. * @var bool
  32. */
  33. protected $readDataOnly = true;
  34. /**
  35. * File pointer.
  36. *
  37. * @var bool|resource
  38. */
  39. protected $fileHandle;
  40. /**
  41. * Read data only?
  42. *
  43. * @return bool
  44. */
  45. public function isReadDataOnly()
  46. {
  47. // return $this->readDataOnly;
  48. return true;
  49. }
  50. /**
  51. * Set read data only.
  52. *
  53. * @param bool $value
  54. *
  55. * @return self
  56. */
  57. public function setReadDataOnly($value = true)
  58. {
  59. $this->readDataOnly = $value;
  60. return $this;
  61. }
  62. /**
  63. * Open file for reading.
  64. *
  65. * @param string $filename
  66. *
  67. * @return resource
  68. */
  69. protected function openFile($filename)
  70. {
  71. // Check if file exists
  72. if (!file_exists($filename) || !is_readable($filename)) {
  73. throw new Exception("Could not open $filename for reading! File does not exist.");
  74. }
  75. // Open file
  76. $this->fileHandle = fopen($filename, 'rb');
  77. if ($this->fileHandle === false) {
  78. throw new Exception("Could not open file $filename for reading.");
  79. }
  80. }
  81. /**
  82. * Can the current ReaderInterface read the file?
  83. *
  84. * @param string $filename
  85. *
  86. * @return bool
  87. */
  88. public function canRead($filename)
  89. {
  90. // Check if file exists
  91. try {
  92. $this->openFile($filename);
  93. } catch (Exception $e) {
  94. return false;
  95. }
  96. if (is_resource($this->fileHandle)) {
  97. fclose($this->fileHandle);
  98. }
  99. return true;
  100. }
  101. }