1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fracture\Http; |
4
|
|
|
|
5
|
|
|
class FileBagBuilder |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $uploadedFileBuilder = null; |
9
|
|
|
|
10
|
|
|
|
11
|
6 |
|
public function __construct(UploadedFileBuilder $builder) |
12
|
|
|
{ |
13
|
6 |
|
$this->uploadedFileBuilder = $builder; |
14
|
6 |
|
} |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Used for producinf a FileBag instance from standard $_FILES values |
19
|
|
|
* |
20
|
|
|
* @param array $list |
21
|
|
|
* @return FileBag |
22
|
|
|
*/ |
23
|
6 |
|
public function create($list) |
24
|
|
|
{ |
25
|
6 |
|
$instance = new FileBag; |
26
|
|
|
|
27
|
6 |
|
foreach ($list as $key => $value) { |
28
|
5 |
|
$item = $this->createItem($value); |
29
|
5 |
|
$instance[$key] = $item; |
30
|
|
|
} |
31
|
|
|
|
32
|
6 |
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
|
36
|
5 |
|
private function createItem($params) |
37
|
|
|
{ |
38
|
|
|
// when using multiple "array inputs", the data ends up formated |
39
|
|
|
// as 'name' => [first, second, ..] |
40
|
5 |
|
if (isset($params['name']) === true && is_array($params['name']) === true) { |
41
|
1 |
|
return $this->createFromList($params); |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
return $this->uploadedFileBuilder->create($params); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* When using <input type="file" name="foobar[]"> type of definitions, |
50
|
|
|
* this method is repsonsible for re-packeging the inputs into a sub-filebag |
51
|
|
|
*/ |
52
|
1 |
|
private function createFromList($list) |
53
|
|
|
{ |
54
|
1 |
|
$instance = new FileBag; |
55
|
|
|
|
56
|
1 |
|
foreach (array_keys($list['name']) as $key) { |
57
|
|
|
$params = [ |
58
|
1 |
|
'name' => $list['name'][$key], |
59
|
1 |
|
'type' => $list['type'][$key], |
60
|
1 |
|
'tmp_name' => $list['tmp_name'][$key], |
61
|
1 |
|
'error' => $list['error'][$key], |
62
|
1 |
|
'size' => $list['size'][$key], |
63
|
|
|
]; |
64
|
1 |
|
$file = $this->uploadedFileBuilder->create($params); |
65
|
1 |
|
$instance[] = $file; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return $instance; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|