1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
/**
|
4
|
|
|
* File.php
|
5
|
|
|
*
|
6
|
|
|
* An uploaded file.
|
7
|
|
|
*
|
8
|
|
|
* @package jaxon-upload
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]>
|
10
|
|
|
* @copyright 2017 Thierry Feuzeu <[email protected]>
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core
|
13
|
|
|
*/
|
14
|
|
|
|
15
|
|
|
namespace Jaxon\Upload\Manager;
|
16
|
|
|
|
17
|
|
|
use Jaxon\Request\Upload\FileInterface;
|
18
|
|
|
use League\Flysystem\Filesystem;
|
19
|
|
|
use Nyholm\Psr7\UploadedFile;
|
20
|
|
|
|
21
|
|
|
use function pathinfo;
|
22
|
|
|
|
23
|
|
|
class File implements FileInterface
|
24
|
|
|
{
|
25
|
|
|
/**
|
26
|
|
|
* The uploaded file type
|
27
|
|
|
*
|
28
|
|
|
* @var string
|
29
|
|
|
*/
|
30
|
|
|
protected $sType;
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* The uploaded file name, without the extension and sanitized
|
34
|
|
|
*
|
35
|
|
|
* @var string
|
36
|
|
|
*/
|
37
|
|
|
protected $sName;
|
38
|
|
|
|
39
|
|
|
/**
|
40
|
|
|
* The uploaded file name, with the extension
|
41
|
|
|
*
|
42
|
|
|
* @var string
|
43
|
|
|
*/
|
44
|
|
|
protected $sFilename;
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* The uploaded file path
|
48
|
|
|
*
|
49
|
|
|
* @var string
|
50
|
|
|
*/
|
51
|
|
|
protected $sPath;
|
52
|
|
|
|
53
|
|
|
/**
|
54
|
|
|
* The uploaded file size
|
55
|
|
|
*
|
56
|
|
|
* @var int
|
57
|
|
|
*/
|
58
|
|
|
protected $nSize;
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* The uploaded file extension
|
62
|
|
|
*
|
63
|
|
|
* @var string
|
64
|
|
|
*/
|
65
|
|
|
protected $sExtension;
|
66
|
|
|
|
67
|
|
|
/**
|
68
|
|
|
* The filesystem where the file is stored
|
69
|
|
|
*
|
70
|
|
|
* @var Filesystem
|
71
|
|
|
*/
|
72
|
|
|
protected $xFilesystem;
|
73
|
|
|
|
74
|
|
|
/**
|
75
|
|
|
* Create an instance of this class using data from an uploaded file.
|
76
|
|
|
*
|
77
|
|
|
* @param Filesystem $xFilesystem
|
78
|
|
|
* @param UploadedFile $xHttpFile
|
79
|
|
|
* @param string $sUploadDir
|
80
|
|
|
* @param string $sName
|
81
|
|
|
*
|
82
|
|
|
* @return File
|
83
|
|
|
*/
|
84
|
|
|
public static function fromHttpFile(Filesystem $xFilesystem,
|
85
|
|
|
UploadedFile $xHttpFile, string $sUploadDir, string $sName): File
|
86
|
|
|
{
|
87
|
|
|
$xFile = new File();
|
88
|
|
|
$xFile->xFilesystem = $xFilesystem;
|
89
|
|
|
$xFile->sType = $xHttpFile->getClientMediaType();
|
90
|
|
|
$xFile->sName = $sName;
|
91
|
|
|
$xFile->sFilename = $xHttpFile->getClientFilename();
|
92
|
|
|
$xFile->sExtension = pathinfo($xFile->sFilename, PATHINFO_EXTENSION);
|
|
|
|
|
93
|
|
|
$xFile->nSize = $xHttpFile->getSize();
|
94
|
|
|
$xFile->sPath = $sUploadDir . $xFile->sName . '.' . $xFile->sExtension;
|
|
|
|
|
95
|
|
|
return $xFile;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* Get the filesystem where the file is stored
|
100
|
|
|
*
|
101
|
|
|
* @return Filesystem
|
102
|
|
|
*/
|
103
|
|
|
public function filesystem(): Filesystem
|
104
|
|
|
{
|
105
|
|
|
return $this->xFilesystem;
|
106
|
|
|
}
|
107
|
|
|
|
108
|
|
|
/**
|
109
|
|
|
* Get the uploaded file type
|
110
|
|
|
*
|
111
|
|
|
* @return string
|
112
|
|
|
*/
|
113
|
|
|
public function type(): string
|
114
|
|
|
{
|
115
|
|
|
return $this->sType;
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
/**
|
119
|
|
|
* Get the uploaded file name, without the extension and slugified
|
120
|
|
|
*
|
121
|
|
|
* @return string
|
122
|
|
|
*/
|
123
|
|
|
public function name(): string
|
124
|
|
|
{
|
125
|
|
|
return $this->sName;
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
/**
|
129
|
|
|
* Get the uploaded file name, with the extension
|
130
|
|
|
*
|
131
|
|
|
* @return string
|
132
|
|
|
*/
|
133
|
|
|
public function filename(): string
|
134
|
|
|
{
|
135
|
|
|
return $this->sFilename;
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* Get the uploaded file path
|
140
|
|
|
*
|
141
|
|
|
* @return string
|
142
|
|
|
*/
|
143
|
|
|
public function path(): string
|
144
|
|
|
{
|
145
|
|
|
return $this->sPath;
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
/**
|
149
|
|
|
* Get the uploaded file size
|
150
|
|
|
*
|
151
|
|
|
* @return int
|
152
|
|
|
*/
|
153
|
|
|
public function size(): int
|
154
|
|
|
{
|
155
|
|
|
return $this->nSize;
|
156
|
|
|
}
|
157
|
|
|
|
158
|
|
|
/**
|
159
|
|
|
* Get the uploaded file extension
|
160
|
|
|
*
|
161
|
|
|
* @return string
|
162
|
|
|
*/
|
163
|
|
|
public function extension(): string
|
164
|
|
|
{
|
165
|
|
|
return $this->sExtension;
|
166
|
|
|
}
|
167
|
|
|
}
|
168
|
|
|
|
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
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. 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.