Completed
Push — master ( e2529c...2bd67e )
by Taosikai
29:08 queued 14:09
created

FileInfo::humanReadableToBytes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
/**
3
 * slince upload handler library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Upload;
7
8
use Slince\Upload\Exception\UploadException;
9
10
class FileInfo
11
{
12
    /**
13
     * has error
14
     * @var boolean
15
     */
16
    public $hasError;
17
18
    /**
19
     * tmp filename, $_FILES['upfile']['tmp_name']
20
     * @var string
21
     */
22
    protected $tmpName;
23
24
    /**
25
     * Original file name(the name saved on the client); $_FILES['upfile']['name']
26
     * @var string
27
     */
28
    protected $originName;
29
30
    /**
31
     * errors;$_FILES['error']
32
     * @var int
33
     */
34
    protected $error;
35
36
    /**
37
     * file size(bytes);$_FILES['upfile']['size']
38
     * @var int
39
     */
40
    protected $size;
41
42
    /**
43
     * file type;$_FILES['upfile']['type']
44
     * @var string
45
     */
46
    protected $type;
47
48
    /**
49
     * file mime type
50
     * @var string
51
     */
52
    protected $mimeType;
53
54
    /**
55
     * error code
56
     * @var int
57
     */
58
    protected $errorCode;
59
60
    /**
61
     * error message
62
     * @var string
63
     */
64
    protected $errorMsg;
65
66
    /**
67
     * The file path after upload
68
     * @var string
69
     */
70
    protected $path;
71
72
    public function __construct(array $file)
73
    {
74
        if (!isset($file['tmp_name'])
75
            || !isset($file['name'])
76
            || !isset($file['error'])
77
            || !isset($file['size'])
78
            || !isset($file['type'])
79
        ) {
80
            throw new UploadException("Invalid file array");
81
        }
82
        $this->tmpName = $file['tmp_name'];
83
        $this->originName = $file['name'];
84
        $this->error = $file['error'];
85
        $this->size = $file['size'];
86
        $this->type = $file['type'];
87
    }
88
89
    /**
90
     * Create instance from array
91
     * @param array $info
92
     * @return FileInfo
93
     */
94
    public static function fromArray(array $info)
95
    {
96
        return static::createFromArray($info);
0 ignored issues
show
Deprecated Code introduced by slince
The method Slince\Upload\FileInfo::createFromArray() has been deprecated.

This method has been deprecated.

Loading history...
97
    }
98
99
    /**
100
     * Create instance from array
101
     * @param array $info
102
     * @return FileInfo
103
     * @deprecated
104
     */
105
    public static function createFromArray(array $info)
106
    {
107
        return new static($info);
108
    }
109
110
    /**
111
     * gets file size
112
     * @return int
113
     */
114
    public function getSize()
115
    {
116
        return $this->size;
117
    }
118
119
    /**
120
     * gets file mime type
121
     * @return string
122
     */
123
    public function getMimeType()
124
    {
125
        if (is_null($this->mimeType)) {
126
            $this->mimeType = $this->detectMimeType();
127
        }
128
        return $this->mimeType;
129
    }
130
131
    /**
132
     * detect file mime type
133
     * @return string
134
     */
135
    protected function detectMimeType()
136
    {
137
        $mimeType = null;
138
        if (class_exists('finfo')) {
139
            $finfo = new \finfo(FILEINFO_MIME_TYPE);
140
            $mimeType = $finfo->file($this->tmpName);
141
        }
142
        if (!$mimeType) {
0 ignored issues
show
Bug Best Practice introduced by slince
The expression $mimeType of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
143
            $mimeType = MimeTypeStore::getMimeType($this->getExtension());
144
            $mimeType = is_array($mimeType) ? reset($mimeType) : $mimeType;
145
        }
146
        return $mimeType;
147
    }
148
149
    /**
150
     * gets file extension
151
     * @return string
152
     */
153
    public function getExtension()
154
    {
155
        return pathinfo($this->originName, PATHINFO_EXTENSION);
156
    }
157
158
    /**
159
     * gets file tmp name
160
     * @return string
161
     */
162
    public function getTmpName()
163
    {
164
        return $this->tmpName;
165
    }
166
167
    /**
168
     * gets original file name
169
     * @return string
170
     */
171
    public function getOriginName()
172
    {
173
        return $this->originName;
174
    }
175
176
    /**
177
     * gets file type
178
     * @return string
179
     */
180
    public function getType()
181
    {
182
        return $this->type;
183
    }
184
185
    /**
186
     * gets error($_FILES['upfile']['error'])
187
     * @return int
188
     */
189
    public function getError()
190
    {
191
        return $this->error;
192
    }
193
194
    /**
195
     * set error code
196
     * @param int $code
197
     */
198
    public function setErrorCode($code)
199
    {
200
        $this->errorCode = $code;
201
    }
202
203
    /**
204
     * gets final error code
205
     * @return int
206
     */
207
    public function getErrorCode()
208
    {
209
        return $this->errorCode;
210
    }
211
212
    /**
213
     * set error message
214
     * @param string $msg
215
     */
216
    public function setErrorMsg($msg)
217
    {
218
        $this->errorMsg = $msg;
219
    }
220
221
    /**
222
     * get error message
223
     * @return string
224
     */
225
    public function getErrorMsg()
226
    {
227
        return $this->errorMsg;
228
    }
229
230
    /**
231
     * set file path
232
     * @param string $path
233
     */
234
    public function setPath($path)
235
    {
236
        $this->path = $path;
237
    }
238
239
    /**
240
     * get file path
241
     * @return string
242
     */
243
    public function getPath()
244
    {
245
        return $this->path;
246
    }
247
248
    /**
249
     * whether there is an error
250
     * @return bool
251
     */
252
    public function hasError()
253
    {
254
        return $this->hasError;
255
    }
256
257
    /**
258
     * set has error
259
     * @param $result
260
     */
261
    public function setHasError($result)
262
    {
263
        $this->hasError = $result;
264
    }
265
266
    /**
267
     * Convert human readable file size (e.g. "10K" or "3M") into bytes
268
     * @link https://github.com/brandonsavage/Upload/blob/master/src/Upload/File.php#L446
269
     * @param  string $input
270
     * @return int
271
     */
272
    public static function humanReadableToBytes($input)
273
    {
274
        $number = (int)$input;
275
        $units = array(
276
            'b' => 1,
277
            'k' => 1024,
278
            'm' => 1048576,
279
            'g' => 1073741824
280
        );
281
        $unit = strtolower(substr($input, -1));
282
        if (isset($units[$unit])) {
283
            $number = $number * $units[$unit];
284
        }
285
        return $number;
286
    }
287
}
288