Addshore has uploaded a new change for review.
https://gerrit.wikimedia.org/r/93056
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, 91 insertions(+), 17 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseDataModel
refs/changes/56/93056/1
diff --git a/DataModel/Snak/SnakList.php b/DataModel/Snak/SnakList.php
index 66d2619..780762d 100644
--- a/DataModel/Snak/SnakList.php
+++ b/DataModel/Snak/SnakList.php
@@ -121,22 +121,62 @@
/**
* 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() ) {
+ if( count( $order ) === 0 ){
+ $this->orderByPropertyGrouped();
+ } else {
+ $this->orderByPropertyOrdered( $order );
+ }
+ }
+
+ /**
+ * 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 ) {
+ return $snaksByProperty;
+ }
+
+ /**
+ * Orders the snaks in the list grouping them by property using the
given order
+ * @param array $order array of serliazed propertyIds to order by.
+ */
+ private function orderBypropertyOrdered( array $order ){
+ $snaksByProperty = $this->getSnaksByProperty();
+ $orderedProperties = array_unique( array_merge( $order,
array_keys( $snaksByProperty ) ) );
+
+ foreach( $orderedProperties as $property ){
+ $snaks = $snaksByProperty[ $property ];
+ foreach( $snaks as $snak ) {
+ $this->removeSnak( $snak );
+ $this->addSnak( $snak );
+ }
+ }
+ }
+
+ /**
+ * Orders the snaks in the list grouping them by property.
+ */
+ private function orderBypropertyGrouped(){
+ $snaksByProperty = $this->getSnaksByProperty();
+ foreach( $snaksByProperty as $snaks ){
foreach( $snaks as $snak ) {
$this->removeSnak( $snak );
$this->addSnak( $snak );
diff --git a/tests/phpunit/Snak/SnakListTest.php
b/tests/phpunit/Snak/SnakListTest.php
index dfc82fe..5b6e8a0 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
@@ -75,7 +71,7 @@
/**
* @dataProvider instanceProvider
*
- * @param \Wikibase\SnakList $array
+ * @param SnakList $array
*/
public function testHasSnak( SnakList $array ) {
/**
@@ -95,7 +91,7 @@
/**
* @dataProvider instanceProvider
*
- * @param \Wikibase\SnakList $array
+ * @param SnakList $array
*/
public function testRemoveSnak( SnakList $array ) {
$elementCount = $array->count();
@@ -128,7 +124,7 @@
/**
* @dataProvider instanceProvider
*
- * @param \Wikibase\SnakList $array
+ * @param SnakList $array
*/
public function testAddSnak( SnakList $array ) {
$elementCount = $array->count();
@@ -175,6 +171,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 +180,7 @@
* @var array
*/
$rawArguments = array(
+ //default order
array( array(), array() ),
array(
array( new PropertyNoValueSnak( $id1 ) ),
@@ -208,14 +206,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 +259,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: newchange
Gerrit-Change-Id: I523536b8bf6e09ac8b5af4d09bb00dede790675f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseDataModel
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits