Skip to content

Commit c88c51c

Browse files
committed
Merge pull request #163 from wmde/statementlist
Have Item use StatementList
2 parents b5e0008 + b00ada5 commit c88c51c

5 files changed

Lines changed: 246 additions & 90 deletions

File tree

src/Claim/ClaimList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Wikibase\DataModel\Snak\Snaks;
1111

1212
/**
13-
* Ordered, non-unique, collection of Claim objects.
13+
* Ordered, non-unique, mutable, collection of Claim objects.
1414
* Provides various filter operations though does not do any indexing by default.
1515
*
1616
* @since 1.0

src/Entity/Item.php

Lines changed: 50 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,61 +319,63 @@ 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;
346343
}
347344

345+
/**
346+
* @since 1.0
347+
*
348+
* @param StatementList $statements
349+
*/
350+
public function setStatements( StatementList $statements ) {
351+
$this->statements = $statements;
352+
}
353+
348354
/**
349355
* @deprecated since 1.0, use getStatements instead
350356
*
351357
* @return Statement[]
352358
*/
353359
public function getClaims() {
354-
return $this->statements;
360+
return $this->statements->toArray();
355361
}
356362

357363
/**
358-
* @since 0.4
364+
* @deprecated since 1.0, use setStatements instead
359365
*
360366
* @param Claims $claims
361367
*/
362368
public function setClaims( Claims $claims ) {
363-
$this->statements = iterator_to_array( $claims );
369+
$this->statements = new StatementList( iterator_to_array( $claims ) );
364370
}
365371

366372
/**
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
373+
* @deprecated since 1.0, use getStatements instead
373374
*
374375
* @return bool
375376
*/
376377
public function hasClaims() {
377-
return !empty( $this->statements );
378+
return $this->statements->count() !== 0;
378379
}
379380

380381
/**
@@ -405,12 +406,7 @@ public function equals( $that ) {
405406
*/
406407
return $this->fingerprint->equals( $that->fingerprint )
407408
&& $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 ) );
409+
&& $that->statements->equals( $that->statements );
414410
}
415411

416412
}

src/Statement/StatementList.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
use Wikibase\DataModel\Snak\Snaks;
1515

1616
/**
17-
* Ordered, non-unique, collection of Statement objects.
17+
* Ordered, non-unique, mutable, collection of Statement objects.
1818
* Provides various filter operations though does not do any indexing by default.
1919
*
2020
* @since 1.0
2121
*
2222
* @licence GNU GPL v2+
2323
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
2424
*/
25-
class StatementList implements \IteratorAggregate {
25+
class StatementList implements \IteratorAggregate, \Comparable, \Countable {
2626

2727
/**
2828
* @var Statement[]
@@ -142,4 +142,45 @@ public function toArray() {
142142
return $this->statements;
143143
}
144144

145+
/**
146+
* @see Countable::count
147+
* @return int
148+
*/
149+
public function count() {
150+
return count( $this->statements );
151+
}
152+
153+
/**
154+
* @see Comparable::equals
155+
*
156+
* @param mixed $statementList
157+
*
158+
* @return boolean
159+
*/
160+
public function equals( $statementList ) {
161+
if ( !( $statementList instanceof self ) ) {
162+
return false;
163+
}
164+
165+
if ( $this->count() !== $statementList->count() ) {
166+
return false;
167+
}
168+
169+
return $this->statementsEqual( $statementList->statements );
170+
}
171+
172+
private function statementsEqual( array $statements ) {
173+
reset( $statements );
174+
175+
foreach ( $this->statements as $statement ) {
176+
if ( !$statement->equals( current( $statements ) ) ) {
177+
return false;
178+
}
179+
180+
next( $statements );
181+
}
182+
183+
return true;
184+
}
185+
145186
}

0 commit comments

Comments
 (0)