1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
class AssetsCollection |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array<string, array<string, array<string, Asset>>> |
12
|
|
|
*/ |
13
|
|
|
private array $assets = []; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
public function __construct(private readonly LoggerInterface $logger = new NullLogger()) |
17
|
|
|
{ |
18
|
|
|
} |
19
|
17 |
|
|
20
|
|
|
public function add(Asset $asset, string $group): void |
21
|
17 |
|
{ |
22
|
|
|
if (!$asset->isValid()) { |
23
|
|
|
$this->logger->notice(sprintf('Path invalid: %s', $asset->getOrigPath())); |
24
|
17 |
|
return; |
25
|
|
|
} |
26
|
17 |
|
|
27
|
3 |
|
if ($this->has($asset, $group)) { |
28
|
3 |
|
$this->logger->notice(sprintf('Duplicate path: %s', $asset->getOrigPath())); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
17 |
|
|
32
|
1 |
|
|
33
|
1 |
|
$this->assets[$asset->getType()->value][$group][$asset->getId()] = $asset; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function has(Asset $asset, string $group): bool |
37
|
17 |
|
{ |
38
|
|
|
if (isset($this->assets[$asset->getType()->value][$group][$asset->getId()])) { |
39
|
|
|
return true; |
40
|
17 |
|
} |
41
|
|
|
return false; |
42
|
17 |
|
} |
43
|
1 |
|
|
44
|
|
|
/** |
45
|
17 |
|
* @param AssetType $type |
46
|
|
|
* @param string $group |
47
|
|
|
* @return Asset[] |
48
|
|
|
*/ |
49
|
|
|
public function get(AssetType $type, string $group): array |
50
|
|
|
{ |
51
|
|
|
if (!isset($this->assets[$type->value][$group])) { |
52
|
|
|
return []; |
53
|
15 |
|
} |
54
|
|
|
return $this->assets[$type->value][$group]; |
55
|
15 |
|
} |
56
|
1 |
|
|
57
|
|
|
/** |
58
|
15 |
|
* @psalm-suppress MixedPropertyTypeCoercion |
59
|
|
|
*/ |
60
|
|
|
public function push(AssetsCollection $collection): void |
61
|
|
|
{ |
62
|
|
|
$this->assets = array_merge_recursive_distinct($this->getAssets(), $collection->getAssets()); |
63
|
|
|
} |
64
|
|
|
|
65
|
13 |
|
/** |
66
|
|
|
* @psalm-suppress MixedPropertyTypeCoercion |
67
|
13 |
|
*/ |
68
|
|
|
public function unshift(AssetsCollection $collection): void |
69
|
|
|
{ |
70
|
|
|
$this->assets = array_merge_recursive_distinct($collection->getAssets(), $this->getAssets()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
1 |
|
/** |
75
|
|
|
* @return array<string, array<string, array<string, Asset>>> |
76
|
1 |
|
*/ |
77
|
|
|
public function getAssets(): array |
78
|
|
|
{ |
79
|
|
|
return $this->assets; |
80
|
14 |
|
} |
81
|
|
|
} |
82
|
|
|
|