ZipNewFileEntry.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace PhpZip\Model\Entry;
  3. use PhpZip\Exception\InvalidArgumentException;
  4. use PhpZip\Exception\RuntimeException;
  5. use PhpZip\Exception\ZipException;
  6. /**
  7. * @author Ne-Lexa alexey@nelexa.ru
  8. * @license MIT
  9. */
  10. class ZipNewFileEntry extends ZipAbstractEntry
  11. {
  12. /** @var string Filename */
  13. protected $file;
  14. /**
  15. * ZipNewEntry constructor.
  16. *
  17. * @param string $file
  18. *
  19. * @throws ZipException
  20. */
  21. public function __construct($file)
  22. {
  23. parent::__construct();
  24. if ($file === null) {
  25. throw new InvalidArgumentException('file is null');
  26. }
  27. $file = (string) $file;
  28. if (!is_file($file)) {
  29. throw new ZipException("File {$file} does not exist.");
  30. }
  31. if (!is_readable($file)) {
  32. throw new ZipException("The '{$file}' file could not be read. Check permissions.");
  33. }
  34. $this->file = $file;
  35. }
  36. /**
  37. * Returns an string content of the given entry.
  38. *
  39. * @return string|null
  40. */
  41. public function getEntryContent()
  42. {
  43. if (!is_file($this->file)) {
  44. throw new RuntimeException("File {$this->file} does not exist.");
  45. }
  46. return file_get_contents($this->file);
  47. }
  48. }