wikimedia/mediawiki-extensions-Wikibase

View on GitHub
lib/packages/wikibase/data-model-services/src/Statement/Grouper/ByPropertyIdStatementGrouper.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Wikibase\DataModel\Services\Statement\Grouper;

use Wikibase\DataModel\Statement\StatementList;

/**
 * @since 3.2
 *
 * @license GPL-2.0-or-later
 * @author Thiemo Kreuz
 */
class ByPropertyIdStatementGrouper implements StatementGrouper {

    /**
     * @param StatementList $statements
     *
     * @return StatementList[] An associative array, mapping property id serializations to
     *  StatementList objects.
     */
    public function groupStatements( StatementList $statements ) {
        /** @var StatementList[] $groups */
        $groups = [];

        foreach ( $statements->toArray() as $statement ) {
            $id = $statement->getPropertyId()->getSerialization();

            if ( !isset( $groups[$id] ) ) {
                $groups[$id] = new StatementList();
            }

            $groups[$id]->addStatement( $statement );
        }

        return $groups;
    }

}