4
4
namespace ArrayAccess \RdapClient \Response \Data ;
5
5
6
6
use ArrayAccess \RdapClient \Response \Data \Abstracts \AbstractRdapResponseDataRecursiveArray ;
7
+ use IteratorAggregate ;
7
8
use function array_filter ;
8
- use function in_array ;
9
9
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
11
14
{
15
+ /**
16
+ * @var string $name The name of the object
17
+ */
12
18
protected string $ name = 'link ' ;
13
19
20
+ /**
21
+ * @var array<"value"|"rel"|"href"|"hreflang"|"title"|"media"|"type">
22
+ */
14
23
protected array $ allowedKeys = [
15
24
'value ' ,
16
25
'rel ' ,
@@ -21,62 +30,120 @@ class Link extends AbstractRdapResponseDataRecursiveArray
21
30
'type ' ,
22
31
];
23
32
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
+ */
24
49
public function __construct (Value |Rel |Href |HrefLang |Title |Media |Type ...$ args )
25
50
{
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 = [];
35
52
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 ;
38
75
continue ;
39
76
}
40
- $ this ->values [$ name ] = $ arg ;
77
+ $ this ->values [' value ' ] = $ arg ;
41
78
}
42
- $ this ->values = array_filter ($ this ->values );
43
79
}
44
80
81
+ /**
82
+ * @return array<string, Value|Rel|Href|HrefLang|Title|Media|Type>
83
+ */
45
84
public function getValues (): array
46
85
{
47
86
return array_filter ($ this ->values );
48
87
}
49
88
89
+ /**
90
+ * Get the value
91
+ * @return Value|null
92
+ */
50
93
public function getValue () : ?Value
51
94
{
52
95
return $ this ->values ['value ' ]??null ;
53
96
}
54
97
98
+ /**
99
+ * Get the rel
100
+ * @return Rel|null
101
+ */
55
102
public function getRel () : ?Rel
56
103
{
57
104
return $ this ->values ['rel ' ]??null ;
58
105
}
59
106
107
+ /**
108
+ * Get the href
109
+ * @return Href|null
110
+ */
60
111
public function getHref () : ?Href
61
112
{
62
113
return $ this ->values ['href ' ]??null ;
63
114
}
64
115
116
+ /**
117
+ * Get the hreflang
118
+ * @return HrefLang|null
119
+ */
65
120
public function getHrefLang () : ?HrefLang
66
121
{
67
122
return $ this ->values ['hreflang ' ]??null ;
68
123
}
69
124
125
+ /**
126
+ * Get the title
127
+ * @return Title|null
128
+ */
70
129
public function getTitle () : ?Title
71
130
{
72
131
return $ this ->values ['title ' ]??null ;
73
132
}
74
133
134
+ /**
135
+ * Get the media
136
+ * @return Media|null
137
+ */
75
138
public function getMedia () : ?Media
76
139
{
77
140
return $ this ->values ['media ' ]??null ;
78
141
}
79
142
143
+ /**
144
+ * Get the type
145
+ * @return Type|null
146
+ */
80
147
public function getType () : ?Type
81
148
{
82
149
return $ this ->values ['type ' ]??null ;
0 commit comments