WildcardMatcher::match()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright   (c) 2006-present brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace ptlis\ConNeg\Negotiator\Matcher;
10
11
use ptlis\ConNeg\Preference\Matched\MatchedPreference;
12
use ptlis\ConNeg\Preference\Matched\MatchedPreferenceInterface;
13
use ptlis\ConNeg\Preference\PreferenceInterface;
14
15
/**
16
 * Matcher for full wildcards (e.g. '*' and '*\/*
17
 */
18
class WildcardMatcher implements MatcherInterface
19
{
20
    /**
21
     * @inheritDoc
22
     */
23 54
    public function hasMatch($fromField, array $matchingList, PreferenceInterface $clientPref)
24
    {
25 54
        return PreferenceInterface::WILDCARD === $clientPref->getPrecedence()
26 54
            && count($this->getMatchingIndexes($matchingList, $clientPref));
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32 16
    public function match($fromField, array $matchingList, PreferenceInterface $clientPref)
33
    {
34 16
        $matchingIndexList = $this->getMatchingIndexes($matchingList, $clientPref);
35
36 16
        foreach ($matchingIndexList as $matchingIndex) {
37 16
            $matchingList[$matchingIndex] = new MatchedPreference(
38 16
                $fromField,
39
                $clientPref,
40 16
                $matchingList[$matchingIndex]->getServerPreference()
41
            );
42
        }
43
44 16
        return $matchingList;
45
    }
46
47
    /**
48
     * Returns an array of indexes for of matches of higher precedence than the existing pairing.
49
     *
50
     * @param MatchedPreferenceInterface[] $matchingList
51
     * @param PreferenceInterface $clientPref
52
     *
53
     * @return int[]
54
     */
55 16
    private function getMatchingIndexes(array $matchingList, PreferenceInterface $clientPref)
56
    {
57 16
        $matchingIndexList = array();
58
59 16
        foreach ($matchingList as $key => $matching) {
60 16
            if ($clientPref->getPrecedence() > $matching->getClientPreference()->getPrecedence()) {
61 16
                $matchingIndexList[] = $key;
62
            }
63
        }
64
65 16
        return $matchingIndexList;
66
    }
67
}
68