public function findSubdefsByRecordReferenceFromCollection($records, array $names = null)
    {
        $subdefs = $this->reduceRecordReferenceCollection(
            $records,
            function (array &$carry, array $subdefs, array $references) {
                $subdefsByRecordId = [];

                /** @var \media_subdef $subdef */
                foreach ($subdefs as $subdef) {
                    $recordId = $subdef->get_record_id();

                    if (!isset($subdefsByRecordId[$recordId])) {
                        $subdefsByRecordId[$recordId] = [];
                    }

                    $subdefsByRecordId[$recordId][$subdef->get_name()] = $subdef;
                }

                /** @var RecordReferenceInterface $reference */
                foreach ($references as $index => $reference) {
                    if (isset($subdefsByRecordId[$reference->getRecordId()])) {
                        $carry[$index] = $subdefsByRecordId[$reference->getRecordId()];
                    };
                }

                return $carry;
            },
            array_fill_keys(array_keys($records instanceof \Traversable ? iterator_to_array($records) : $records), []),
            $names
        );

        $reordered = [];

        foreach ($records as $index => $record) {
            $reordered[$index] = $subdefs[$index];
        }

        return $reordered;
    }
public function findSubdefsByRecordReferenceFromCollection($records, array $names = null)
    {
        $subdefs = $this->reduceRecordReferenceCollection(
            $records,
            function (array &$carry, array $subdefs, array $references) {
                $subdefsByRecordId = [];

                /** @var \media_subdef $subdef */
                foreach ($subdefs as $subdef) {
                    $recordId = $subdef->get_record_id();

                    if (!isset($subdefsByRecordId[$recordId])) {
                        $subdefsByRecordId[$recordId] = [];
                    }

                    $subdefsByRecordId[$recordId][$subdef->get_name()] = $subdef;
                }

                /** @var RecordReferenceInterface $reference */
                foreach ($references as $index => $reference) {
                    if (isset($subdefsByRecordId[$reference->getRecordId()])) {
                        $carry[$index] = $subdefsByRecordId[$reference->getRecordId()];
                    };
                }

                return $carry;
            },
            array_fill_keys(array_keys($records instanceof \Traversable ? iterator_to_array($records) : $records), []),
            $names
        );

        $reordered = [];

        foreach ($records as $index => $record) {
            $reordered[$index] = $subdefs[$index];
        }

        return $reordered;
    }
public function findSubdefsByRecordReferenceFromCollection ( $ records , array $ names = null ) { $ subdefs = $ this -> reduceRecordReferenceCollection ( $ records , function ( array & $ carry , array $ subdefs , array $ references ) { $ subdefsByRecordId = [ ] ; /** @var \media_subdef $subdef */ foreach ( $ subdefs as $ subdef ) { $ recordId = $ subdef -> get_record_id ( ) ; if ( ! isset ( $ subdefsByRecordId [ $ recordId ] ) ) { $ subdefsByRecordId [ $ recordId ] = [ ] ; } $ subdefsByRecordId [ $ recordId ] [ $ subdef -> get_name ( ) ] = $ subdef ; } /** @var RecordReferenceInterface $reference */ foreach ( $ references as $ index => $ reference ) { if ( isset ( $ subdefsByRecordId [ $ reference -> getRecordId ( ) ] ) ) { $ carry [ $ index ] = $ subdefsByRecordId [ $ reference -> getRecordId ( ) ] ; } ; } return $ carry ; } , array_fill_keys ( array_keys ( $ records instanceof \ Traversable ? iterator_to_array ( $ records ) : $ records ) , [ ] ) , $ names ) ; $ reordered = [ ] ; foreach ( $ records as $ index => $ record ) { $ reordered [ $ index ] = $ subdefs [ $ index ] ; } return $ reordered ; }
Returns all available subdefs grouped by each record reference and by its name

@param RecordReferenceInterface[]|RecordReferenceCollection $records
@param null|array $names
@return \media_subdef[][]
Returns all available subdefs grouped by each record reference and by its name