|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace IQParts\Content\Object; |
|
5
|
|
|
|
|
6
|
|
|
use IQParts\Content\MimeDetector; |
|
7
|
|
|
use League\Flysystem\File as FlyFile; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class File |
|
11
|
|
|
* @package IQParts\Content\Object |
|
12
|
|
|
*/ |
|
13
|
|
|
final class File extends Item |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var FlyFile |
|
17
|
|
|
*/ |
|
18
|
|
|
private $file; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $path; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $basename; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $size; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $extension; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $parent; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var \DateTime |
|
41
|
|
|
*/ |
|
42
|
|
|
private $date; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var false|int |
|
45
|
|
|
*/ |
|
46
|
|
|
private $timestamp; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
private $isEditable; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $content = null; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* PublicFile constructor. |
|
58
|
|
|
* @param FlyFile $file |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct(FlyFile $file = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($file !== null) { |
|
63
|
|
|
$editable = [ |
|
64
|
|
|
"js" => true, |
|
65
|
|
|
"css" => true, |
|
66
|
|
|
"html" => true, |
|
67
|
|
|
"svg" => true |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$this->file = $file; |
|
71
|
|
|
$this->path = $file->getPath(); |
|
72
|
|
|
$this->size = $file->getSize(); |
|
|
|
|
|
|
73
|
|
|
$this->basename = basename($file->getPath()); |
|
74
|
|
|
$this->date = new \DateTime(); |
|
75
|
|
|
$timestamp = $file->getTimestamp(); |
|
76
|
|
|
$this->date->setTimestamp($timestamp); |
|
77
|
|
|
$this->timestamp = $timestamp; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$dotPlace = strrpos($this->basename, '.'); |
|
80
|
|
|
if ($dotPlace !== false) $extension = substr($this->basename, $dotPlace + 1); |
|
81
|
|
|
else $extension = ''; |
|
82
|
|
|
$this->extension = $extension; |
|
83
|
|
|
|
|
84
|
|
|
$parentSep = strrpos($this->path, '/'); |
|
85
|
|
|
if ($parentSep !== false) { |
|
86
|
|
|
$parent = substr($this->path, 0, $parentSep); |
|
87
|
|
|
} else $parent = '/'; |
|
88
|
|
|
$this->parent = base64_encode($parent); |
|
89
|
|
|
|
|
90
|
|
|
$isEditable = $editable[$extension] ?? false; |
|
91
|
|
|
$content = false; |
|
92
|
|
|
if ($isEditable) { |
|
93
|
|
|
$content = $file->read(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($isEditable && $content !== false) { |
|
97
|
|
|
$this->content = $content; |
|
98
|
|
|
$this->isEditable = true; |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->isEditable = false; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getId(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return base64_encode($this->path); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return FlyFile |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getFile(): FlyFile |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->file; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getPath(): string |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->path; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return int |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getSize(): int |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->size; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getBasename(): string |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->basename; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getExtension() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->extension; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getParent() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->parent; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return \DateTime |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getDate(): \DateTime |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->date; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getTimestamp() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->timestamp; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getMD5(): string |
|
181
|
|
|
{ |
|
182
|
|
|
return md5($this->file->read()); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return false|string |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getContents() |
|
189
|
|
|
{ |
|
190
|
|
|
if ($this->content !== null) return $this->content; |
|
191
|
|
|
return $this->file->read(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return false|resource |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getStream() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->file->readStream(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return false|string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getMimetype() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->file->getMimetype(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return bool |
|
212
|
|
|
*/ |
|
213
|
|
|
public function isEditable(): bool |
|
214
|
|
|
{ |
|
215
|
|
|
return (new MimeDetector())->isText($this->basename); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
public function toArray(): array |
|
222
|
|
|
{ |
|
223
|
|
|
return [ |
|
224
|
|
|
'basename' => $this->getBasename(), |
|
225
|
|
|
'contents' => $this->isEditable() ? $this->getContents() : null, |
|
226
|
|
|
'date' => $this->getDate()->format(DATE_ISO8601), |
|
227
|
|
|
'extension' => $this->getExtension(), |
|
228
|
|
|
'id' => $this->getId(), |
|
229
|
|
|
'isEditable' => $this->isEditable(), |
|
230
|
|
|
'mimetype' => $this->getMimetype(), |
|
231
|
|
|
'md5' => $this->getMD5(), |
|
232
|
|
|
'parent' => $this->getParent(), |
|
233
|
|
|
'path' => $this->getPath(), |
|
234
|
|
|
'size' => $this->getSize(), |
|
235
|
|
|
'timestamp' => $this->getTimestamp() |
|
236
|
|
|
]; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return bool |
|
241
|
|
|
*/ |
|
242
|
|
|
function isDir(): bool |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
|
|
return false; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* @return bool |
|
249
|
|
|
*/ |
|
250
|
|
|
function isFile(): bool |
|
|
|
|
|
|
251
|
|
|
{ |
|
252
|
|
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param string $path |
|
257
|
|
|
* @param string $parent |
|
258
|
|
|
* @param int $timestamp |
|
259
|
|
|
* @return File |
|
260
|
|
|
*/ |
|
261
|
|
|
public static function fromVariables(string $path, string $parent, int $timestamp): self |
|
262
|
|
|
{ |
|
263
|
|
|
$file = new self; |
|
264
|
|
|
$file->path = $path; |
|
265
|
|
|
$file->parent = $parent; |
|
266
|
|
|
$file->timestamp = $timestamp; |
|
267
|
|
|
return $file; |
|
268
|
|
|
} |
|
269
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.