Skip to content

Commit 59ec644

Browse files
committed
Merge pull request #632 from wmde/item-property-terms-provider
Add interfaces to Item and Property
2 parents fbb909b + 2458729 commit 59ec644

5 files changed

Lines changed: 220 additions & 4 deletions

File tree

RELEASE-NOTES.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
* Removed `Entity` class (deprecated since 1.0)
66
* `Item` and `Property` no longer extend `Entity`
7-
* Removed `getLabel`, `getLabels`, `getDescription`, `getDescriptions`, `getAliases`,
8-
`getAllAliases`, `setLabels`, `setDescriptions`, `addAliases`, `setAllAliases`,
7+
* Removed `getLabel`, `getDescription`, `getAliases`, `getAllAliases`,
8+
`setLabels`, `setDescriptions`, `addAliases`, `setAllAliases`,
99
`removeLabel`, `removeDescription` and `removeAliases` methods
10+
* `Item` and `Property` now implement `LabelsProvider`, `DescriptionsProvider` and `AliasesProvider`
11+
* `Item::getLabels` and `Property::getLabels` now return a `TermList`
12+
* `Item::getDescriptions` and `Property::getDescriptions` now return a `TermList`
13+
* Added `Item::getAliasGroups` and `Property::getAliasGroups`
1014
* `TermList` and `AliasGroupList` no longer throw an `InvalidArgumentException` for invalid language codes.
1115
* `getByLangauge` now throws an `OutOfBoundsException`.
1216
* `removeByLanguage` does nothing for invalid values.

src/Entity/Item.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
use Wikibase\DataModel\SiteLinkList;
99
use Wikibase\DataModel\Statement\StatementList;
1010
use Wikibase\DataModel\Statement\StatementListHolder;
11+
use Wikibase\DataModel\Term\AliasesProvider;
12+
use Wikibase\DataModel\Term\AliasGroupList;
13+
use Wikibase\DataModel\Term\DescriptionsProvider;
1114
use Wikibase\DataModel\Term\Fingerprint;
1215
use Wikibase\DataModel\Term\FingerprintHolder;
16+
use Wikibase\DataModel\Term\LabelsProvider;
17+
use Wikibase\DataModel\Term\TermList;
1318

1419
/**
1520
* Represents a single Wikibase item.
@@ -19,8 +24,10 @@
1924
*
2025
* @licence GNU GPL v2+
2126
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
27+
* @author Bene* < benestar.wikimedia@gmail.com >
2228
*/
23-
class Item implements EntityDocument, FingerprintHolder, StatementListHolder {
29+
class Item implements EntityDocument, FingerprintHolder, StatementListHolder,
30+
LabelsProvider, DescriptionsProvider, AliasesProvider {
2431

2532
const ENTITY_TYPE = 'item';
2633

@@ -113,6 +120,39 @@ public function setFingerprint( Fingerprint $fingerprint ) {
113120
$this->fingerprint = $fingerprint;
114121
}
115122

123+
/**
124+
* @see LabelsProvider::getLabels
125+
*
126+
* @since 6.0
127+
*
128+
* @return TermList
129+
*/
130+
public function getLabels() {
131+
return $this->fingerprint->getLabels();
132+
}
133+
134+
/**
135+
* @see DescriptionsProvider::getDescriptions
136+
*
137+
* @since 6.0
138+
*
139+
* @return TermList
140+
*/
141+
public function getDescriptions() {
142+
return $this->fingerprint->getDescriptions();
143+
}
144+
145+
/**
146+
* @see AliasesProvider::getAliasGroups
147+
*
148+
* @since 6.0
149+
*
150+
* @return AliasGroupList
151+
*/
152+
public function getAliasGroups() {
153+
return $this->fingerprint->getAliasGroups();
154+
}
155+
116156
/**
117157
* @param string $languageCode
118158
* @param string $value

src/Entity/Property.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
use InvalidArgumentException;
66
use Wikibase\DataModel\Statement\StatementList;
77
use Wikibase\DataModel\Statement\StatementListHolder;
8+
use Wikibase\DataModel\Term\AliasesProvider;
9+
use Wikibase\DataModel\Term\AliasGroupList;
10+
use Wikibase\DataModel\Term\DescriptionsProvider;
811
use Wikibase\DataModel\Term\Fingerprint;
912
use Wikibase\DataModel\Term\FingerprintHolder;
13+
use Wikibase\DataModel\Term\LabelsProvider;
14+
use Wikibase\DataModel\Term\TermList;
1015

1116
/**
1217
* Represents a single Wikibase property.
@@ -16,8 +21,10 @@
1621
*
1722
* @licence GNU GPL v2+
1823
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
24+
* @author Bene* < benestar.wikimedia@gmail.com >
1925
*/
20-
class Property implements EntityDocument, FingerprintHolder, StatementListHolder {
26+
class Property implements EntityDocument, FingerprintHolder, StatementListHolder,
27+
LabelsProvider, DescriptionsProvider, AliasesProvider {
2128

2229
const ENTITY_TYPE = 'property';
2330

@@ -111,6 +118,39 @@ public function setFingerprint( Fingerprint $fingerprint ) {
111118
$this->fingerprint = $fingerprint;
112119
}
113120

121+
/**
122+
* @see LabelsProvider::getLabels
123+
*
124+
* @since 6.0
125+
*
126+
* @return TermList
127+
*/
128+
public function getLabels() {
129+
return $this->fingerprint->getLabels();
130+
}
131+
132+
/**
133+
* @see DescriptionsProvider::getDescriptions
134+
*
135+
* @since 6.0
136+
*
137+
* @return TermList
138+
*/
139+
public function getDescriptions() {
140+
return $this->fingerprint->getDescriptions();
141+
}
142+
143+
/**
144+
* @see AliasesProvider::getAliasGroups
145+
*
146+
* @since 6.0
147+
*
148+
* @return AliasGroupList
149+
*/
150+
public function getAliasGroups() {
151+
return $this->fingerprint->getAliasGroups();
152+
}
153+
114154
/**
115155
* @param string $languageCode
116156
* @param string $value

tests/unit/Entity/ItemTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ public function testClearRemovesAllButId() {
241241

242242
$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
243243
$this->assertTrue( $item->getFingerprint()->isEmpty() );
244+
$this->assertTrue( $item->getLabels()->isEmpty() );
245+
$this->assertTrue( $item->getDescriptions()->isEmpty() );
246+
$this->assertTrue( $item->getAliasGroups()->isEmpty() );
244247
$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
245248
$this->assertTrue( $item->getStatements()->isEmpty() );
246249
}
@@ -250,6 +253,9 @@ public function testEmptyConstructor() {
250253

251254
$this->assertNull( $item->getId() );
252255
$this->assertTrue( $item->getFingerprint()->isEmpty() );
256+
$this->assertTrue( $item->getLabels()->isEmpty() );
257+
$this->assertTrue( $item->getDescriptions()->isEmpty() );
258+
$this->assertTrue( $item->getAliasGroups()->isEmpty() );
253259
$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
254260
$this->assertTrue( $item->getStatements()->isEmpty() );
255261
}
@@ -720,4 +726,67 @@ public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
720726
$this->assertSame( $fingerprint, $newFingerprint );
721727
}
722728

729+
public function testGetLabels() {
730+
$item = new Item();
731+
$item->setLabel( 'en', 'foo' );
732+
733+
$this->assertEquals(
734+
new TermList( array(
735+
new Term( 'en', 'foo' )
736+
) ),
737+
$item->getLabels()
738+
);
739+
}
740+
741+
public function testGetDescriptions() {
742+
$item = new Item();
743+
$item->setDescription( 'en', 'foo bar' );
744+
745+
$this->assertEquals(
746+
new TermList( array(
747+
new Term( 'en', 'foo bar' )
748+
) ),
749+
$item->getDescriptions()
750+
);
751+
}
752+
753+
public function testGetAliasGroups() {
754+
$item = new Item();
755+
$item->setAliases( 'en', array( 'foo', 'bar' ) );
756+
757+
$this->assertEquals(
758+
new AliasGroupList( array(
759+
new AliasGroup( 'en', array( 'foo', 'bar' ) )
760+
) ),
761+
$item->getAliasGroups()
762+
);
763+
}
764+
765+
public function testGetLabels_sameListAsFingerprint() {
766+
$item = new Item();
767+
768+
$this->assertSame(
769+
$item->getFingerprint()->getLabels(),
770+
$item->getLabels()
771+
);
772+
}
773+
774+
public function testGetDescriptions_sameListAsFingerprint() {
775+
$item = new Item();
776+
777+
$this->assertSame(
778+
$item->getFingerprint()->getDescriptions(),
779+
$item->getDescriptions()
780+
);
781+
}
782+
783+
public function testGetAliasGroups_sameListAsFingerprint() {
784+
$item = new Item();
785+
786+
$this->assertSame(
787+
$item->getFingerprint()->getAliasGroups(),
788+
$item->getAliasGroups()
789+
);
790+
}
791+
723792
}

tests/unit/Entity/PropertyTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,4 +578,67 @@ public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
578578
$this->assertSame( $fingerprint, $newFingerprint );
579579
}
580580

581+
public function testGetLabels() {
582+
$property = Property::newFromType( 'string' );
583+
$property->setLabel( 'en', 'foo' );
584+
585+
$this->assertEquals(
586+
new TermList( array(
587+
new Term( 'en', 'foo' )
588+
) ),
589+
$property->getLabels()
590+
);
591+
}
592+
593+
public function testGetDescriptions() {
594+
$property = Property::newFromType( 'string' );
595+
$property->setDescription( 'en', 'foo bar' );
596+
597+
$this->assertEquals(
598+
new TermList( array(
599+
new Term( 'en', 'foo bar' )
600+
) ),
601+
$property->getDescriptions()
602+
);
603+
}
604+
605+
public function testGetAliasGroups() {
606+
$property = Property::newFromType( 'string' );
607+
$property->setAliases( 'en', array( 'foo', 'bar' ) );
608+
609+
$this->assertEquals(
610+
new AliasGroupList( array(
611+
new AliasGroup( 'en', array( 'foo', 'bar' ) )
612+
) ),
613+
$property->getAliasGroups()
614+
);
615+
}
616+
617+
public function testGetLabels_sameListAsFingerprint() {
618+
$property = Property::newFromType( 'string' );
619+
620+
$this->assertSame(
621+
$property->getFingerprint()->getLabels(),
622+
$property->getLabels()
623+
);
624+
}
625+
626+
public function testGetDescriptions_sameListAsFingerprint() {
627+
$property = Property::newFromType( 'string' );
628+
629+
$this->assertSame(
630+
$property->getFingerprint()->getDescriptions(),
631+
$property->getDescriptions()
632+
);
633+
}
634+
635+
public function testGetAliasGroups_sameListAsFingerprint() {
636+
$property = Property::newFromType( 'string' );
637+
638+
$this->assertSame(
639+
$property->getFingerprint()->getAliasGroups(),
640+
$property->getAliasGroups()
641+
);
642+
}
643+
581644
}

0 commit comments

Comments
 (0)