2626 * @license GPL-2.0+
2727 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
2828 * @author Bene* < benestar.wikimedia@gmail.com >
29+ * @author Thiemo Mättig
2930 */
3031class StatementList implements IteratorAggregate, Comparable, Countable {
3132
@@ -74,8 +75,23 @@ public function getPropertyIds() {
7475 return $ propertyIds ;
7576 }
7677
77- public function addStatement ( Statement $ statement ) {
78- $ this ->statements [] = $ statement ;
78+ /**
79+ * @since 1.0, setting an index is supported since 6.1
80+ * @see ReferenceList::addReference
81+ *
82+ * @param Statement $statement
83+ * @param int|null $index New position of the added statement, or null to append.
84+ *
85+ * @throws InvalidArgumentException
86+ */
87+ public function addStatement ( Statement $ statement , $ index = null ) {
88+ if ( $ index === null ) {
89+ $ this ->statements [] = $ statement ;
90+ } elseif ( is_int ( $ index ) && $ index >= 0 ) {
91+ array_splice ( $ this ->statements , $ index , 0 , array ( $ statement ) );
92+ } else {
93+ throw new InvalidArgumentException ( '$index must be a non-negative integer or null ' );
94+ }
7995 }
8096
8197 /**
@@ -91,7 +107,7 @@ public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references
91107 $ statement = new Statement ( $ mainSnak , $ qualifiers , $ references );
92108 $ statement ->setGuid ( $ guid );
93109
94- $ this ->addStatement ( $ statement ) ;
110+ $ this ->statements [] = $ statement ;
95111 }
96112
97113 /**
@@ -139,7 +155,7 @@ public function getByPropertyId( PropertyId $id ) {
139155
140156 foreach ( $ this ->statements as $ statement ) {
141157 if ( $ statement ->getPropertyId ()->equals ( $ id ) ) {
142- $ statementList ->addStatement ( $ statement ) ;
158+ $ statementList ->statements [] = $ statement ;
143159 }
144160 }
145161
@@ -159,7 +175,7 @@ public function getByRank( $acceptableRanks ) {
159175
160176 foreach ( $ this ->statements as $ statement ) {
161177 if ( array_key_exists ( $ statement ->getRank (), $ acceptableRanks ) ) {
162- $ statementList ->addStatement ( $ statement ) ;
178+ $ statementList ->statements [] = $ statement ;
163179 }
164180 }
165181
@@ -318,7 +334,7 @@ public function filter( StatementFilter $filter ) {
318334
319335 foreach ( $ this ->statements as $ statement ) {
320336 if ( $ filter ->statementMatches ( $ statement ) ) {
321- $ statementList ->addStatement ( $ statement ) ;
337+ $ statementList ->statements [] = $ statement ;
322338 }
323339 }
324340
0 commit comments