/
Files.php
81 lines (59 loc) · 1.61 KB
/
Files.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
<?php
namespace Centum\Http;
use Centum\Http\Exception\FileGroupAlreadyExistsException;
use Centum\Http\Exception\FileGroupNotFoundException;
use Centum\Interfaces\Http\FileGroupInterface;
use Centum\Interfaces\Http\FilesInterface;
class Files implements FilesInterface
{
/**
* @var array<non-empty-string, FileGroupInterface>
*/
protected array $fileGroups = [];
/**
* @param array<FileGroupInterface> $fileGroups
*/
public function __construct(array $fileGroups = [])
{
foreach ($fileGroups as $fileGroup) {
$this->add($fileGroup);
}
}
/**
* @throws FileGroupAlreadyExistsException
*/
public function add(FileGroupInterface $fileGroup): void
{
$id = $fileGroup->getID();
if (isset($this->fileGroups[$id])) {
throw new FileGroupAlreadyExistsException($id);
}
$this->fileGroups[$id] = $fileGroup;
}
public function has(string $id): bool
{
return isset($this->fileGroups[$id]);
}
/**
* @throws FileGroupNotFoundException
*/
public function get(string $id): FileGroupInterface
{
if (!isset($this->fileGroups[$id])) {
throw new FileGroupNotFoundException($id);
}
return $this->fileGroups[$id];
}
public function all(): array
{
return $this->fileGroups;
}
public function toArray(): array
{
$fileGroups = [];
foreach ($this->fileGroups as $id => $fileGroup) {
$fileGroups[$id] = $fileGroup->toArray();
}
return $fileGroups;
}
}