1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Domain\Entities; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Domain\Entities\IStringerEntity; |
8
|
|
|
use DateTime; |
9
|
|
|
|
10
|
|
|
class File implements IStringerEntity |
11
|
|
|
{ |
12
|
|
|
const DATE_FORMAT = 'Y-m-d'; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $id; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $filesystemName; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $oldFilesystemName; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $publicName; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $mime; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $description; |
31
|
|
|
|
32
|
|
|
/** @var DateTime */ |
33
|
|
|
protected $uploadedAt; |
34
|
|
|
|
35
|
|
|
/** @var FileCategory */ |
36
|
|
|
protected $category; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
protected $writable; |
40
|
|
|
|
41
|
|
|
/** @var string|null */ |
42
|
|
|
protected $content; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* File constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $id |
48
|
|
|
* @param string $filesystemName |
49
|
|
|
* @param string $publicName |
50
|
|
|
* @param string $mime |
51
|
|
|
* @param string $description |
52
|
|
|
* @param FileCategory|null $category |
53
|
|
|
* @param DateTime|null $uploadedAt |
54
|
|
|
* @param bool $writable |
55
|
|
|
* @param string|null $content |
56
|
|
|
* |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
string $id, |
61
|
|
|
string $filesystemName, |
62
|
|
|
string $publicName, |
63
|
|
|
string $mime, |
64
|
|
|
string $description, |
65
|
|
|
FileCategory $category = null, |
66
|
|
|
DateTime $uploadedAt = null, |
67
|
|
|
bool $writable = false, |
68
|
|
|
?string $content = null |
69
|
|
|
) { |
70
|
|
|
$this->id = $id; |
71
|
|
|
$this->filesystemName = $filesystemName; |
72
|
|
|
$this->oldFilesystemName = $filesystemName; |
73
|
|
|
$this->publicName = $publicName; |
74
|
|
|
$this->mime = $mime; |
75
|
|
|
$this->description = $description; |
76
|
|
|
$this->category = $category; |
77
|
|
|
$this->uploadedAt = $uploadedAt ?: new DateTime(); |
78
|
|
|
$this->writable = $writable; |
79
|
|
|
$this->content = $content; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getId() |
86
|
|
|
{ |
87
|
|
|
return $this->id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $id |
92
|
|
|
*/ |
93
|
|
|
public function setId($id) |
94
|
|
|
{ |
95
|
|
|
$this->id = $id; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getFilesystemName(): string |
102
|
|
|
{ |
103
|
|
|
return $this->filesystemName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $filesystemName |
108
|
|
|
* |
109
|
|
|
* @return File |
110
|
|
|
*/ |
111
|
|
|
public function setFilesystemName(string $filesystemName): File |
112
|
|
|
{ |
113
|
|
|
$this->filesystemName = $filesystemName; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getOldFilesystemName(): string |
122
|
|
|
{ |
123
|
|
|
return $this->oldFilesystemName; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function isFileUploaded(): bool |
130
|
|
|
{ |
131
|
|
|
return $this->oldFilesystemName !== $this->filesystemName; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getPublicName(): string |
138
|
|
|
{ |
139
|
|
|
return $this->publicName; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $file |
144
|
|
|
* |
145
|
|
|
* @return File |
146
|
|
|
*/ |
147
|
|
|
public function setPublicName(string $publicName): File |
148
|
|
|
{ |
149
|
|
|
$this->publicName = $publicName; |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getMime(): string |
158
|
|
|
{ |
159
|
|
|
return $this->mime; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $mime |
164
|
|
|
* |
165
|
|
|
* @return File |
166
|
|
|
*/ |
167
|
|
|
public function setMime(string $mime): File |
168
|
|
|
{ |
169
|
|
|
$this->mime = $mime; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getDescription(): string |
178
|
|
|
{ |
179
|
|
|
return $this->description; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $description |
184
|
|
|
* |
185
|
|
|
* @return File |
186
|
|
|
*/ |
187
|
|
|
public function setDescription(string $description): File |
188
|
|
|
{ |
189
|
|
|
$this->description = $description; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return FileCategory |
196
|
|
|
*/ |
197
|
|
|
public function getCategory(): FileCategory |
198
|
|
|
{ |
199
|
|
|
if (null === $this->category) { |
200
|
|
|
throw new \RuntimeException('Category is missing from file'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $this->category; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param FileCategory $category |
208
|
|
|
* |
209
|
|
|
* @return File |
210
|
|
|
*/ |
211
|
|
|
public function setCategory(FileCategory $category): File |
212
|
|
|
{ |
213
|
|
|
$this->category = $category; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return DateTime |
220
|
|
|
*/ |
221
|
|
|
public function getUploadedAt(): DateTime |
222
|
|
|
{ |
223
|
|
|
return $this->uploadedAt; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $uploadedAt |
228
|
|
|
* |
229
|
|
|
* @return File |
230
|
|
|
*/ |
231
|
|
|
public function setUploadedAt(\DateTime $uploadedAt): File |
232
|
|
|
{ |
233
|
|
|
$this->uploadedAt = $uploadedAt; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
|
|
public function isWritable(): bool |
242
|
|
|
{ |
243
|
|
|
return $this->writable; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param bool $writable |
248
|
|
|
*/ |
249
|
|
|
public function setWritable(bool $writable): File |
250
|
|
|
{ |
251
|
|
|
$this->writable = $writable; |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function hasContent(): bool |
260
|
|
|
{ |
261
|
|
|
return !($this->content === null); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return string|null |
266
|
|
|
*/ |
267
|
|
|
public function getContent(): ?string |
268
|
|
|
{ |
269
|
|
|
return $this->content; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param string|null $content |
274
|
|
|
*/ |
275
|
|
|
public function setContent(?string $content): void |
276
|
|
|
{ |
277
|
|
|
$this->content = $content; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function __toString(): string |
284
|
|
|
{ |
285
|
|
|
if (!$this->publicName) { |
286
|
|
|
return '#' . $this->getId(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $this->publicName; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public function toJSON(): string |
296
|
|
|
{ |
297
|
|
|
$data = [ |
298
|
|
|
'id' => $this->getId(), |
299
|
|
|
'name' => $this->getPublicName(), |
300
|
|
|
'mime' => $this->getMime(), |
301
|
|
|
'description' => $this->getDescription(), |
302
|
|
|
'category_id' => $this->getCategory()->getId(), |
303
|
|
|
'uploaded_at' => $this->getUploadedAt()->format(\DateTime::ISO8601), |
304
|
|
|
]; |
305
|
|
|
|
306
|
|
|
if ($this->hasContent()) { |
307
|
|
|
$data['data'] = $this->getContent(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return json_encode($data); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|