-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileList.php
129 lines (112 loc) · 2.54 KB
/
FileList.php
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
<?php
namespace ICanBoogie\HTTP;
use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use function count;
/**
* Represents a list of request files.
*
* @implements ArrayAccess<string, File>
* @implements IteratorAggregate<string, File>
*/
class FileList implements ArrayAccess, IteratorAggregate, Countable
{
/**
* Creates a {@see FileList} instance.
*
* @param array|FileList|null $files
*
* @return FileList
*/
public static function from(array|FileList|null $files): self
{
if ($files instanceof self) {
return clone $files;
}
if (!$files) {
return new self([]);
}
foreach ($files as &$file) {
$file = File::from($file);
}
return new self($files);
}
/**
* @var File[]
*/
private array $list = [];
/**
* @param array $files
*/
public function __construct(array $files = [])
{
foreach ($files as $id => $file) {
$this[$id] = $file;
}
}
/**
* Checks if a file exists.
*
* @param mixed $offset File identifier.
*/
public function offsetExists(mixed $offset): bool
{
return isset($this->list[$offset]);
}
/**
* Returns a file.
*
* @param mixed $offset File identifier.
*
* @return File|null A {@see File} instance, or `null` if it does not exist.
*/
public function offsetGet(mixed $offset): ?File
{
if (!$this->offsetExists($offset)) {
return null;
}
return $this->list[$offset];
}
/**
* Adds a file.
*
* @param mixed $offset File identifier.
* @param mixed|array|File|FileList $value File.
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (!($value instanceof File || $value instanceof FileList)) {
$value = File::from($value);
}
$this->list[$offset] = $value;
}
/**
* Removes a file.
*
* @param mixed $offset File identifier.
*/
public function offsetUnset(mixed $offset): void
{
unset($this->list[$offset]);
}
/**
* @inheritdoc
*
* @return ArrayIterator<string, File>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->list);
}
/**
* Returns the number of files in the collection.
*
* @inheritdoc
*/
public function count(): int
{
return count($this->list);
}
}