Skip to content

Commit 2a3984a

Browse files
authored
Merge pull request #439 from wmde/snakListConstructor
Add SnakList constructor
2 parents d0017a8 + 2970269 commit 2a3984a

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/Snak/SnakList.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Wikibase\DataModel\Snak;
44

5+
use InvalidArgumentException;
6+
use Traversable;
57
use Wikibase\DataModel\HashArray;
68
use Wikibase\DataModel\Internal\MapValueHasher;
79

@@ -17,6 +19,21 @@
1719
*/
1820
class SnakList extends HashArray {
1921

22+
/**
23+
* @param Snak[]|Traversable $snaks
24+
*
25+
* @throws InvalidArgumentException
26+
*/
27+
public function __construct( $snaks = [] ) {
28+
if ( !is_array( $snaks ) && !( $snaks instanceof Traversable ) ) {
29+
throw new InvalidArgumentException( '$snaks must be an array or an instance of Traversable' );
30+
}
31+
32+
foreach ( $snaks as $index => $snak ) {
33+
$this->setElement( $index, $snak );
34+
}
35+
}
36+
2037
/**
2138
* @see GenericArrayObject::getObjectType
2239
*

tests/unit/HashArray/HashArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function instanceProvider() {
3333
$instances = [];
3434

3535
foreach ( $this->constructorProvider() as $args ) {
36-
$instances[] = [ new $class( array_key_exists( 0, $args ) ? $args[0] : null ) ];
36+
$instances[] = [ new $class( array_key_exists( 0, $args ) ? $args[0] : [] ) ];
3737
}
3838

3939
return $instances;

tests/unit/Snak/SnakListTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function invalidConstructorArgumentsProvider() {
8484
$id1 = new PropertyId( 'P1' );
8585

8686
return [
87+
[ null ],
8788
[ false ],
8889
[ 1 ],
8990
[ 0.1 ],

0 commit comments

Comments
 (0)