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 $description; |
28
|
|
|
|
29
|
|
|
/** @var DateTime */ |
30
|
|
|
protected $uploadedAt; |
31
|
|
|
|
32
|
|
|
/** @var FileCategory */ |
33
|
|
|
protected $category; |
34
|
|
|
|
35
|
|
|
/** @var bool */ |
36
|
|
|
protected $writable; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* File constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $id |
42
|
|
|
* @param string $filesystemName |
43
|
|
|
* @param string $publicName |
44
|
|
|
* @param string $description |
45
|
|
|
* @param FileCategory|null $category |
46
|
|
|
* @param DateTime|null $uploadedAt |
47
|
|
|
* @param bool $writable |
48
|
|
|
* |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
string $id, |
53
|
|
|
string $filesystemName, |
54
|
|
|
string $publicName, |
55
|
|
|
string $description, |
56
|
|
|
FileCategory $category = null, |
57
|
|
|
DateTime $uploadedAt = null, |
58
|
|
|
bool $writable = false |
59
|
|
|
) { |
60
|
|
|
$this->id = $id; |
61
|
|
|
$this->filesystemName = $filesystemName; |
62
|
|
|
$this->oldFilesystemName = $filesystemName; |
63
|
|
|
$this->publicName = $publicName; |
64
|
|
|
$this->description = $description; |
65
|
|
|
$this->category = $category; |
66
|
|
|
$this->uploadedAt = $uploadedAt ?: new DateTime(); |
67
|
|
|
$this->writable = $writable; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getId() |
74
|
|
|
{ |
75
|
|
|
return $this->id; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $id |
80
|
|
|
*/ |
81
|
|
|
public function setId($id) |
82
|
|
|
{ |
83
|
|
|
$this->id = $id; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getFilesystemName(): string |
90
|
|
|
{ |
91
|
|
|
return $this->filesystemName; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $filesystemName |
96
|
|
|
* |
97
|
|
|
* @return File |
98
|
|
|
*/ |
99
|
|
|
public function setFilesystemName(string $filesystemName): File |
100
|
|
|
{ |
101
|
|
|
$this->filesystemName = $filesystemName; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getOldFilesystemName(): string |
110
|
|
|
{ |
111
|
|
|
return $this->oldFilesystemName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function isFileUploaded(): bool |
118
|
|
|
{ |
119
|
|
|
return $this->oldFilesystemName !== $this->filesystemName; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getPublicName(): string |
126
|
|
|
{ |
127
|
|
|
return $this->publicName; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $file |
132
|
|
|
* |
133
|
|
|
* @return File |
134
|
|
|
*/ |
135
|
|
|
public function setPublicName(string $publicName): File |
136
|
|
|
{ |
137
|
|
|
$this->publicName = $publicName; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
|
|
public function getDescription(): string |
146
|
|
|
{ |
147
|
|
|
return $this->description; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $description |
152
|
|
|
* |
153
|
|
|
* @return File |
154
|
|
|
*/ |
155
|
|
|
public function setDescription(string $description): File |
156
|
|
|
{ |
157
|
|
|
$this->description = $description; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return FileCategory |
164
|
|
|
*/ |
165
|
|
|
public function getCategory(): FileCategory |
166
|
|
|
{ |
167
|
|
|
if (null === $this->category) { |
168
|
|
|
throw new \RuntimeException('Category is missing from file'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->category; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param FileCategory $category |
176
|
|
|
* |
177
|
|
|
* @return File |
178
|
|
|
*/ |
179
|
|
|
public function setCategory(FileCategory $category): File |
180
|
|
|
{ |
181
|
|
|
$this->category = $category; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return DateTime |
188
|
|
|
*/ |
189
|
|
|
public function getUploadedAt(): DateTime |
190
|
|
|
{ |
191
|
|
|
return $this->uploadedAt; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $uploadedAt |
196
|
|
|
* |
197
|
|
|
* @return File |
198
|
|
|
*/ |
199
|
|
|
public function setUploadedAt(\DateTime $uploadedAt): File |
200
|
|
|
{ |
201
|
|
|
$this->uploadedAt = $uploadedAt; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
|
public function isWritable(): bool |
210
|
|
|
{ |
211
|
|
|
return $this->writable; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param bool $writable |
216
|
|
|
*/ |
217
|
|
|
public function setWritable(bool $writable): File |
218
|
|
|
{ |
219
|
|
|
$this->writable = $writable; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return string |
226
|
|
|
*/ |
227
|
|
|
public function __toString(): string |
228
|
|
|
{ |
229
|
|
|
if (!$this->publicName) { |
230
|
|
|
return '#' . $this->getId(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $this->publicName; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function toJSON(): string |
240
|
|
|
{ |
241
|
|
|
return json_encode( |
242
|
|
|
[ |
243
|
|
|
"id" => $this->getId(), |
244
|
|
|
] |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|