Skip to content

Commit 4ce254c

Browse files
committed
- code improvements
- add doc comment phpstan & readabilities improvement
1 parent cd96fa0 commit 4ce254c

File tree

1 file changed

+82
-15
lines changed

1 file changed

+82
-15
lines changed

src/Response/Data/Link.php

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@
44
namespace ArrayAccess\RdapClient\Response\Data;
55

66
use ArrayAccess\RdapClient\Response\Data\Abstracts\AbstractRdapResponseDataRecursiveArray;
7+
use IteratorAggregate;
78
use function array_filter;
8-
use function in_array;
99

10-
class Link extends AbstractRdapResponseDataRecursiveArray
10+
/**
11+
* @template-implements IteratorAggregate<string, Value|Rel|Href|HrefLang|Title|Media|Type>
12+
*/
13+
class Link extends AbstractRdapResponseDataRecursiveArray implements IteratorAggregate
1114
{
15+
/**
16+
* @var string $name The name of the object
17+
*/
1218
protected string $name = 'link';
1319

20+
/**
21+
* @var array<"value"|"rel"|"href"|"hreflang"|"title"|"media"|"type">
22+
*/
1423
protected array $allowedKeys = [
1524
'value',
1625
'rel',
@@ -21,62 +30,120 @@ class Link extends AbstractRdapResponseDataRecursiveArray
2130
'type',
2231
];
2332

33+
/**
34+
* @var array{
35+
* value?: Value,
36+
* rel?: Rel,
37+
* href?: Href,
38+
* hreflang?: HrefLang,
39+
* title?: Title,
40+
* media?: Media,
41+
* type?: Type,
42+
* } $values
43+
*/
44+
protected array $values = [];
45+
46+
/**
47+
* @param Value|Rel|Href|HrefLang|Title|Media|Type ...$args
48+
*/
2449
public function __construct(Value|Rel|Href|HrefLang|Title|Media|Type ...$args)
2550
{
26-
$this->values = [
27-
'value' => null,
28-
'rel' => null,
29-
'href' => null,
30-
'hreflang' => null,
31-
'title' => null,
32-
'media' => null,
33-
'type' => null
34-
];
51+
$this->values = [];
3552
foreach ($args as $arg) {
36-
$name = $arg->getName();
37-
if (!in_array($name, $this->allowedKeys)) {
53+
if ($arg instanceof Rel) {
54+
$this->values['rel'] = $arg;
55+
continue;
56+
}
57+
if ($arg instanceof HrefLang) {
58+
$this->values['hreflang'] = $arg;
59+
continue;
60+
}
61+
if ($arg instanceof Title) {
62+
$this->values['title'] = $arg;
63+
continue;
64+
}
65+
if ($arg instanceof Media) {
66+
$this->values['media'] = $arg;
67+
continue;
68+
}
69+
if ($arg instanceof Type) {
70+
$this->values['type'] = $arg;
71+
continue;
72+
}
73+
if ($arg instanceof Href) {
74+
$this->values['href'] = $arg;
3875
continue;
3976
}
40-
$this->values[$name] = $arg;
77+
$this->values['value'] = $arg;
4178
}
42-
$this->values = array_filter($this->values);
4379
}
4480

81+
/**
82+
* @return array<string, Value|Rel|Href|HrefLang|Title|Media|Type>
83+
*/
4584
public function getValues(): array
4685
{
4786
return array_filter($this->values);
4887
}
4988

89+
/**
90+
* Get the value
91+
* @return Value|null
92+
*/
5093
public function getValue() : ?Value
5194
{
5295
return $this->values['value']??null;
5396
}
5497

98+
/**
99+
* Get the rel
100+
* @return Rel|null
101+
*/
55102
public function getRel() : ?Rel
56103
{
57104
return $this->values['rel']??null;
58105
}
59106

107+
/**
108+
* Get the href
109+
* @return Href|null
110+
*/
60111
public function getHref() : ?Href
61112
{
62113
return $this->values['href']??null;
63114
}
64115

116+
/**
117+
* Get the hreflang
118+
* @return HrefLang|null
119+
*/
65120
public function getHrefLang() : ?HrefLang
66121
{
67122
return $this->values['hreflang']??null;
68123
}
69124

125+
/**
126+
* Get the title
127+
* @return Title|null
128+
*/
70129
public function getTitle() : ?Title
71130
{
72131
return $this->values['title']??null;
73132
}
74133

134+
/**
135+
* Get the media
136+
* @return Media|null
137+
*/
75138
public function getMedia() : ?Media
76139
{
77140
return $this->values['media']??null;
78141
}
79142

143+
/**
144+
* Get the type
145+
* @return Type|null
146+
*/
80147
public function getType() : ?Type
81148
{
82149
return $this->values['type']??null;

0 commit comments

Comments
 (0)