1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Countable; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Iterator; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use OutOfBoundsException; |
11
|
|
|
use Traversable; |
12
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
13
|
|
|
use Wikibase\DataModel\Entity\ItemIdSet; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Unordered collection of SiteLink objects. |
17
|
|
|
* SiteLink objects can be accessed by site id. |
18
|
|
|
* Only one SiteLink per site id can exist in the collection. |
19
|
|
|
* |
20
|
|
|
* @since 0.7 |
21
|
|
|
* |
22
|
|
|
* @license GPL-2.0-or-later |
23
|
|
|
* @author Jeroen De Dauw < [email protected] > |
24
|
|
|
*/ |
25
|
|
|
class SiteLinkList implements IteratorAggregate, Countable { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var SiteLink[] |
29
|
|
|
*/ |
30
|
|
|
private $siteLinks = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param iterable|SiteLink[] $siteLinks Can be a non-array iterable since 8.1 |
34
|
|
|
* |
35
|
|
|
* @throws InvalidArgumentException |
36
|
|
|
*/ |
37
|
30 |
|
public function __construct( /* iterable */ $siteLinks = [] ) { |
38
|
30 |
|
if ( !is_array( $siteLinks ) && !( $siteLinks instanceof Traversable ) ) { |
39
|
19 |
|
throw new InvalidArgumentException( '$siteLinks must be iterable' ); |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
foreach ( $siteLinks as $siteLink ) { |
43
|
17 |
|
if ( !( $siteLink instanceof SiteLink ) ) { |
44
|
28 |
|
throw new InvalidArgumentException( 'Every element of $siteLinks must be an instance of SiteLink' ); |
45
|
25 |
|
} |
46
|
|
|
|
47
|
|
|
$this->addSiteLink( $siteLink ); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @since 0.8 |
53
|
|
|
* |
54
|
22 |
|
* @param SiteLink $link |
55
|
22 |
|
* |
56
|
2 |
|
* @throws InvalidArgumentException |
57
|
|
|
*/ |
58
|
|
|
public function addSiteLink( SiteLink $link ) { |
59
|
22 |
|
if ( array_key_exists( $link->getSiteId(), $this->siteLinks ) ) { |
60
|
22 |
|
throw new InvalidArgumentException( 'Duplicate site id: ' . $link->getSiteId() ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->siteLinks[$link->getSiteId()] = $link; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @see SiteLink::__construct |
68
|
|
|
* |
69
|
|
|
* @since 0.8 |
70
|
|
|
* |
71
|
|
|
* @param string $siteId |
72
|
|
|
* @param string $pageName |
73
|
6 |
|
* @param ItemIdSet|ItemId[]|null $badges |
74
|
6 |
|
* |
75
|
6 |
|
* @throws InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
public function addNewSiteLink( $siteId, $pageName, $badges = null ) { |
78
|
|
|
$this->addSiteLink( new SiteLink( $siteId, $pageName, $badges ) ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
4 |
|
* @since 2.5 |
83
|
4 |
|
* |
84
|
4 |
|
* @param SiteLink $link |
85
|
|
|
*/ |
86
|
|
|
public function setSiteLink( SiteLink $link ) { |
87
|
|
|
$this->siteLinks[$link->getSiteId()] = $link; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @since 2.5 |
92
|
|
|
* |
93
|
2 |
|
* @param string $siteId |
94
|
2 |
|
* @param string $pageName |
95
|
2 |
|
* @param ItemIdSet|ItemId[]|null $badges |
96
|
|
|
*/ |
97
|
|
|
public function setNewSiteLink( $siteId, $pageName, $badges = null ) { |
98
|
|
|
$this->setSiteLink( new SiteLink( $siteId, $pageName, $badges ) ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see IteratorAggregate::getIterator |
103
|
|
|
* |
104
|
4 |
|
* Returns an Iterator of SiteLink in which the keys are the site ids. |
105
|
4 |
|
* |
106
|
|
|
* @return Iterator|SiteLink[] |
107
|
|
|
*/ |
108
|
|
|
public function getIterator() { |
109
|
|
|
return new ArrayIterator( $this->siteLinks ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
1 |
|
* @see Countable::count |
114
|
1 |
|
* |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function count() { |
118
|
|
|
return count( $this->siteLinks ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $siteId |
123
|
|
|
* |
124
|
3 |
|
* @return SiteLink |
125
|
3 |
|
* @throws OutOfBoundsException |
126
|
1 |
|
* @throws InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
public function getBySiteId( $siteId ) { |
129
|
1 |
|
if ( !$this->hasLinkWithSiteId( $siteId ) ) { |
130
|
|
|
throw new OutOfBoundsException( 'SiteLink with siteId "' . $siteId . '" not found' ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->siteLinks[$siteId]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @since 0.8 |
138
|
|
|
* |
139
|
|
|
* @param string $siteId |
140
|
4 |
|
* |
141
|
4 |
|
* @return boolean |
142
|
1 |
|
* @throws InvalidArgumentException |
143
|
|
|
*/ |
144
|
|
|
public function hasLinkWithSiteId( $siteId ) { |
145
|
3 |
|
if ( !is_string( $siteId ) ) { |
146
|
|
|
throw new InvalidArgumentException( '$siteId must be a string; got ' . gettype( $siteId ) ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return array_key_exists( $siteId, $this->siteLinks ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* @since 0.7.4 |
155
|
|
|
* |
156
|
|
|
* @param mixed $target |
157
|
10 |
|
* |
158
|
10 |
|
* @return bool |
159
|
3 |
|
*/ |
160
|
|
|
public function equals( $target ) { |
161
|
|
|
if ( $this === $target ) { |
162
|
|
|
return true; |
163
|
10 |
|
} |
164
|
|
|
|
165
|
|
|
return $target instanceof self |
166
|
|
|
&& $this->siteLinks == $target->siteLinks; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @since 1.0 |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function isEmpty() { |
175
|
|
|
return empty( $this->siteLinks ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @since 2.5 |
180
|
1 |
|
* |
181
|
1 |
|
* @return SiteLink[] Array indexed by site id. |
182
|
|
|
*/ |
183
|
|
|
public function toArray() { |
184
|
|
|
return $this->siteLinks; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @since 0.8 |
189
|
|
|
* |
190
|
|
|
* @param string $siteId |
191
|
3 |
|
* |
192
|
3 |
|
* @throws InvalidArgumentException |
193
|
1 |
|
*/ |
194
|
|
|
public function removeLinkWithSiteId( $siteId ) { |
195
|
|
|
if ( !is_string( $siteId ) ) { |
196
|
2 |
|
throw new InvalidArgumentException( '$siteId must be a string; got ' . gettype( $siteId ) ); |
197
|
2 |
|
} |
198
|
|
|
|
199
|
|
|
unset( $this->siteLinks[$siteId] ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} |
203
|
|
|
|