-
Notifications
You must be signed in to change notification settings - Fork 8
/
InMemoryFile.php
75 lines (60 loc) · 1.44 KB
/
InMemoryFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace League\Flysystem\InMemory;
use const FILEINFO_MIME_TYPE;
use finfo;
/**
* @internal
*/
class InMemoryFile
{
private string $contents = '';
private int $lastModified = 0;
private ?string $visibility = null;
public function updateContents(string $contents, ?int $timestamp): void
{
$this->contents = $contents;
$this->lastModified = $timestamp ?? time();
}
public function lastModified(): int
{
return $this->lastModified;
}
public function withLastModified(int $lastModified): self
{
$clone = clone $this;
$clone->lastModified = $lastModified;
return $clone;
}
public function read(): string
{
return $this->contents;
}
/**
* @return resource
*/
public function readStream()
{
/** @var resource $stream */
$stream = fopen('php://temp', 'w+b');
fwrite($stream, $this->contents);
rewind($stream);
return $stream;
}
public function fileSize(): int
{
return strlen($this->contents);
}
public function mimeType(): string
{
return (string) (new finfo(FILEINFO_MIME_TYPE))->buffer($this->contents);
}
public function setVisibility(string $visibility): void
{
$this->visibility = $visibility;
}
public function visibility(): ?string
{
return $this->visibility;
}
}