1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 SURFnet B.V. |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\MiddlewareBundle\EventSourcing; |
20
|
|
|
|
21
|
|
|
use ArrayIterator; |
22
|
|
|
use IteratorAggregate; |
23
|
|
|
use Surfnet\StepupMiddleware\MiddlewareBundle\Exception\InvalidArgumentException; |
24
|
|
|
|
25
|
|
|
final class EventCollection implements IteratorAggregate |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $eventNames = []; |
31
|
|
|
|
32
|
|
|
public function __construct(array $eventNames) |
33
|
|
|
{ |
34
|
|
|
foreach ($eventNames as $eventName) { |
35
|
|
|
if (!is_string($eventName) || empty($eventName)) { |
36
|
|
|
throw InvalidArgumentException::invalidType('non-empty string', 'eventName', $eventName); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!class_exists($eventName)) { |
40
|
|
|
throw new InvalidArgumentException(sprintf( |
41
|
|
|
'Cannot create EventCollection: class "%s" does not exist', |
42
|
|
|
$eventName |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->eventNames[] = $eventName; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $subset |
52
|
|
|
* @return EventCollection |
53
|
|
|
*/ |
54
|
|
|
public function select(array $subset) |
55
|
|
|
{ |
56
|
|
|
$nonAvailableEventNames = array_diff($subset, $this->eventNames); |
57
|
|
|
|
58
|
|
|
if (!empty($nonAvailableEventNames)) { |
59
|
|
|
throw new InvalidArgumentException( |
60
|
|
|
sprintf( |
61
|
|
|
'Subset of event names contains event names not present in collection: %s', |
62
|
|
|
implode(', ', $nonAvailableEventNames) |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new self($subset); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getIterator() |
71
|
|
|
{ |
72
|
|
|
return new ArrayIterator($this->eventNames); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|