jenkins-bot has submitted this change and it was merged. Change subject: Snaklists can now sort themselves given an order ......................................................................
Snaklists can now sort themselves given an order Change-Id: I523536b8bf6e09ac8b5af4d09bb00dede790675f --- M DataModel/Snak/SnakList.php M tests/phpunit/Snak/SnakListTest.php 2 files changed, 80 insertions(+), 22 deletions(-) Approvals: Daniel Kinzler: Looks good to me, approved jenkins-bot: Verified diff --git a/DataModel/Snak/SnakList.php b/DataModel/Snak/SnakList.php index 66d2619..31fbe11 100644 --- a/DataModel/Snak/SnakList.php +++ b/DataModel/Snak/SnakList.php @@ -13,6 +13,7 @@ * * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > + * @author Adam Shorland */ class SnakList extends HashArray implements Snaks { @@ -121,27 +122,49 @@ /** * Orders the snaks in the list grouping them by property. + * @param $order array of serliazed propertyIds to order by. * * @since 0.5 */ - public function orderByProperty() { + public function orderByProperty( $order = array() ) { + $snaksByProperty = $this->getSnaksByProperty(); + $orderedProperties = array_unique( array_merge( $order, array_keys( $snaksByProperty ) ) ); + + foreach( $orderedProperties as $property ){ + $snaks = $snaksByProperty[ $property ]; + $this->moveSnaksToBottom( $snaks ); + } + } + + /** + * @param array $snaks to remove and re add + */ + private function moveSnaksToBottom( $snaks ) { + foreach( $snaks as $snak ) { + $this->removeSnak( $snak ); + $this->addSnak( $snak ); + } + } + + /** + * Gets the snaks in the current object in an array + * grouped by property id + * + * @return array + */ + private function getSnaksByProperty() { $snaksByProperty = array(); foreach( $this as $snak ) { + /** @var Snak $snak */ $propertyId = $snak->getPropertyId()->getSerialization(); - if( !isset( $snaksByProperty[$propertyId] ) ) { $snaksByProperty[$propertyId] = array(); } $snaksByProperty[$propertyId][] = $snak; } - foreach( $snaksByProperty as $snaks ) { - foreach( $snaks as $snak ) { - $this->removeSnak( $snak ); - $this->addSnak( $snak ); - } - } + return $snaksByProperty; } /** diff --git a/tests/phpunit/Snak/SnakListTest.php b/tests/phpunit/Snak/SnakListTest.php index dfc82fe..d082023 100644 --- a/tests/phpunit/Snak/SnakListTest.php +++ b/tests/phpunit/Snak/SnakListTest.php @@ -12,12 +12,8 @@ /** * @covers Wikibase\SnakList - * - * @file + * @since 0.1 - * - * @ingroup WikibaseLib - * @ingroup Test * * @group Wikibase * @group WikibaseDataModel @@ -25,6 +21,7 @@ * * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > + * @author Adam Shorland */ class SnakListTest extends HashArrayTest { @@ -75,7 +72,7 @@ /** * @dataProvider instanceProvider * - * @param \Wikibase\SnakList $array + * @param SnakList $array */ public function testHasSnak( SnakList $array ) { /** @@ -95,7 +92,7 @@ /** * @dataProvider instanceProvider * - * @param \Wikibase\SnakList $array + * @param SnakList $array */ public function testRemoveSnak( SnakList $array ) { $elementCount = $array->count(); @@ -128,7 +125,7 @@ /** * @dataProvider instanceProvider * - * @param \Wikibase\SnakList $array + * @param SnakList $array */ public function testAddSnak( SnakList $array ) { $elementCount = $array->count(); @@ -175,6 +172,7 @@ $id1 = new EntityId( Property::ENTITY_TYPE, 1 ); $id2 = new EntityId( Property::ENTITY_TYPE, 2 ); + $id3 = new EntityId( Property::ENTITY_TYPE, 3 ); /** * List of test data containing snaks to initialize SnakList objects. The first list of @@ -183,6 +181,7 @@ * @var array */ $rawArguments = array( + //default order array( array(), array() ), array( array( new PropertyNoValueSnak( $id1 ) ), @@ -208,14 +207,49 @@ new PropertyNoValueSnak( $id1 ), new PropertyValueSnak( $id1, new StringValue( 'a' ) ), new PropertyNoValueSnak( $id2 ), - ) + ), + ), + //with additional order + array( + array( + new PropertyNoValueSnak( $id3 ), + new PropertyNoValueSnak( $id2 ), + new PropertyValueSnak( $id1, new StringValue( 'a' ) ), + ), + array( + new PropertyNoValueSnak( $id2 ), + new PropertyNoValueSnak( $id3 ), + new PropertyValueSnak( $id1, new StringValue( 'a' ) ), + ), + array( $id2->getSerialization() ) + ), + array( + array( + new PropertyNoValueSnak( $id3 ), + new PropertyNoValueSnak( $id2 ), + new PropertyNoValueSnak( $id2 ), + new PropertyValueSnak( $id1, new StringValue( 'a' ) ), + new PropertyNoValueSnak( $id1 ), + ), + array( + + new PropertyValueSnak( $id1, new StringValue( 'a' ) ), + new PropertyNoValueSnak( $id1 ), + new PropertyNoValueSnak( $id3 ), + new PropertyNoValueSnak( $id2 ), + new PropertyNoValueSnak( $id2 ), + ), + array( $id1->getSerialization() ) ), ); $arguments = array(); - foreach( $rawArguments as $rawSnakLists ) { - $arguments[] = array( new $class( $rawSnakLists[0] ), new $class( $rawSnakLists[1] ) ); + foreach( $rawArguments as $rawArgument ) { + if( !array_key_exists( 2, $rawArgument ) ){ + $rawArgument[2] = array(); + } + $arguments[] = array( new $class( $rawArgument[0] ), new $class( $rawArgument[1] ), $rawArgument[2] ); } return $arguments; @@ -226,17 +260,18 @@ * * @param SnakList $snakList * @param SnakList $expected + * @param array $order */ - public function testOrderByProperty( SnakList $snakList, SnakList $expected ) { + public function testOrderByProperty( SnakList $snakList, SnakList $expected, $order = array() ) { $initialSnakList = SnakList::newFromArray( $snakList->toArray() ); - $snakList->orderByProperty(); + $snakList->orderByProperty( $order ); // Instantiate new SnakList resetting the snaks' array keys. This allows comparing the // reordered SnakList to the expected SnakList. $orderedSnakList = SnakList::newFromArray( $snakList->toArray() ); - $this->assertEquals( $orderedSnakList, $expected ); + $this->assertEquals( $expected, $orderedSnakList ); if( $orderedSnakList->equals( $initialSnakList ) ) { $this->assertTrue( $initialSnakList->getHash() === $snakList->getHash() ); -- To view, visit https://gerrit.wikimedia.org/r/93056 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I523536b8bf6e09ac8b5af4d09bb00dede790675f Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/WikibaseDataModel Gerrit-Branch: master Gerrit-Owner: Addshore <[email protected]> Gerrit-Reviewer: Addshore <[email protected]> Gerrit-Reviewer: Daniel Kinzler <[email protected]> Gerrit-Reviewer: Henning Snater <[email protected]> Gerrit-Reviewer: Jeroen De Dauw <[email protected]> Gerrit-Reviewer: Tobias Gritschacher <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
