Skip to content

Commit 73e2db3

Browse files
jakobwaddshore
authored andcommitted
Add clear method to EntityDocument (#776)
* Add Clearable interface for entities * Fix whitespace * Rename to ClearableEntity * Make clear part of EntityDocument. * Improve ItemTest::testClear
1 parent c8b4d1c commit 73e2db3

5 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/Entity/EntityDocument.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,11 @@ public function equals( $target );
8888
*/
8989
public function copy();
9090

91+
/**
92+
* Clears all fields of the entity that can be emptied. The entity's id stays the same.
93+
*
94+
* @since 7.4
95+
*/
96+
public function clear();
97+
9198
}

src/Entity/Item.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,13 @@ public function __clone() {
342342
$this->statements = clone $this->statements;
343343
}
344344

345+
/**
346+
* @since 7.4
347+
*/
348+
public function clear() {
349+
$this->fingerprint = new Fingerprint();
350+
$this->siteLinks = new SiteLinkList();
351+
$this->statements = new StatementList();
352+
}
353+
345354
}

src/Entity/Property.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,4 +297,12 @@ public function __clone() {
297297
$this->statements = clone $this->statements;
298298
}
299299

300+
/**
301+
* @since 7.4
302+
*/
303+
public function clear() {
304+
$this->fingerprint = new Fingerprint();
305+
$this->statements = new StatementList();
306+
}
307+
300308
}

tests/unit/Entity/ItemTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Wikibase\DataModel\Entity\Item;
88
use Wikibase\DataModel\Entity\ItemId;
99
use Wikibase\DataModel\SiteLink;
10+
use Wikibase\DataModel\SiteLinkList;
1011
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
1112
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
1213
use Wikibase\DataModel\Statement\Statement;
@@ -742,4 +743,43 @@ public function testGetAliasGroups_sameListAsFingerprint() {
742743
);
743744
}
744745

746+
/**
747+
* @dataProvider clearableProvider
748+
*/
749+
public function testClear( Item $item ) {
750+
$clone = $item->copy();
751+
752+
$item->clear();
753+
754+
$this->assertEquals( $clone->getId(), $item->getId(), 'cleared Item should keep its id' );
755+
$this->assertTrue( $item->isEmpty(), 'cleared Item should be empty' );
756+
}
757+
758+
public function clearableProvider() {
759+
return [
760+
'empty' => [ new Item( new ItemId( 'Q23' ) ), ],
761+
'with fingerprint' => [
762+
new Item(
763+
new ItemId( 'Q42' ),
764+
new Fingerprint( new TermList( [ new Term( 'en', 'foo' ) ] ) )
765+
),
766+
],
767+
'with sitelink' => [
768+
new Item(
769+
new ItemId( 'Q123' ),
770+
null,
771+
new SiteLinkList( [ new SiteLink( 'enwiki', 'Wikidata' ) ] )
772+
)
773+
],
774+
'with statement' => [
775+
new Item(
776+
new ItemId( 'Q321' ),
777+
null,
778+
null,
779+
new StatementList( [ $this->newStatement() ] )
780+
)
781+
]
782+
];
783+
}
784+
745785
}

tests/unit/Entity/PropertyTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,4 +619,48 @@ public function testGetAliasGroups_sameListAsFingerprint() {
619619
);
620620
}
621621

622+
/**
623+
* @dataProvider clearableProvider
624+
*/
625+
public function testClear( Property $property ) {
626+
$clone = $property->copy();
627+
628+
$property->clear();
629+
630+
$this->assertEquals(
631+
$clone->getId(),
632+
$property->getId(),
633+
'cleared Property should keep its id'
634+
);
635+
$this->assertEquals(
636+
$clone->getDataTypeId(),
637+
$property->getDataTypeId(),
638+
'cleared Property should keep its data type'
639+
);
640+
$this->assertTrue( $property->isEmpty(), 'cleared Property should be empty' );
641+
}
642+
643+
public function clearableProvider() {
644+
return [
645+
'empty' => [
646+
new Property( new PropertyId( 'P123' ), null, 'string' ),
647+
],
648+
'with fingerprint' => [
649+
new Property(
650+
new PropertyId( 'P321' ),
651+
new Fingerprint( new TermList( [ new Term( 'en', 'foo' ) ] ) ),
652+
'time'
653+
),
654+
],
655+
'with statement' => [
656+
new Property(
657+
new PropertyId( 'P234' ),
658+
null,
659+
'wikibase-entityid',
660+
$this->newNonEmptyStatementList()
661+
)
662+
],
663+
];
664+
}
665+
622666
}

0 commit comments

Comments
 (0)