|
8 | 8 | use ArrayAccess\RdapClient\Response\Data\EventActor; |
9 | 9 | use ArrayAccess\RdapClient\Response\Data\EventDate; |
10 | 10 | use ArrayAccess\RdapClient\Response\Data\Links; |
| 11 | +use ArrayIterator; |
| 12 | +use IteratorAggregate; |
| 13 | +use Traversable; |
11 | 14 |
|
12 | | -class EventsCollection extends AbstractRdapResponseDataRecursiveArrayEmptyName |
| 15 | +/** |
| 16 | + * @template-implements IteratorAggregate<array{ |
| 17 | + * eventAction?: EventAction, |
| 18 | + * eventActor?: EventActor, |
| 19 | + * eventDate?: EventDate, |
| 20 | + * links?: Links, |
| 21 | + * }> |
| 22 | + * @template-implements IteratorAggregate<string, EventAction|EventActor|EventDate|Links> |
| 23 | + */ |
| 24 | +class EventsCollection extends AbstractRdapResponseDataRecursiveArrayEmptyName implements IteratorAggregate |
13 | 25 | { |
| 26 | + /** |
| 27 | + * @var array<array-key, string> $allowedKeys |
| 28 | + */ |
14 | 29 | protected array $allowedKeys = [ |
15 | 30 | 'eventAction', // required |
16 | 31 | 'eventActor', |
17 | 32 | 'eventDate', // required |
18 | 33 | 'links', |
19 | 34 | ]; |
20 | 35 |
|
| 36 | + /** |
| 37 | + * @var array{ |
| 38 | + * eventAction?: EventAction, |
| 39 | + * eventActor?: EventActor, |
| 40 | + * eventDate?: EventDate, |
| 41 | + * links?: Links, |
| 42 | + * } $values |
| 43 | + */ |
| 44 | + protected array $values = []; |
| 45 | + |
21 | 46 | public function __construct(EventActor|EventAction|EventDate|Links ...$data) |
22 | 47 | { |
23 | | - $this->values = [ |
24 | | - 'eventAction' => null, |
25 | | - 'eventActor' => null, |
26 | | - 'eventDate' => null, |
27 | | - 'links' => null, |
28 | | - ]; |
29 | 48 | foreach ($data as $action) { |
30 | | - $this->values[$action->getName()] = $action; |
31 | | - } |
32 | | - foreach ($this->values as $key => $value) { |
33 | | - // skip required |
34 | | - if ($value || $key === 'eventAction' || $key === 'eventDate') { |
| 49 | + if ($action instanceof EventActor) { |
| 50 | + $this->values['eventActor'] = $action; |
| 51 | + continue; |
| 52 | + } |
| 53 | + if ($action instanceof EventDate) { |
| 54 | + $this->values['eventDate'] = $action; |
| 55 | + continue; |
| 56 | + } |
| 57 | + if ($action instanceof Links) { |
| 58 | + $this->values['links'] = $action; |
35 | 59 | continue; |
36 | 60 | } |
37 | | - unset($this->values[$key]); |
| 61 | + $this->values['eventAction'] = $action; |
38 | 62 | } |
| 63 | + $this->values = array_filter($this->values); |
39 | 64 | } |
40 | 65 |
|
| 66 | + /** |
| 67 | + * @return EventAction|null |
| 68 | + */ |
41 | 69 | public function getAction() : ?EventAction |
42 | 70 | { |
43 | 71 | return $this->values['eventAction']??null; |
44 | 72 | } |
45 | 73 |
|
| 74 | + /** |
| 75 | + * @return EventActor|null |
| 76 | + */ |
46 | 77 | public function getActor() : ?EventActor |
47 | 78 | { |
48 | 79 | return $this->values['eventActor']??null; |
49 | 80 | } |
50 | 81 |
|
| 82 | + /** |
| 83 | + * @return EventDate|null |
| 84 | + */ |
51 | 85 | public function getDate() : ?EventDate |
52 | 86 | { |
53 | 87 | return $this->values['eventDate']??null; |
54 | 88 | } |
55 | 89 |
|
| 90 | + /** |
| 91 | + * @return Links|null |
| 92 | + */ |
56 | 93 | public function getLinks() : ?Links |
57 | 94 | { |
58 | 95 | return $this->values['links']??null; |
59 | 96 | } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return array{ |
| 100 | + * eventAction?: EventAction, |
| 101 | + * eventActor?: EventActor, |
| 102 | + * eventDate?: EventDate, |
| 103 | + * links?: Links, |
| 104 | + * } |
| 105 | + */ |
| 106 | + public function getValues(): array |
| 107 | + { |
| 108 | + return $this->values; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @return Traversable<"eventAction"|"eventActor"|"eventDate"|"links", EventAction|EventActor|EventDate|Links> |
| 113 | + * @return Traversable<string, EventAction|EventActor|EventDate|Links> |
| 114 | + */ |
| 115 | + public function getIterator(): Traversable |
| 116 | + { |
| 117 | + return new ArrayIterator($this->getValues()); |
| 118 | + } |
60 | 119 | } |
0 commit comments