We could implement filter support for our collections. I got this idea by looking at Ardent.
This means adding a filter method to StatementList and other collections where we find need for it.
You can then do things such as these without needing to write a foreach:
$referencedStatements = $statements->filter( function( Statement $statement ) {
return !$statement->getReferences->isEmpty();
} );
This approach is more OOP / high level than the more imperative foreach.
Implementation is simple:
public function filter( callable $filter ) {
$statementList = new self();
foreach ( $this->statements as $statement ) {
if ( call_user_func( $filter, $statement ) ) {
$statementList->addStatement( $statement );
}
}
return $statementList;
}
Example usecase for this in Wikibase.git is explained in this commit message: https://gerrit.wikimedia.org/r/#/c/219041/
We could implement filter support for our collections. I got this idea by looking at Ardent.
This means adding a filter method to StatementList and other collections where we find need for it.
You can then do things such as these without needing to write a foreach:
This approach is more OOP / high level than the more imperative foreach.
Implementation is simple:
Example usecase for this in Wikibase.git is explained in this commit message: https://gerrit.wikimedia.org/r/#/c/219041/