Skip to content

RFC: filter support for collections #505

@JeroenDeDauw

Description

@JeroenDeDauw

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/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions