FileAttributes.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. declare(strict_types=1);
  3. namespace League\Flysystem;
  4. class FileAttributes implements StorageAttributes
  5. {
  6. use ProxyArrayAccessToProperties;
  7. /**
  8. * @var string
  9. */
  10. private $type = StorageAttributes::TYPE_FILE;
  11. /**
  12. * @var string
  13. */
  14. private $path;
  15. /**
  16. * @var int|null
  17. */
  18. private $fileSize;
  19. /**
  20. * @var string|null
  21. */
  22. private $visibility;
  23. /**
  24. * @var int|null
  25. */
  26. private $lastModified;
  27. /**
  28. * @var string|null
  29. */
  30. private $mimeType;
  31. /**
  32. * @var array
  33. */
  34. private $extraMetadata;
  35. public function __construct(
  36. string $path,
  37. ?int $fileSize = null,
  38. ?string $visibility = null,
  39. ?int $lastModified = null,
  40. ?string $mimeType = null,
  41. array $extraMetadata = []
  42. ) {
  43. $this->path = $path;
  44. $this->fileSize = $fileSize;
  45. $this->visibility = $visibility;
  46. $this->lastModified = $lastModified;
  47. $this->mimeType = $mimeType;
  48. $this->extraMetadata = $extraMetadata;
  49. }
  50. public function type(): string
  51. {
  52. return $this->type;
  53. }
  54. public function path(): string
  55. {
  56. return $this->path;
  57. }
  58. public function fileSize(): ?int
  59. {
  60. return $this->fileSize;
  61. }
  62. public function visibility(): ?string
  63. {
  64. return $this->visibility;
  65. }
  66. public function lastModified(): ?int
  67. {
  68. return $this->lastModified;
  69. }
  70. public function mimeType(): ?string
  71. {
  72. return $this->mimeType;
  73. }
  74. public function extraMetadata(): array
  75. {
  76. return $this->extraMetadata;
  77. }
  78. public function isFile(): bool
  79. {
  80. return true;
  81. }
  82. public function isDir(): bool
  83. {
  84. return false;
  85. }
  86. public function withPath(string $path): StorageAttributes
  87. {
  88. $clone = clone $this;
  89. $clone->path = $path;
  90. return $clone;
  91. }
  92. public static function fromArray(array $attributes): StorageAttributes
  93. {
  94. return new FileAttributes(
  95. $attributes[StorageAttributes::ATTRIBUTE_PATH],
  96. $attributes[StorageAttributes::ATTRIBUTE_FILE_SIZE] ?? null,
  97. $attributes[StorageAttributes::ATTRIBUTE_VISIBILITY] ?? null,
  98. $attributes[StorageAttributes::ATTRIBUTE_LAST_MODIFIED] ?? null,
  99. $attributes[StorageAttributes::ATTRIBUTE_MIME_TYPE] ?? null,
  100. $attributes[StorageAttributes::ATTRIBUTE_EXTRA_METADATA] ?? []
  101. );
  102. }
  103. public function jsonSerialize(): array
  104. {
  105. return [
  106. StorageAttributes::ATTRIBUTE_TYPE => self::TYPE_FILE,
  107. StorageAttributes::ATTRIBUTE_PATH => $this->path,
  108. StorageAttributes::ATTRIBUTE_FILE_SIZE => $this->fileSize,
  109. StorageAttributes::ATTRIBUTE_VISIBILITY => $this->visibility,
  110. StorageAttributes::ATTRIBUTE_LAST_MODIFIED => $this->lastModified,
  111. StorageAttributes::ATTRIBUTE_MIME_TYPE => $this->mimeType,
  112. StorageAttributes::ATTRIBUTE_EXTRA_METADATA => $this->extraMetadata,
  113. ];
  114. }
  115. }