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