SimpleFile.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV;
  4. /**
  5. * SimpleFile.
  6. *
  7. * The 'SimpleFile' class is used to easily add read-only immutable files to
  8. * the directory structure. One usecase would be to add a 'readme.txt' to a
  9. * root of a webserver with some standard content.
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class SimpleFile extends File
  16. {
  17. /**
  18. * File contents.
  19. *
  20. * @var string
  21. */
  22. protected $contents = [];
  23. /**
  24. * Name of this resource.
  25. *
  26. * @var string
  27. */
  28. protected $name;
  29. /**
  30. * A mimetype, such as 'text/plain' or 'text/html'.
  31. *
  32. * @var string
  33. */
  34. protected $mimeType;
  35. /**
  36. * Creates this node.
  37. *
  38. * The name of the node must be passed, as well as the contents of the
  39. * file.
  40. *
  41. * @param string $name
  42. * @param string $contents
  43. * @param string|null $mimeType
  44. */
  45. public function __construct($name, $contents, $mimeType = null)
  46. {
  47. $this->name = $name;
  48. $this->contents = $contents;
  49. $this->mimeType = $mimeType;
  50. }
  51. /**
  52. * Returns the node name for this file.
  53. *
  54. * This name is used to construct the url.
  55. *
  56. * @return string
  57. */
  58. public function getName()
  59. {
  60. return $this->name;
  61. }
  62. /**
  63. * Returns the data.
  64. *
  65. * This method may either return a string or a readable stream resource
  66. *
  67. * @return mixed
  68. */
  69. public function get()
  70. {
  71. return $this->contents;
  72. }
  73. /**
  74. * Returns the size of the file, in bytes.
  75. *
  76. * @return int
  77. */
  78. public function getSize()
  79. {
  80. return strlen($this->contents);
  81. }
  82. /**
  83. * Returns the ETag for a file.
  84. *
  85. * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
  86. * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
  87. *
  88. * Return null if the ETag can not effectively be determined
  89. *
  90. * @return string
  91. */
  92. public function getETag()
  93. {
  94. return '"'.sha1($this->contents).'"';
  95. }
  96. /**
  97. * Returns the mime-type for a file.
  98. *
  99. * If null is returned, we'll assume application/octet-stream
  100. *
  101. * @return string
  102. */
  103. public function getContentType()
  104. {
  105. return $this->mimeType;
  106. }
  107. }