1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\FileWaiter; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Psr\Http\Message\StreamInterface; |
9
|
|
|
use Stadly\FileWaiter\Exception\StreamCouldNotBeOpened; |
10
|
|
|
use Stadly\Http\Header\Value\Date; |
11
|
|
|
use Stadly\Http\Header\Value\EntityTag\EntityTag; |
12
|
|
|
use Stadly\Http\Header\Value\MediaType\MediaType; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class for handling files. |
16
|
|
|
*/ |
17
|
|
|
final class File |
18
|
|
|
{ |
19
|
|
|
public const NO_INFO = 0; |
20
|
|
|
public const FILE_NAME = 1; |
21
|
|
|
public const FILE_SIZE = 2; |
22
|
|
|
public const MEDIA_TYPE = 4; |
23
|
|
|
public const LAST_MODIFIED_DATE = 8; |
24
|
|
|
public const ENTITY_TAG = 16; |
25
|
|
|
public const ALL_INFO = 32 - 1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Adapter Adapter used to access the file in its file system. |
29
|
|
|
*/ |
30
|
|
|
private $adapter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null File name. |
34
|
|
|
*/ |
35
|
|
|
private $fileName = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int|null File size. |
39
|
|
|
*/ |
40
|
|
|
private $fileSize = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MediaType|null Media type of the file. |
44
|
|
|
*/ |
45
|
|
|
private $mediaType = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Date|null Date when the file was last modified. |
49
|
|
|
*/ |
50
|
|
|
private $lastModifiedDate = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var EntityTag|null Entity tag for the file. |
54
|
|
|
*/ |
55
|
|
|
private $entityTag = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor. |
59
|
|
|
* |
60
|
|
|
* @param Adapter $adapter Adapter used to access the file in its file system. |
61
|
|
|
* @param int $populateInfo Bit mask specifying which information to populate from the file's adapter. |
62
|
|
|
*/ |
63
|
7 |
|
public function __construct(Adapter $adapter, int $populateInfo = self::ALL_INFO) |
64
|
|
|
{ |
65
|
7 |
|
$this->adapter = $adapter; |
66
|
7 |
|
$this->populateInfo($populateInfo); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return StreamInterface File stream that can be used to read from the file. |
71
|
|
|
* @throws StreamCouldNotBeOpened If the file stream could not be opened. |
72
|
|
|
*/ |
73
|
1 |
|
public function getFileStream(): StreamInterface |
74
|
|
|
{ |
75
|
1 |
|
return $this->adapter->getFileStream(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Populate information retrieved from the file's adapter. |
80
|
|
|
* |
81
|
|
|
* @param int $whichInfo Bit mask specifying which information to populate from the file's adapter. |
82
|
|
|
*/ |
83
|
7 |
|
public function populateInfo(int $whichInfo = self::ALL_INFO): void |
84
|
|
|
{ |
85
|
7 |
|
if ((self::FILE_NAME & $whichInfo) !== 0) { |
86
|
2 |
|
$this->setFileName($this->adapter->getFileName()); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
if ((self::FILE_SIZE & $whichInfo) !== 0) { |
90
|
2 |
|
$this->setFileSize($this->adapter->getFileSize()); |
91
|
|
|
} |
92
|
|
|
|
93
|
7 |
|
if ((self::MEDIA_TYPE & $whichInfo) !== 0) { |
94
|
2 |
|
$this->setMediaType($this->adapter->getMediaType()); |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
if ((self::LAST_MODIFIED_DATE & $whichInfo) !== 0) { |
98
|
2 |
|
$this->setLastModifiedDate($this->adapter->getLastModifiedDate()); |
99
|
|
|
} |
100
|
|
|
|
101
|
7 |
|
if ((self::ENTITY_TAG & $whichInfo) !== 0) { |
102
|
2 |
|
$this->setEntityTag($this->adapter->getEntityTag()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string|null File name, or null if not set. |
108
|
|
|
*/ |
109
|
1 |
|
public function getFileName(): ?string |
110
|
|
|
{ |
111
|
1 |
|
return $this->fileName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string|null $fileName File name. |
116
|
|
|
*/ |
117
|
2 |
|
public function setFileName(?string $fileName): void |
118
|
|
|
{ |
119
|
2 |
|
$this->fileName = $fileName; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return int|null File size, or null if not set. |
124
|
|
|
*/ |
125
|
1 |
|
public function getFileSize(): ?int |
126
|
|
|
{ |
127
|
1 |
|
return $this->fileSize; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param int|null $fileSize File size. |
132
|
|
|
*/ |
133
|
4 |
|
public function setFileSize(?int $fileSize): void |
134
|
|
|
{ |
135
|
|
|
// File size must be non-negative. |
136
|
4 |
|
if ($fileSize < 0) { |
137
|
1 |
|
throw new InvalidArgumentException('Invalid file size: ' . $fileSize); |
138
|
|
|
} |
139
|
|
|
|
140
|
4 |
|
$this->fileSize = $fileSize; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return MediaType|null Media type of the file, or null if not set. |
145
|
|
|
*/ |
146
|
1 |
|
public function getMediaType(): ?MediaType |
147
|
|
|
{ |
148
|
1 |
|
return $this->mediaType; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param MediaType|null $mediaType Media type of the file. |
153
|
|
|
*/ |
154
|
2 |
|
public function setMediaType(?MediaType $mediaType): void |
155
|
|
|
{ |
156
|
2 |
|
$this->mediaType = $mediaType; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return Date|null Date when the file was last modified, or null if not set. |
161
|
|
|
*/ |
162
|
1 |
|
public function getLastModifiedDate(): ?Date |
163
|
|
|
{ |
164
|
1 |
|
return $this->lastModifiedDate; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Date|null $date Date when the file was last modified. |
169
|
|
|
*/ |
170
|
2 |
|
public function setLastModifiedDate(?Date $date): void |
171
|
|
|
{ |
172
|
2 |
|
$this->lastModifiedDate = $date; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return EntityTag|null Entity tag for the file, or null if not set. |
177
|
|
|
*/ |
178
|
1 |
|
public function getEntityTag(): ?EntityTag |
179
|
|
|
{ |
180
|
1 |
|
return $this->entityTag; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param EntityTag|null $entityTag Entity tag for the file. |
185
|
|
|
*/ |
186
|
2 |
|
public function setEntityTag(?EntityTag $entityTag): void |
187
|
|
|
{ |
188
|
2 |
|
$this->entityTag = $entityTag; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|