-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStatementGroup.php
More file actions
130 lines (108 loc) · 2.68 KB
/
StatementGroup.php
File metadata and controls
130 lines (108 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
namespace Wikibase\DataModel\Statement;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\PropertyIdProvider;
/**
* List of statements with the same property id.
*
* @since 4.3
*
* @license GNU GPL v2+
* @author Bene* < benestar.wikimedia@gmail.com >
*/
class StatementGroup implements IteratorAggregate, Countable {
/**
* @var PropertyId
*/
private $propertyId;
/**
* @var Statement[]
*/
private $statements = array();
/**
* @param PropertyId|int $propertyId
*/
public function __construct( $propertyId ) {
if ( is_int( $propertyId ) ) {
$propertyId = PropertyId::newFromNumber( $propertyId );
}
if ( !( $propertyId instanceof PropertyId ) ) {
throw new InvalidArgumentException( '$propertyId must be an integer or an instance of PropertyId' );
}
$this->propertyId = $propertyId;
}
/**
* @param Statement[]|Traversable $statements
* @throws InvalidArgumentException
*/
public function addStatements( $statements ) {
if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) {
throw new InvalidArgumentException( '$statements must be an array or an instance of Traversable' );
}
foreach ( $statements as $statement ) {
if ( !( $statement instanceof Statement ) ) {
throw new InvalidArgumentException( 'Every element in $statements must be an instance of Statement' );
}
$this->addStatement( $statement );
}
}
/**
* @param Statement $statement
* @throws InvalidArgumentException
*/
public function addStatement( Statement $statement ) {
if ( !$statement->getPropertyId()->equals( $this->propertyId ) ) {
throw new InvalidArgumentException( '$statement must have the property id ' . $this->propertyId->getSerialization() );
}
$this->statements[] = $statement;
}
/**
* @return PropertyId
*/
public function getPropertyId() {
return $this->propertyId;
}
/**
* @param int $rank
* @return Statement[]
*/
public function getByRank( $rank ) {
$statements = array();
foreach ( $this->statements as $statement ) {
if ( $statement->getRank() === $rank ) {
$statements[] = $statement;
}
}
return $statements;
}
/**
* @return Traversable
*/
public function getIterator() {
return new ArrayIterator( $this->statements );
}
/**
* @return Statement[] Numerically indexed (non-sparse) array.
*/
public function toArray() {
return $this->statements;
}
/**
* @see Countable::count
*
* @return int
*/
public function count() {
return count( $this->statements );
}
/**
* @return bool
*/
public function isEmpty() {
return empty( $this->statements );
}
}