|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zapheus\Bridge\Psr; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Uploaded File |
|
9
|
|
|
* |
|
10
|
|
|
* @package Zapheus |
|
11
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class UploadedFile implements UploadedFileInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $file; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var integer|null |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $size; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var integer |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $error; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $name; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $media; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Initializes the uploaded file instance. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $file |
|
44
|
|
|
* @param integer|null $size |
|
45
|
|
|
* @param integer $error |
|
46
|
|
|
* @param string|null $name |
|
47
|
|
|
* @param string|null $media |
|
48
|
|
|
*/ |
|
49
|
48 |
|
public function __construct($file, $size = null, $error = UPLOAD_ERR_OK, $name = null, $media = null) |
|
50
|
|
|
{ |
|
51
|
48 |
|
$this->error = $error; |
|
52
|
|
|
|
|
53
|
48 |
|
$this->file = $file; |
|
54
|
|
|
|
|
55
|
48 |
|
$this->media = $media; |
|
56
|
|
|
|
|
57
|
48 |
|
$this->name = $name; |
|
58
|
|
|
|
|
59
|
48 |
|
$this->size = $size; |
|
60
|
48 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Retrieves the filename sent by the client. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string|null |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function getClientFilename() |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->name; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Retrieves the media type sent by the client. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string|null |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function getClientMediaType() |
|
78
|
|
|
{ |
|
79
|
3 |
|
return $this->media; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Retrieves the error associated with the uploaded file. |
|
84
|
|
|
* |
|
85
|
|
|
* @return integer |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function getError() |
|
88
|
|
|
{ |
|
89
|
3 |
|
return $this->error; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Retrieves the file size. |
|
94
|
|
|
* |
|
95
|
|
|
* @return integer|null |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function getSize() |
|
98
|
|
|
{ |
|
99
|
3 |
|
return $this->size; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieves a stream representing the uploaded file. |
|
104
|
|
|
* |
|
105
|
|
|
* @return \Psr\Http\Message\StreamInterface |
|
106
|
|
|
* |
|
107
|
|
|
* @throws \RuntimeException |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function getStream() |
|
110
|
|
|
{ |
|
111
|
|
|
// TODO: Add \RuntimeException |
|
112
|
|
|
|
|
113
|
3 |
|
$stream = fopen($this->file, 'r+'); |
|
114
|
|
|
|
|
115
|
3 |
|
$stream = $stream === false ? null : $stream; |
|
116
|
|
|
|
|
117
|
3 |
|
return new Stream($stream); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Move the uploaded file to a new location. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $target |
|
124
|
|
|
* |
|
125
|
|
|
* @throws \InvalidArgumentException |
|
126
|
|
|
* @throws \RuntimeException |
|
127
|
|
|
*/ |
|
128
|
3 |
|
public function moveTo($target) |
|
129
|
|
|
{ |
|
130
|
|
|
// TODO: Add \InvalidArgumentException |
|
131
|
|
|
// TODO: Add \RuntimeException |
|
132
|
|
|
|
|
133
|
3 |
|
rename($this->file, $target); |
|
134
|
3 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Parses the $_FILES into multiple \File instances. |
|
138
|
|
|
* |
|
139
|
|
|
* @param array $uploaded |
|
140
|
|
|
* @param array $files |
|
141
|
|
|
* @return \Zapheus\Http\Message\FileInterface[] |
|
142
|
|
|
*/ |
|
143
|
81 |
|
public static function normalize(array $uploaded, $files = array()) |
|
144
|
|
|
{ |
|
145
|
81 |
|
foreach (self::diverse($uploaded) as $name => $file) { |
|
146
|
81 |
|
list($files[$name], $items) = array($file, array()); |
|
147
|
|
|
|
|
148
|
81 |
|
if (isset($file['name']) === true) { |
|
149
|
27 |
|
foreach ($file['name'] as $key => $value) { |
|
150
|
27 |
|
$instance = self::create($file, $key); |
|
151
|
|
|
|
|
152
|
27 |
|
array_push($items, $instance); |
|
153
|
27 |
|
} |
|
154
|
|
|
|
|
155
|
27 |
|
$files[$name] = (array) $items; |
|
156
|
27 |
|
} |
|
157
|
81 |
|
} |
|
158
|
|
|
|
|
159
|
81 |
|
return $files; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Creates a new UploadedFile instance. |
|
164
|
|
|
* |
|
165
|
|
|
* @param array $file |
|
166
|
|
|
* @param integer $key |
|
167
|
|
|
* @return \Psr\Http\Message\UploadedFile |
|
168
|
|
|
*/ |
|
169
|
27 |
|
protected static function create(array $file, $key) |
|
170
|
|
|
{ |
|
171
|
27 |
|
$tmp = $file['tmp_name'][$key]; |
|
172
|
|
|
|
|
173
|
27 |
|
$size = $file['size'][$key]; |
|
174
|
|
|
|
|
175
|
27 |
|
$error = $file['error'][$key]; |
|
176
|
|
|
|
|
177
|
27 |
|
$original = $file['name'][$key]; |
|
178
|
|
|
|
|
179
|
27 |
|
$type = $file['type'][$key]; |
|
180
|
|
|
|
|
181
|
27 |
|
return new UploadedFile($tmp, $size, $error, $original, $type); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Diverse the $_FILES into a consistent result. |
|
186
|
|
|
* |
|
187
|
|
|
* @param array $uploaded |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
81 |
|
protected static function diverse(array $uploaded) |
|
191
|
|
|
{ |
|
192
|
81 |
|
$result = array(); |
|
193
|
|
|
|
|
194
|
81 |
|
foreach ($uploaded as $file => $item) { |
|
195
|
81 |
|
foreach ($item as $key => $value) { |
|
196
|
81 |
|
$diversed = (array) $value; |
|
197
|
|
|
|
|
198
|
81 |
|
$result[$file][$key] = $diversed; |
|
199
|
81 |
|
} |
|
200
|
81 |
|
} |
|
201
|
|
|
|
|
202
|
81 |
|
return $result; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|