1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Term; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Countable; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Iterator; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use OutOfBoundsException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Unordered list of Term objects. |
14
|
|
|
* If multiple terms with the same language code are provided, only the last one will be retained. |
15
|
|
|
* Empty terms are skipped and treated as non-existing. |
16
|
|
|
* |
17
|
|
|
* @since 0.7.3 |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0-or-later |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class TermList implements Countable, IteratorAggregate { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Term[] |
26
|
|
|
*/ |
27
|
|
|
private $terms = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param iterable|Term[] $terms Can be a non-array since 8.1 |
31
|
|
|
* @throws InvalidArgumentException |
32
|
|
|
*/ |
33
|
39 |
|
public function __construct( /* iterable */ $terms = [] ) { |
34
|
39 |
|
$this->addAll( $terms ); |
35
|
19 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see Countable::count |
39
|
19 |
|
* @return int |
40
|
39 |
|
*/ |
41
|
39 |
|
public function count() { |
42
|
|
|
return count( $this->terms ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns an array with language codes as keys and the term text as values. |
47
|
7 |
|
* |
48
|
7 |
|
* @return string[] |
49
|
|
|
*/ |
50
|
|
|
public function toTextArray() { |
51
|
|
|
$array = []; |
52
|
|
|
|
53
|
|
|
foreach ( $this->terms as $term ) { |
54
|
|
|
$array[$term->getLanguageCode()] = $term->getText(); |
55
|
|
|
} |
56
|
2 |
|
|
57
|
2 |
|
return $array; |
58
|
|
|
} |
59
|
2 |
|
|
60
|
1 |
|
/** |
61
|
2 |
|
* @see IteratorAggregate::getIterator |
62
|
|
|
* @return Iterator|Term[] |
63
|
2 |
|
*/ |
64
|
|
|
public function getIterator() { |
65
|
|
|
return new ArrayIterator( $this->terms ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $languageCode |
70
|
4 |
|
* |
71
|
4 |
|
* @return Term |
72
|
|
|
* @throws OutOfBoundsException |
73
|
|
|
*/ |
74
|
|
|
public function getByLanguage( $languageCode ) { |
75
|
|
|
if ( !array_key_exists( $languageCode, $this->terms ) ) { |
76
|
|
|
throw new OutOfBoundsException( 'Term with languageCode "' . $languageCode . '" not found' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->terms[$languageCode]; |
80
|
|
|
} |
81
|
6 |
|
|
82
|
6 |
|
/** |
83
|
|
|
* @since 2.5 |
84
|
3 |
|
* |
85
|
1 |
|
* @param string[] $languageCodes |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
2 |
|
*/ |
89
|
|
|
public function getWithLanguages( array $languageCodes ) { |
90
|
|
|
return new self( array_intersect_key( $this->terms, array_flip( $languageCodes ) ) ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $languageCode |
95
|
|
|
*/ |
96
|
|
|
public function removeByLanguage( $languageCode ) { |
97
|
|
|
unset( $this->terms[$languageCode] ); |
98
|
4 |
|
} |
99
|
4 |
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $languageCode |
102
|
5 |
|
* |
103
|
5 |
|
* @return bool |
104
|
2 |
|
*/ |
105
|
2 |
|
public function hasTermForLanguage( $languageCode ) { |
106
|
|
|
return array_key_exists( $languageCode, $this->terms ); |
107
|
4 |
|
} |
108
|
4 |
|
|
109
|
1 |
|
/** |
110
|
|
|
* Replaces non-empty or removes empty terms. |
111
|
|
|
* |
112
|
15 |
|
* @param Term $term |
113
|
15 |
|
*/ |
114
|
9 |
|
public function setTerm( Term $term ) { |
115
|
|
|
if ( $term->getText() === '' ) { |
116
|
6 |
|
unset( $this->terms[$term->getLanguageCode()] ); |
117
|
|
|
} else { |
118
|
6 |
|
$this->terms[$term->getLanguageCode()] = $term; |
119
|
6 |
|
} |
120
|
6 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @since 0.8 |
124
|
|
|
* |
125
|
|
|
* @param string $languageCode |
126
|
|
|
* @param string $termText |
127
|
|
|
*/ |
128
|
6 |
|
public function setTextForLanguage( $languageCode, $termText ) { |
129
|
6 |
|
$this->setTerm( new Term( $languageCode, $termText ) ); |
130
|
4 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* |
134
|
|
|
* @since 0.7.4 |
135
|
|
|
* |
136
|
|
|
* @param mixed $target |
137
|
|
|
* |
138
|
|
|
* @return bool |
139
|
|
|
*/ |
140
|
|
|
public function equals( $target ) { |
141
|
5 |
|
if ( $this === $target ) { |
142
|
5 |
|
return true; |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
if ( !( $target instanceof self ) |
146
|
5 |
|
|| $this->count() !== $target->count() |
147
|
5 |
|
) { |
148
|
5 |
|
return false; |
149
|
2 |
|
} |
150
|
|
|
|
151
|
|
|
foreach ( $this->terms as $term ) { |
152
|
4 |
|
if ( !$target->hasTerm( $term ) ) { |
153
|
3 |
|
return false; |
154
|
1 |
|
} |
155
|
|
|
} |
156
|
4 |
|
|
157
|
|
|
return true; |
158
|
3 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @since 2.4.0 |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function isEmpty() { |
166
|
1 |
|
return empty( $this->terms ); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @since 0.7.4 |
171
|
|
|
* |
172
|
|
|
* @param Term $term |
173
|
|
|
* |
174
|
|
|
* @return boolean |
175
|
|
|
*/ |
176
|
|
|
public function hasTerm( Term $term ) { |
177
|
6 |
|
return array_key_exists( $term->getLanguageCode(), $this->terms ) |
178
|
6 |
|
&& $this->terms[$term->getLanguageCode()]->equals( $term ); |
179
|
6 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Removes all terms from this list. |
183
|
|
|
* |
184
|
|
|
* @since 7.0 |
185
|
|
|
*/ |
186
|
|
|
public function clear() { |
187
|
|
|
$this->terms = []; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @since 8.1 |
192
|
|
|
* |
193
|
|
|
* @param iterable|Term[] $terms |
194
|
|
|
* @throws InvalidArgumentException |
195
|
|
|
*/ |
196
|
|
|
public function addAll( /* iterable */ $terms ) { |
197
|
|
|
if ( !is_array( $terms ) && !( $terms instanceof \Traversable ) ) { |
198
|
|
|
throw new InvalidArgumentException( '$terms must be iterable' ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
foreach ( $terms as $term ) { |
202
|
|
|
if ( !( $term instanceof Term ) ) { |
203
|
|
|
throw new InvalidArgumentException( 'Every element in $terms must be an instance of Term' ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$this->setTerm( $term ); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|