src/Matcher.php
public function get(): Collection
    {
        if ($this->specifications()->all()->isEmpty()) {
            return $this->getCandidates();
        }

        $scores = [];

        $this->specifications()->all()->map(function (AttributeScore $attributeScore) {
            return $this->getMatchingScoreByAttributeScore($attributeScore);
        })->each(function (Collection $matchingScoreByAttributeScore) use (&$scores) {
            $matchingScoreByAttributeScore->each(function ($score, $productKey) use (&$scores) {
                if (!isset($scores[$productKey])) {
                    $scores[$productKey] = 0;
                }

                $scores[$productKey] += $score;
            });
        });

        return $this->getCandidates()->map(function (CanBeSpecified $candidate, $productKey) use ($scores) {
            $score = $scores[$productKey];

            return compact('candidate', 'score');
        })->sortByDesc('score')->map(function ($candidateWithScore): CanBeSpecified {
            return $candidateWithScore['candidate'];
        })->values();
    }
public function get(): Collection
    {
        if ($this->specifications()->all()->isEmpty()) {
            return $this->getCandidates();
        }

        $scores = [];

        $this->specifications()->all()->map(function (AttributeScore $attributeScore) {
            return $this->getMatchingScoreByAttributeScore($attributeScore);
        })->each(function (Collection $matchingScoreByAttributeScore) use (&$scores) {
            $matchingScoreByAttributeScore->each(function ($score, $productKey) use (&$scores) {
                if (!isset($scores[$productKey])) {
                    $scores[$productKey] = 0;
                }

                $scores[$productKey] += $score;
            });
        });

        return $this->getCandidates()->map(function (CanBeSpecified $candidate, $productKey) use ($scores) {
            $score = $scores[$productKey];

            return compact('candidate', 'score');
        })->sortByDesc('score')->map(function ($candidateWithScore): CanBeSpecified {
            return $candidateWithScore['candidate'];
        })->values();
    }
public function get ( ) : Collection { if ( $ this -> specifications ( ) -> all ( ) -> isEmpty ( ) ) { return $ this -> getCandidates ( ) ; } $ scores = [ ] ; $ this -> specifications ( ) -> all ( ) -> map ( function ( AttributeScore $ attributeScore ) { return $ this -> getMatchingScoreByAttributeScore ( $ attributeScore ) ; } ) -> each ( function ( Collection $ matchingScoreByAttributeScore ) use ( & $ scores ) { $ matchingScoreByAttributeScore -> each ( function ( $ score , $ productKey ) use ( & $ scores ) { if ( ! isset ( $ scores [ $ productKey ] ) ) { $ scores [ $ productKey ] = 0 ; } $ scores [ $ productKey ] += $ score ; } ) ; } ) ; return $ this -> getCandidates ( ) -> map ( function ( CanBeSpecified $ candidate , $ productKey ) use ( $ scores ) { $ score = $ scores [ $ productKey ] ; return compact ( 'candidate' , 'score' ) ; } ) -> sortByDesc ( 'score' ) -> map ( function ( $ candidateWithScore ) : CanBeSpecified { return $ candidateWithScore [ 'candidate' ] ; } ) -> values ( ) ; }
Returns a collection where the candidaties are sorted based
on how close they are to the specifications.

@return \Illuminate\Support\Collection
Returns a collection where the candidaties are sorted based on how close they are to the specifications .