Skip to content

Commit b473be1

Browse files
committed
- add override method
- add doc comment phpstan & readabilities improvement
1 parent f895396 commit b473be1

File tree

1 file changed

+72
-13
lines changed

1 file changed

+72
-13
lines changed

src/Response/Data/Definitions/EventsCollection.php

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,112 @@
88
use ArrayAccess\RdapClient\Response\Data\EventActor;
99
use ArrayAccess\RdapClient\Response\Data\EventDate;
1010
use ArrayAccess\RdapClient\Response\Data\Links;
11+
use ArrayIterator;
12+
use IteratorAggregate;
13+
use Traversable;
1114

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
1325
{
26+
/**
27+
* @var array<array-key, string> $allowedKeys
28+
*/
1429
protected array $allowedKeys = [
1530
'eventAction', // required
1631
'eventActor',
1732
'eventDate', // required
1833
'links',
1934
];
2035

36+
/**
37+
* @var array{
38+
* eventAction?: EventAction,
39+
* eventActor?: EventActor,
40+
* eventDate?: EventDate,
41+
* links?: Links,
42+
* } $values
43+
*/
44+
protected array $values = [];
45+
2146
public function __construct(EventActor|EventAction|EventDate|Links ...$data)
2247
{
23-
$this->values = [
24-
'eventAction' => null,
25-
'eventActor' => null,
26-
'eventDate' => null,
27-
'links' => null,
28-
];
2948
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;
3559
continue;
3660
}
37-
unset($this->values[$key]);
61+
$this->values['eventAction'] = $action;
3862
}
63+
$this->values = array_filter($this->values);
3964
}
4065

66+
/**
67+
* @return EventAction|null
68+
*/
4169
public function getAction() : ?EventAction
4270
{
4371
return $this->values['eventAction']??null;
4472
}
4573

74+
/**
75+
* @return EventActor|null
76+
*/
4677
public function getActor() : ?EventActor
4778
{
4879
return $this->values['eventActor']??null;
4980
}
5081

82+
/**
83+
* @return EventDate|null
84+
*/
5185
public function getDate() : ?EventDate
5286
{
5387
return $this->values['eventDate']??null;
5488
}
5589

90+
/**
91+
* @return Links|null
92+
*/
5693
public function getLinks() : ?Links
5794
{
5895
return $this->values['links']??null;
5996
}
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+
}
60119
}

0 commit comments

Comments
 (0)