AbstractReader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\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. * @return self
  55. */
  56. public function setReadDataOnly($value = true)
  57. {
  58. $this->readDataOnly = $value;
  59. return $this;
  60. }
  61. /**
  62. * Open file for reading
  63. *
  64. * @param string $filename
  65. *
  66. * @throws \PhpOffice\PhpWord\Exception\Exception
  67. *
  68. * @return resource
  69. */
  70. protected function openFile($filename)
  71. {
  72. // Check if file exists
  73. if (!file_exists($filename) || !is_readable($filename)) {
  74. throw new Exception("Could not open $filename for reading! File does not exist.");
  75. }
  76. // Open file
  77. $this->fileHandle = fopen($filename, 'r');
  78. if ($this->fileHandle === false) {
  79. throw new Exception("Could not open file $filename for reading.");
  80. }
  81. }
  82. /**
  83. * Can the current ReaderInterface read the file?
  84. *
  85. * @param string $filename
  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. /**
  102. * Read data only?
  103. *
  104. * @deprecated 0.10.0
  105. *
  106. * @codeCoverageIgnore
  107. */
  108. public function getReadDataOnly()
  109. {
  110. return $this->isReadDataOnly();
  111. }
  112. }