Passed
Push — master ( c7b7b3...d0017a )
by Daniel
07:28
created

TermList::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Wikibase\DataModel\Term;
4
5
use ArrayIterator;
6
use Comparable;
7
use Countable;
8
use InvalidArgumentException;
9
use IteratorAggregate;
10
use OutOfBoundsException;
11
use Traversable;
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 39
	 */
34 39
	public function __construct( array $terms = [] ) {
35 19
		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 19
40 39
			$this->setTerm( $term );
41 39
		}
42
	}
43
44
	/**
45
	 * @see Countable::count
46
	 * @return int
47 7
	 */
48 7
	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 2
	 */
57 2
	public function toTextArray() {
58
		$array = [];
59 2
60 1
		foreach ( $this->terms as $term ) {
61 2
			$array[$term->getLanguageCode()] = $term->getText();
62
		}
63 2
64
		return $array;
65
	}
66
67
	/**
68
	 * @see IteratorAggregate::getIterator
69
	 * @return Traversable|Term[]
70 4
	 */
71 4
	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 6
	public function getByLanguage( $languageCode ) {
82 6
		if ( !array_key_exists( $languageCode, $this->terms ) ) {
83
			throw new OutOfBoundsException( 'Term with languageCode "' . $languageCode . '" not found' );
84 3
		}
85 1
86
		return $this->terms[$languageCode];
87
	}
88 2
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 4
	}
99 4
100
	/**
101
	 * @param string $languageCode
102 5
	 */
103 5
	public function removeByLanguage( $languageCode ) {
104 2
		unset( $this->terms[$languageCode] );
105 2
	}
106
107 4
	/**
108 4
	 * @param string $languageCode
109 1
	 *
110
	 * @return bool
111
	 */
112 15
	public function hasTermForLanguage( $languageCode ) {
113 15
		return array_key_exists( $languageCode, $this->terms );
114 9
	}
115
116 6
	/**
117
	 * Replaces non-empty or removes empty terms.
118 6
	 *
119 6
	 * @param Term $term
120 6
	 */
121
	public function setTerm( Term $term ) {
122
		if ( $term->getText() === '' ) {
123
			unset( $this->terms[$term->getLanguageCode()] );
124
		}
125
		else {
126
			$this->terms[$term->getLanguageCode()] = $term;
127
		}
128 6
	}
129 6
130 4
	/**
131
	 * @since 0.8
132
	 *
133
	 * @param string $languageCode
134
	 * @param string $termText
135
	 */
136
	public function setTextForLanguage( $languageCode, $termText ) {
137
		$this->setTerm( new Term( $languageCode, $termText ) );
138
	}
139
140
	/**
141 5
	 * @see Comparable::equals
142 5
	 *
143 1
	 * @since 0.7.4
144
	 *
145
	 * @param mixed $target
146 5
	 *
147 5
	 * @return bool
148 5
	 */
149 2
	public function equals( $target ) {
150
		if ( $this === $target ) {
151
			return true;
152 4
		}
153 3
154 1
		if ( !( $target instanceof self )
155
			|| $this->count() !== $target->count()
156 4
		) {
157
			return false;
158 3
		}
159
160
		foreach ( $this->terms as $term ) {
161
			if ( !$target->hasTerm( $term ) ) {
162
				return false;
163
			}
164
		}
165
166 1
		return true;
167 1
	}
168
169
	/**
170
	 * @since 2.4.0
171
	 *
172
	 * @return bool
173
	 */
174
	public function isEmpty() {
175
		return empty( $this->terms );
176
	}
177 6
178 6
	/**
179 6
	 * @since 0.7.4
180
	 *
181
	 * @param Term $term
182
	 *
183
	 * @return boolean
184
	 */
185
	public function hasTerm( Term $term ) {
186
		return array_key_exists( $term->getLanguageCode(), $this->terms )
187
			&& $this->terms[$term->getLanguageCode()]->equals( $term );
188
	}
189
190
	/**
191
	 * Removes all terms from this list.
192
	 *
193
	 * @since 6.0
194
	 */
195
	public function clear() {
196
		$this->terms = [];
197
	}
198
199
}
200