Skip to content

Commit 181e35d

Browse files
committed
Have Item use StatementList
1 parent 824db3c commit 181e35d

2 files changed

Lines changed: 92 additions & 86 deletions

File tree

src/Entity/Item.php

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Wikibase\DataModel\SiteLink;
1717
use Wikibase\DataModel\SiteLinkList;
1818
use Wikibase\DataModel\Snak\Snak;
19+
use Wikibase\DataModel\Statement\StatementList;
1920
use Wikibase\DataModel\Term\Fingerprint;
2021

2122
/**
@@ -37,7 +38,7 @@ class Item extends Entity {
3738
private $siteLinks;
3839

3940
/**
40-
* @var Statement[]
41+
* @var StatementList
4142
*/
4243
private $statements;
4344

@@ -47,9 +48,9 @@ class Item extends Entity {
4748
* @param ItemId|null $id
4849
* @param Fingerprint $fingerprint
4950
* @param SiteLinkList $links
50-
* @param Statement[] $statements
51+
* @param StatementList $statements
5152
*/
52-
public function __construct( ItemId $id = null, Fingerprint $fingerprint, SiteLinkList $links, array $statements ) {
53+
public function __construct( ItemId $id = null, Fingerprint $fingerprint, SiteLinkList $links, StatementList $statements ) {
5354
$this->id = $id;
5455
$this->fingerprint = $fingerprint;
5556
$this->siteLinks = $links;
@@ -182,8 +183,8 @@ public static function newEmpty() {
182183
return new self(
183184
null,
184185
Fingerprint::newEmpty(),
185-
new SiteLinkList( array() ),
186-
array()
186+
new SiteLinkList(),
187+
new StatementList()
187188
);
188189
}
189190

@@ -199,9 +200,7 @@ public function getType() {
199200
}
200201

201202
/**
202-
* @see Entity::newClaim
203-
*
204-
* @since 0.3
203+
* @deprecated since 1.0
205204
*
206205
* @param Snak $mainSnak
207206
*
@@ -222,7 +221,7 @@ public function newClaim( Snak $mainSnak ) {
222221
public function isEmpty() {
223222
return $this->fingerprint->isEmpty()
224223
&& $this->siteLinks->isEmpty()
225-
&& empty( $this->statements );
224+
&& $this->statements->count() === 0;
226225
}
227226

228227
/**
@@ -237,27 +236,6 @@ public function clear() {
237236
$this->statements = array();
238237
}
239238

240-
private function getLinksInDiffFormat() {
241-
$links = array();
242-
243-
/**
244-
* @var SiteLink $siteLink
245-
*/
246-
foreach ( $this->siteLinks as $siteLink ) {
247-
$links[$siteLink->getSiteId()] = array(
248-
'name' => $siteLink->getPageName(),
249-
'badges' => array_map(
250-
function( ItemId $id ) {
251-
return $id->getSerialization();
252-
},
253-
$siteLink->getBadges()
254-
)
255-
);
256-
}
257-
258-
return $links;
259-
}
260-
261239
/**
262240
* @see Entity::patchSpecificFields
263241
*
@@ -299,6 +277,27 @@ function( $idSerialization ) {
299277
}
300278
}
301279

280+
private function getLinksInDiffFormat() {
281+
$links = array();
282+
283+
/**
284+
* @var SiteLink $siteLink
285+
*/
286+
foreach ( $this->siteLinks as $siteLink ) {
287+
$links[$siteLink->getSiteId()] = array(
288+
'name' => $siteLink->getPageName(),
289+
'badges' => array_map(
290+
function( ItemId $id ) {
291+
return $id->getSerialization();
292+
},
293+
$siteLink->getBadges()
294+
)
295+
);
296+
}
297+
298+
return $links;
299+
}
300+
302301
private function patchClaims( ItemDiff $patch ) {
303302
$patcher = new MapPatcher();
304303

@@ -320,26 +319,24 @@ function( Claim $firstClaim, Claim $secondClaim ) {
320319
}
321320

322321
/**
323-
* @see ClaimListAccess::addClaim
324-
*
325-
* @since 0.3
322+
* @deprecated since 1.0, use getStatements instead
326323
*
327-
* @param Claim $claim
324+
* @param Claim $statement This needs to be a Statement as of 1.0
328325
*
329326
* @throws InvalidArgumentException
330327
*/
331-
public function addClaim( Claim $claim ) {
332-
if ( $claim->getGuid() === null ) {
328+
public function addClaim( Claim $statement ) {
329+
if ( $statement->getGuid() === null ) {
333330
throw new InvalidArgumentException( 'Can\'t add a Claim without a GUID.' );
334331
}
335332

336-
$this->statements[] = $claim;
333+
$this->statements->addStatement( $statement );
337334
}
338335

339336
/**
340337
* @since 1.0
341338
*
342-
* @return Statement[]
339+
* @return StatementList
343340
*/
344341
public function getStatements() {
345342
return $this->statements;
@@ -351,30 +348,25 @@ public function getStatements() {
351348
* @return Statement[]
352349
*/
353350
public function getClaims() {
354-
return $this->statements;
351+
return $this->statements->toArray();
355352
}
356353

357354
/**
358-
* @since 0.4
355+
* @deprecated since 1.0, use getStatements instead
359356
*
360357
* @param Claims $claims
361358
*/
362359
public function setClaims( Claims $claims ) {
363-
$this->statements = iterator_to_array( $claims );
360+
$this->statements = new StatementList( iterator_to_array( $claims ) );
364361
}
365362

366363
/**
367-
* Convenience function to check if the entity contains any claims.
368-
*
369-
* On top of being a convenience function, this implementation allows for doing
370-
* the check without forcing an unstub in contrast to count( $this->getClaims() ).
371-
*
372-
* @since 0.2
364+
* @deprecated since 1.0, use getStatements instead
373365
*
374366
* @return bool
375367
*/
376368
public function hasClaims() {
377-
return !empty( $this->statements );
369+
return $this->statements->count() !== 0;
378370
}
379371

380372
/**
@@ -405,12 +397,7 @@ public function equals( $that ) {
405397
*/
406398
return $this->fingerprint->equals( $that->fingerprint )
407399
&& $this->siteLinks->equals( $that->siteLinks )
408-
&& $this->statementsEqual( $that->statements );
409-
}
410-
411-
private function statementsEqual( array $statements ) {
412-
$list = new Claims( $this->statements );
413-
return $list->equals( new Claims( $statements ) );
400+
&& $that->statements->equals( $that->statements );
414401
}
415402

416403
}

tests/unit/Entity/ItemTest.php

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
use Wikibase\DataModel\Snak\PropertyValueSnak;
2626
use Wikibase\DataModel\Snak\Snak;
2727
use Wikibase\DataModel\Snak\SnakList;
28+
use Wikibase\DataModel\Statement\StatementList;
29+
use Wikibase\DataModel\Term\Fingerprint;
2830

2931
/**
3032
* @covers Wikibase\DataModel\Entity\Item
@@ -446,28 +448,29 @@ public function diffProvider() {
446448
public function patchProvider() {
447449
$argLists = parent::patchProvider();
448450

449-
$claim0 = new Claim( new PropertyNoValueSnak( 42 ) );
450-
$claim1 = new Claim( new PropertySomeValueSnak( 42 ) );
451-
$claim2 = new Claim( new PropertyValueSnak( 42, new StringValue( 'ohi' ) ) );
452-
$claim3 = new Claim( new PropertyNoValueSnak( 1 ) );
451+
$statement0 = new Statement( new PropertyNoValueSnak( 42 ) );
452+
$statement1 = new Statement( new PropertySomeValueSnak( 42 ) );
453+
$statement2 = new Statement( new PropertyValueSnak( 42, new StringValue( 'ohi' ) ) );
454+
$statement3 = new Statement( new PropertyNoValueSnak( 1 ) );
453455

454-
$claim0->setGuid( 'claim0' );
455-
$claim1->setGuid( 'claim1' );
456-
$claim2->setGuid( 'claim2' );
457-
$claim3->setGuid( 'claim3' );
456+
$statement0->setGuid( 'claim0' );
457+
$statement1->setGuid( 'claim1' );
458+
$statement2->setGuid( 'claim2' );
459+
$statement3->setGuid( 'claim3' );
458460

459-
$source = $this->getNewEmpty();
460-
$source->addClaim( $claim0 );
461-
$source->addClaim( $claim1 );
461+
$source = Item::newEmpty();
462+
$source->getStatements()->addStatement( $statement0 );
463+
$source->getStatements()->addStatement( $statement1 );
462464
$patch = new ItemDiff( array( 'claim' => new Diff( array(
463-
'claim0' => new DiffOpRemove( $claim0 ),
464-
'claim2' => new DiffOpAdd( $claim2 ),
465-
'claim3' => new DiffOpAdd( $claim3 )
465+
'claim0' => new DiffOpRemove( $statement0 ),
466+
'claim2' => new DiffOpAdd( $statement2 ),
467+
'claim3' => new DiffOpAdd( $statement3 )
466468
), false ) ) );
467-
$expected = $this->getNewEmpty();
468-
$expected->addClaim( $claim1 );
469-
$expected->addClaim( $claim2 );
470-
$expected->addClaim( $claim3 );
469+
470+
$expected = Item::newEmpty();
471+
$expected->getStatements()->addStatement( $statement1 );
472+
$expected->getStatements()->addStatement( $statement2 );
473+
$expected->getStatements()->addStatement( $statement3 );
471474

472475
$argLists[] = array( $source, $patch, $expected );
473476

@@ -483,14 +486,13 @@ public function patchProvider() {
483486
), true ),
484487
), true ),
485488
) );
489+
486490
$expected = clone $source;
487-
$expected->addSiteLink(
488-
new SiteLink(
489-
'enwiki',
490-
'Berlin',
491-
array(
492-
new ItemId( 'Q42' )
493-
)
491+
$expected->getSiteLinkList()->addNewSiteLink(
492+
'enwiki',
493+
'Berlin',
494+
array(
495+
new ItemId( 'Q42' )
494496
)
495497
);
496498

@@ -519,13 +521,11 @@ public function patchProvider() {
519521
), true )
520522
) );
521523
$expected = $this->getNewEmpty();
522-
$expected->addSiteLink(
523-
new SiteLink(
524-
'enwiki',
525-
'Foobar',
526-
array(
527-
new ItemId( 'Q149' )
528-
)
524+
$expected->getSiteLinkList()->addNewSiteLink(
525+
'enwiki',
526+
'Foobar',
527+
array(
528+
new ItemId( 'Q149' )
529529
)
530530
);
531531

@@ -786,4 +786,23 @@ public function testClearRemovesAllButId() {
786786
$this->assertEmpty( $item->getStatements() );
787787
}
788788

789+
public function testCanConstructWithStatementList() {
790+
$statement = new Statement( new PropertyNoValueSnak( 42 ) );
791+
$statement->setGuid( 'meh' );
792+
793+
$statements = new StatementList( array( $statement ) );
794+
795+
$item = new Item(
796+
null,
797+
Fingerprint::newEmpty(),
798+
new SiteLinkList(),
799+
$statements
800+
);
801+
802+
$this->assertEquals(
803+
$statements,
804+
$item->getStatements()
805+
);
806+
}
807+
789808
}

0 commit comments

Comments
 (0)