Skip to content

Commit 074812c

Browse files
committed
Merge pull request #414 from wmde/indexof
Add StatementList::indexOf
2 parents 4431686 + 66d450c commit 074812c

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/Statement/StatementList.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,24 @@ public function isEmpty() {
282282
return empty( $this->statements );
283283
}
284284

285+
/**
286+
* @since 3.0
287+
*
288+
* @param Statement $statement
289+
*
290+
* @return int|bool
291+
*/
292+
public function getIndexByGuid( Statement $statement ) {
293+
$index = 0;
294+
295+
foreach ( $this->statements as $s ) {
296+
if ( $s->getGuid() === $statement->getGuid() ) {
297+
return $index;
298+
}
299+
$index++;
300+
}
301+
302+
return false;
303+
}
304+
285305
}

tests/unit/Statement/StatementListTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,50 @@ public function testWhenNoPreferredStatements_getBestStatementsReturnsOnlyNormal
541541
);
542542
}
543543

544+
public function testGivenNotPresentStatement_getIndexByGuidReturnsFalse() {
545+
$statements = new StatementList();
546+
547+
$this->assertFalse( $statements->getIndexByGuid( $this->getStatement( 1, 'kittens' ) ) );
548+
}
549+
550+
public function testGivenPresentStatement_getIndexByGuidReturnsItsIndex() {
551+
$statements = new StatementList( array(
552+
$this->getStatement( 43, 'kittens43' ),
553+
$this->getStatement( 42, 'kittens42' ),
554+
$this->getStatement( 41, 'kittens41' ),
555+
) );
556+
557+
$this->assertSame(
558+
1,
559+
$statements->getIndexByGuid( $this->getStatement( 42, 'kittens42' ) )
560+
);
561+
}
562+
563+
public function testGivenDoublyPresentStatement_getIndexByGuidReturnsTheFirstIndex() {
564+
$statements = new StatementList( array(
565+
$this->getStatement( 43, 'kittens43' ),
566+
$this->getStatement( 42, 'kittens42' ),
567+
$this->getStatement( 41, 'kittens41' ),
568+
$this->getStatement( 42, 'kittens42' ),
569+
) );
570+
571+
$this->assertSame(
572+
1,
573+
$statements->getIndexByGuid( $this->getStatement( 42, 'kittens42' ) )
574+
);
575+
}
576+
577+
public function testGivenDifferentStatementWithSameGuid_getIndexByGuidReturnsItsIndex() {
578+
$statements = new StatementList( array(
579+
$this->getStatement( 1, 'kittens43' ),
580+
$this->getStatement( 2, 'kittens42' ),
581+
$this->getStatement( 3, 'kittens41' ),
582+
) );
583+
584+
$this->assertSame(
585+
2,
586+
$statements->getIndexByGuid( $this->getStatement( 42, 'kittens41' ) )
587+
);
588+
}
589+
544590
}

0 commit comments

Comments
 (0)