Skip to content

Commit 3925014

Browse files
committed
Take iterable in TermList constructor
1 parent 6d7b08c commit 3925014

4 files changed

Lines changed: 24 additions & 10 deletions

File tree

RELEASE-NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Wikibase DataModel release notes
22

3+
## Version 8.1.0 (dev)
4+
5+
* The `TermList` constructor now take any `iterable` instead of just `array`
6+
* Added `TermList::addAll`
7+
38
## Version 8.0.0 (2018-08-03)
49

510
#### Breaking changes

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"extra": {
5151
"branch-alias": {
52-
"dev-master": "8.0.x-dev"
52+
"dev-master": "8.1.x-dev"
5353
}
5454
},
5555
"scripts": {

src/Term/TermList.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,11 @@ class TermList implements Countable, IteratorAggregate, Comparable {
2828
private $terms = [];
2929

3030
/**
31-
* @param Term[] $terms
31+
* @param iterable|Term[] $terms Can be a non-array since 8.1
3232
* @throws InvalidArgumentException
3333
*/
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-
}
34+
public function __construct( /* iterable */ $terms = [] ) {
35+
$this->addAll( $terms );
4236
}
4337

4438
/**
@@ -199,9 +193,14 @@ public function clear() {
199193
* @since 8.1
200194
*
201195
* @param iterable|Term[] $terms
196+
* @throws InvalidArgumentException
202197
*/
203198
public function addAll( /* iterable */ $terms ) {
204199
foreach ( $terms as $term ) {
200+
if ( !( $term instanceof Term ) ) {
201+
throw new InvalidArgumentException( 'Every element in $terms must be an instance of Term' );
202+
}
203+
205204
$this->setTerm( $term );
206205
}
207206
}

tests/unit/Term/TermListTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,14 @@ public function testWhenAddingEmptyTerms_theyRemoveExistingOnes() {
457457
$this->assertEquals( new TermList(), $terms );
458458
}
459459

460+
public function testCanConstructWithIterables() {
461+
$enTerm = new Term( 'en', 'foo' );
462+
$deTerm = new Term( 'de', 'bar' );
463+
464+
$terms = new TermList( new TermList( [ $enTerm, $deTerm ] ) );
465+
466+
$this->assertEquals( $enTerm, $terms->getByLanguage( 'en' ) );
467+
$this->assertEquals( $deTerm, $terms->getByLanguage( 'de' ) );
468+
}
469+
460470
}

0 commit comments

Comments
 (0)