SubtypeWildcardMatcher::getMatchingIndexes()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 2
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
c 0
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 subtype wildcards (e.g. 'text/*').
17
 */
18
class SubtypeWildcardMatcher implements MatcherInterface
19
{
20
    /**
21
     * @inheritDoc
22
     */
23 54
    public function hasMatch($fromField, array $matchingList, PreferenceInterface $clientPref)
24
    {
25 54
        return PreferenceInterface::MIME === $fromField
26 54
            && PreferenceInterface::PARTIAL_WILDCARD === $clientPref->getPrecedence()
27 54
            && count($this->getMatchingIndexes($matchingList, $clientPref));
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33 6
    public function match($fromField, array $matchingList, PreferenceInterface $clientPref)
34
    {
35 6
        $matchingIndexList = $this->getMatchingIndexes($matchingList, $clientPref);
36
37 6
        foreach ($matchingIndexList as $matchingIndex) {
38 6
            $matchingList[$matchingIndex] = new MatchedPreference(
39 6
                $fromField,
40
                $clientPref,
41 6
                $matchingList[$matchingIndex]->getServerPreference()
42
            );
43
        }
44
45 6
        return $matchingList;
46
    }
47
48
    /**
49
     * Returns an array of indexes for of matches of higher precedence than the existing pairing.
50
     *
51
     * @param MatchedPreferenceInterface[] $matchingList
52
     * @param PreferenceInterface $clientPref
53
     *
54
     * @return int[]
55
     */
56 6
    private function getMatchingIndexes(array $matchingList, PreferenceInterface $clientPref)
57
    {
58 6
        $matchingIndexList = array();
59
60 6
        foreach ($matchingList as $key => $matching) {
61 6
            $serverPref = $matching->getServerPreference();
62 6
            list($clientMimeType) = explode('/', $clientPref->getVariant());
63 6
            list($serverMimeType) = explode('/', $serverPref->getVariant());
64
65 6
            if ($clientMimeType == $serverMimeType
66 6
                && $clientPref->getPrecedence() > $matching->getClientPreference()->getPrecedence()) {
67 6
                $matchingIndexList[] = $key;
68
            }
69
        }
70
71 6
        return $matchingIndexList;
72
    }
73
}
74