-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathInputFile.php
More file actions
318 lines (270 loc) · 5.77 KB
/
InputFile.php
File metadata and controls
318 lines (270 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Pecee\Http\Input;
use Pecee\Exceptions\InvalidArgumentException;
class InputFile implements IInputItem
{
/**
* @var string
*/
public string $index;
/**
* @var string
*/
public string $name;
/**
* @var string|null
*/
public ?string $filename = null;
/**
* @var int|null
*/
public ?int $size = null;
/**
* @var string|null
*/
public ?string $type = null;
/**
* @var int
*/
public int $errors = 0;
/**
* @var string|null
*/
public ?string $tmpName = null;
public function __construct(string $index)
{
$this->index = $index;
$this->errors = 0;
// Make the name human friendly, by replace _ with space
$this->name = ucfirst(str_replace('_', ' ', strtolower($this->index)));
}
/**
* Create from an array
*
* @param array $values
* @throws InvalidArgumentException
* @return static
*/
public static function createFromArray(array $values): self
{
if (isset($values['index']) === false) {
throw new InvalidArgumentException('Index key is required');
}
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
$values += [
'tmp_name' => null,
'type' => null,
'size' => null,
'name' => null,
'error' => null,
];
return (new self($values['index']))
->setSize((int)$values['size'])
->setError((int)$values['error'])
->setType($values['type'])
->setTmpName($values['tmp_name'])
->setFilename($values['name']);
}
/**
* @return string
*/
public function getIndex(): string
{
return $this->index;
}
/**
* Set input index
* @param string $index
* @return static
*/
public function setIndex(string $index): IInputItem
{
$this->index = $index;
return $this;
}
/**
* @return int|null
*/
public function getSize(): ?int
{
return $this->size;
}
/**
* Set file size
* @param int $size
* @return static
*/
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
/**
* Get mime-type of file
* @return string
*/
public function getMime(): string
{
return $this->getType();
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* Set type
* @param string $type
* @return static
*/
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
/**
* Returns extension without "."
*
* @return string
*/
public function getExtension(): string
{
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
}
/**
* Get human friendly name
*
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Set human friendly name.
* Useful for adding validation, etc.
*
* @param string $name
* @return static
*/
public function setName(string $name): IInputItem
{
$this->name = $name;
return $this;
}
/**
* Set filename
*
* @param string $name
* @return static
*/
public function setFilename(string $name): self
{
$this->filename = $name;
return $this;
}
/**
* Get filename
*
* @return string|null mixed
*/
public function getFilename(): ?string
{
return $this->filename;
}
/**
* Move the uploaded temporary file to it's new home
*
* @param string $destination
* @return bool
*/
public function move(string $destination): bool
{
return move_uploaded_file($this->tmpName, $destination);
}
/**
* Get file contents
*
* @return string
*/
public function getContents(): string
{
return file_get_contents($this->tmpName);
}
/**
* Return true if an upload error occurred.
*
* @return bool
*/
public function hasError(): bool
{
return ($this->getError() !== 0);
}
/**
* Get upload-error code.
*
* @return int|null
*/
public function getError(): ?int
{
return $this->errors;
}
/**
* Set error
*
* @param int|null $error
* @return static
*/
public function setError(?int $error): self
{
$this->errors = (int)$error;
return $this;
}
/**
* @return string
*/
public function getTmpName(): string
{
return $this->tmpName;
}
/**
* Set file temp. name
* @param string $name
* @return static
*/
public function setTmpName(string $name): self
{
$this->tmpName = $name;
return $this;
}
public function __toString(): string
{
return $this->getTmpName();
}
public function getValue(): string
{
return $this->getFilename();
}
/**
* @param mixed $value
* @return static
*/
public function setValue(mixed $value): IInputItem
{
$this->filename = $value;
return $this;
}
public function toArray(): array
{
return [
'tmp_name' => $this->tmpName,
'type' => $this->type,
'size' => $this->size,
'name' => $this->name,
'error' => $this->errors,
'filename' => $this->filename,
];
}
}