From 18c935cbfb3616e6a2415ca38b5b6caef0fe57b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiemo=20M=C3=A4ttig?= Date: Thu, 12 Feb 2015 12:11:08 +0100 Subject: [PATCH] Rework ...List constructors --- src/Reference.php | 10 ++++++++-- src/ReferenceList.php | 9 +++++++-- src/Snak/SnakList.php | 22 ++++++++++++++++++++++ tests/unit/HashArray/HashArrayTest.php | 2 +- tests/unit/ReferenceListTest.php | 1 - tests/unit/ReferenceTest.php | 3 +-- tests/unit/Snak/SnakListTest.php | 3 +-- tests/unit/Statement/StatementTest.php | 16 ++++------------ 8 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/Reference.php b/src/Reference.php index 47f2a48a..bd6fd976 100644 --- a/src/Reference.php +++ b/src/Reference.php @@ -29,10 +29,16 @@ class Reference implements Hashable, Comparable, Immutable, Countable { /** * An array of Snak objects is only supported since version 1.1. * - * @param Snak[]|SnakList $snaks + * @param Snak[]|SnakList|Snak $snaks + * @param Snak [$snak2,...] + * * @throws InvalidArgumentException */ - public function __construct( $snaks = array() ) { + public function __construct( $snaks = array() /*...*/ ) { + if ( $snaks instanceof Snak ) { + $snaks = func_get_args(); + } + if ( is_array( $snaks ) ) { $snaks = new SnakList( $snaks ); } diff --git a/src/ReferenceList.php b/src/ReferenceList.php index 15ac67ec..56aa9d1d 100644 --- a/src/ReferenceList.php +++ b/src/ReferenceList.php @@ -26,11 +26,16 @@ class ReferenceList extends HashableObjectStorage { /** - * @param Reference[]|Traversable $references + * @param Reference[]|Traversable|Reference $references + * @param Reference [$reference2,...] * * @throws InvalidArgumentException */ - public function __construct( $references = array() ) { + public function __construct( $references = array() /*...*/ ) { + if ( $references instanceof Reference ) { + $references = func_get_args(); + } + if ( !is_array( $references ) && !( $references instanceof Traversable ) ) { throw new InvalidArgumentException( '$references must be an array or an instance of Traversable' ); } diff --git a/src/Snak/SnakList.php b/src/Snak/SnakList.php index 5cc03a06..30e08330 100644 --- a/src/Snak/SnakList.php +++ b/src/Snak/SnakList.php @@ -2,6 +2,8 @@ namespace Wikibase\DataModel\Snak; +use InvalidArgumentException; +use Traversable; use Wikibase\DataModel\HashArray; use Wikibase\DataModel\Internal\MapValueHasher; @@ -17,6 +19,26 @@ */ class SnakList extends HashArray { + /** + * @param Snak[]|Traversable|Snak $snaks + * @param Snak [$snak2,...] + * + * @throws InvalidArgumentException + */ + public function __construct( $snaks = array() /*...*/ ) { + if ( $snaks instanceof Snak ) { + $snaks = func_get_args(); + } + + if ( !is_array( $snaks ) && !( $snaks instanceof Traversable ) ) { + throw new InvalidArgumentException( '$snaks must be an array or an instance of Traversable' ); + } + + foreach ( $snaks as $index => $snak ) { + $this->setElement( $index, $snak ); + } + } + /** * @see GenericArrayObject::getObjectType * diff --git a/tests/unit/HashArray/HashArrayTest.php b/tests/unit/HashArray/HashArrayTest.php index 4ff744f1..e8a9cae0 100644 --- a/tests/unit/HashArray/HashArrayTest.php +++ b/tests/unit/HashArray/HashArrayTest.php @@ -35,7 +35,7 @@ public function instanceProvider() { $instances = array(); foreach ( $this->constructorProvider() as $args ) { - $instances[] = array( new $class( array_key_exists( 0, $args ) ? $args[0] : null ) ); + $instances[] = array( new $class( array_key_exists( 0, $args ) ? $args[0] : array() ) ); } return $instances; diff --git a/tests/unit/ReferenceListTest.php b/tests/unit/ReferenceListTest.php index 021d8b7f..24be85ee 100644 --- a/tests/unit/ReferenceListTest.php +++ b/tests/unit/ReferenceListTest.php @@ -76,7 +76,6 @@ public function invalidConstructorArgumentsProvider() { array( 'string' ), array( $id1 ), array( new PropertyNoValueSnak( $id1 ) ), - array( new Reference() ), array( new SnakList( array( new PropertyNoValueSnak( $id1 ) ) ) ), array( array( new PropertyNoValueSnak( $id1 ) ) ), array( array( new ReferenceList() ) ), diff --git a/tests/unit/ReferenceTest.php b/tests/unit/ReferenceTest.php index 14eca132..fb6f72ba 100644 --- a/tests/unit/ReferenceTest.php +++ b/tests/unit/ReferenceTest.php @@ -262,8 +262,7 @@ public function invalidConstructorArgumentsProvider() { array( 0.1 ), array( 'string' ), array( $id1 ), - array( new PropertyNoValueSnak( $id1 ) ), - array( new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ), + array( array( new Reference() ) ), array( array( new SnakList() ) ), array( new Reference() ), ); diff --git a/tests/unit/Snak/SnakListTest.php b/tests/unit/Snak/SnakListTest.php index a1492fd6..417cc8df 100644 --- a/tests/unit/Snak/SnakListTest.php +++ b/tests/unit/Snak/SnakListTest.php @@ -84,13 +84,12 @@ public function invalidConstructorArgumentsProvider() { $id1 = new PropertyId( 'P1' ); return array( + array( null ), array( false ), array( 1 ), array( 0.1 ), array( 'string' ), array( $id1 ), - array( new PropertyNoValueSnak( $id1 ) ), - array( new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ), array( array( null ) ), array( array( $id1 ) ), array( array( new SnakList() ) ), diff --git a/tests/unit/Statement/StatementTest.php b/tests/unit/Statement/StatementTest.php index 9339eea7..f882989b 100644 --- a/tests/unit/Statement/StatementTest.php +++ b/tests/unit/Statement/StatementTest.php @@ -296,9 +296,7 @@ public function testGivenSameStatement_equalsReturnsTrue() { new SnakList( array( new PropertyNoValueSnak( 1337 ), ) ), - new ReferenceList( array( - new Reference( array( new PropertyNoValueSnak( 1337 ) ) ), - ) ) + new ReferenceList( new Reference( new PropertyNoValueSnak( 1337 ) ) ) ); $statement->setGuid( 'kittens' ); @@ -348,17 +346,13 @@ public function testStatementClaimWithDifferentReferences_equalsReturnsFalse() { $statement = new Statement( new PropertyNoValueSnak( 42 ), new SnakList(), - new ReferenceList( array( - new Reference( array( new PropertyNoValueSnak( 1337 ) ) ), - ) ) + new ReferenceList( new Reference( new PropertyNoValueSnak( 1337 ) ) ) ); $differentStatement = new Statement( new PropertyNoValueSnak( 42 ), new SnakList(), - new ReferenceList( array( - new Reference( array( new PropertyNoValueSnak( 32202 ) ) ), - ) ) + new ReferenceList( new Reference( new PropertyNoValueSnak( 32202 ) ) ) ); $this->assertFalse( $statement->equals( $differentStatement ) ); @@ -407,9 +401,7 @@ private function newStatement() { $statement = new Statement( new PropertyNoValueSnak( 42 ), $qualifiers, - new ReferenceList( array( - new Reference( array( new PropertyNoValueSnak( 1337 ) ) ), - ) ) + new ReferenceList( new Reference( new PropertyNoValueSnak( 1337 ) ) ) ); $statement->setRank( Statement::RANK_NORMAL );