Skip to content

Commit a25d095

Browse files
committed
Merge pull request #471 from wmde/removestatementswithguid
Add StatementList::removeStatementsWithGuid
2 parents 440a46a + ae12202 commit a25d095

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/Statement/StatementList.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*
2626
* @licence GNU GPL v2+
2727
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
28+
* @author Bene* < benestar.wikimedia@gmail.com >
2829
*/
2930
class StatementList implements IteratorAggregate, Comparable, Countable {
3031

@@ -105,6 +106,17 @@ public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references
105106
$this->addStatement( $statement );
106107
}
107108

109+
/**
110+
* @param string|null $guid
111+
*/
112+
public function removeStatementsWithGuid( $guid ) {
113+
foreach ( $this->statements as $index => $statement ) {
114+
if ( $statement->getGuid() === $guid ) {
115+
unset( $this->statements[$index] );
116+
}
117+
}
118+
}
119+
108120
/**
109121
* Statements that have a main snak already in the list are filtered out.
110122
* The last occurrences are retained.

tests/unit/Statement/StatementListTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,50 @@ public function testAddStatementWithGuid() {
260260
$this->assertEquals( new StatementList( $statement ), $list );
261261
}
262262

263+
public function testRemoveStatementsWithGuid_singleStatementRemoved() {
264+
$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
265+
$statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
266+
$statement3 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
267+
268+
$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
269+
$list->removeStatementsWithGuid( 'foo' );
270+
271+
$statements = array();
272+
$statements[1] = $statement2;
273+
$statements[2] = $statement3;
274+
275+
$this->assertEquals( $statements, $list->toArray() );
276+
}
277+
278+
public function testRemoveStatementsWithGuid_multipleStatementsRemoved() {
279+
$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
280+
$statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
281+
$statement3 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
282+
283+
$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
284+
$list->removeStatementsWithGuid( 'bar' );
285+
286+
$this->assertEquals(
287+
new StatementList( array( $statement1 ) ),
288+
$list
289+
);
290+
}
291+
292+
public function testRemoveStatementsWithGuid_nowStatementRemoved() {
293+
$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
294+
$statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
295+
$statement3 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
296+
297+
$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
298+
$list->removeStatementsWithGuid( 'baz' );
299+
300+
$this->assertEquals(
301+
new StatementList( array( $statement1, $statement2, $statement3 ) ),
302+
$list
303+
);
304+
}
305+
306+
263307
public function testCanConstructWithClaimsObjectContainingOnlyStatements() {
264308
$statementArray = array(
265309
$this->getStatementWithSnak( 1, 'foo' ),

0 commit comments

Comments
 (0)