|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Kami\Component\RequestProcessor; |
|
5
|
|
|
|
|
6
|
|
|
use Closure; |
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ArtifactCollection |
|
11
|
|
|
* |
|
12
|
|
|
* @package Kami\Component\RequestProcessor |
|
13
|
|
|
*/ |
|
14
|
|
|
class ArtifactCollection extends ArrayCollection |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
19 |
|
public function __construct(array $elements = []) |
|
18
|
|
|
{ |
|
19
|
19 |
|
parent::__construct([]); |
|
20
|
|
|
|
|
21
|
19 |
|
foreach ($elements as $element) { |
|
22
|
10 |
|
$this->set($element->getName(), $element); |
|
23
|
|
|
} |
|
24
|
19 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param $element |
|
28
|
|
|
* |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
7 |
|
public function add($element) : void |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
7 |
|
if (!$element instanceof Artifact) { |
|
35
|
1 |
|
throw new \InvalidArgumentException('You can add only Artifact to ArtifactCollection'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
$this->set($element->getName(), $element); |
|
39
|
6 |
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
16 |
|
public function set($key, $value) : void |
|
43
|
|
|
{ |
|
44
|
16 |
|
if (!$value instanceof Artifact) { |
|
45
|
1 |
|
throw new \InvalidArgumentException('You can add only Artifact to ArtifactCollection'); |
|
46
|
|
|
} |
|
47
|
15 |
|
parent::set($key, $value); |
|
48
|
15 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param array $names |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
2 |
|
public function hasRequired(array $names) : bool |
|
56
|
|
|
{ |
|
57
|
2 |
|
foreach ($names as $name) { |
|
58
|
2 |
|
if (!$this->containsKey($name)) { |
|
59
|
|
|
|
|
60
|
2 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $requestedArtifacts |
|
69
|
|
|
* |
|
70
|
|
|
* @throws ProcessingException |
|
71
|
|
|
* |
|
72
|
|
|
* @return ArtifactCollection |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function getRequested(array $requestedArtifacts) : ArtifactCollection |
|
75
|
|
|
{ |
|
76
|
4 |
|
$requested = []; |
|
77
|
4 |
|
foreach ($requestedArtifacts as $requestedArtifact) { |
|
78
|
2 |
|
$artifact = $this->get($requestedArtifact); |
|
79
|
2 |
|
if (!$artifact) { |
|
80
|
1 |
|
throw new ProcessingException( |
|
81
|
1 |
|
sprintf('You don\'t have requested artifact "%s" yet', $requestedArtifact) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
2 |
|
$requested[] = $artifact; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
return new ArtifactCollection($requested); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |