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\Preference\Builder; |
||
10 | |||
11 | use ptlis\ConNeg\Exception\InvalidVariantException; |
||
12 | use ptlis\ConNeg\Preference\Preference; |
||
13 | |||
14 | /** |
||
15 | * MIME preference builder. |
||
16 | */ |
||
17 | class MimePreferenceBuilder extends AbstractPreferenceBuilder |
||
18 | { |
||
19 | /** |
||
20 | * @inheritDoc |
||
21 | */ |
||
22 | 27 | protected function validateVariant($variant) |
|
23 | { |
||
24 | 27 | if ($this->isFromServer && strlen($variant) > 0) { |
|
25 | 27 | $variantParts = explode('/', $variant); |
|
26 | |||
27 | // Too many/few slashes |
||
28 | 27 | if (2 !== count($variantParts)) { |
|
29 | 1 | throw new InvalidVariantException('"' . $variant . '" is not a valid mime type'); |
|
30 | } |
||
31 | |||
32 | // Wildcards disallowed in server preferences |
||
33 | 26 | if (in_array('*', $variantParts)) { |
|
34 | 1 | throw new InvalidVariantException('Wildcards are not allowed in server-provided variants.'); |
|
35 | } |
||
36 | } |
||
37 | 25 | } |
|
38 | |||
39 | /** |
||
40 | * @inheritDoc |
||
41 | */ |
||
42 | 36 | protected function normalizeVariant($variant) |
|
43 | { |
||
44 | 36 | $variantParts = explode('/', $variant); |
|
45 | |||
46 | // Ignore malformed preferences |
||
47 | 36 | if (2 !== count($variantParts)) { |
|
48 | 1 | $variant = ''; |
|
49 | |||
50 | } else { |
||
51 | // Variants in form of */foo aren't valid |
||
52 | 35 | list($mimeType, $subType) = $variantParts; |
|
53 | 35 | if ('*' === $mimeType && '*' !== $subType) { |
|
54 | 1 | $variant = ''; |
|
55 | } |
||
56 | } |
||
57 | |||
58 | 36 | return $variant; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @inheritDoc |
||
63 | * |
||
64 | * @throws \RuntimeException if the HTTP field was not provided |
||
65 | */ |
||
66 | 38 | public function get() |
|
67 | { |
||
68 | 38 | if (is_null($this->fromField)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
69 | 1 | throw new \RuntimeException( |
|
70 | 1 | 'The HTTP field must be provided to the builder.' |
|
71 | ); |
||
72 | } |
||
73 | |||
74 | 37 | return new Preference( |
|
75 | 37 | $this->variant, |
|
76 | 37 | $this->getQualityFactor(), |
|
77 | 37 | $this->getPrecedence() |
|
78 | ); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Get the variants's quality factor, defaulting to 0 on absent variant. |
||
83 | * |
||
84 | * @return float |
||
85 | */ |
||
86 | 37 | private function getQualityFactor() |
|
87 | { |
||
88 | 37 | $qFactor = 0.0; |
|
89 | |||
90 | 37 | if (2 === count(explode('/', $this->variant))) { |
|
91 | 33 | $qFactor = $this->qFactor; |
|
92 | } |
||
93 | |||
94 | 37 | return $qFactor; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Determine the precedence from the variant. |
||
99 | * |
||
100 | * @return int |
||
101 | */ |
||
102 | 37 | private function getPrecedence() |
|
103 | { |
||
104 | 37 | $precedence = Preference::ABSENT; |
|
105 | |||
106 | // A variant is present |
||
107 | 37 | $variantParts = explode('/', $this->variant); |
|
108 | 37 | if (2 === count($variantParts)) { |
|
109 | 33 | list($mimeType, $subType) = $variantParts; |
|
110 | |||
111 | 33 | $precedence = Preference::COMPLETE; |
|
112 | |||
113 | 33 | if ('*' === $mimeType) { |
|
114 | 4 | $precedence = Preference::WILDCARD; |
|
115 | |||
116 | 33 | } elseif ('*' === $subType) { |
|
117 | 6 | $precedence = Preference::PARTIAL_WILDCARD; |
|
118 | } |
||
119 | } |
||
120 | |||
121 | 37 | return $precedence; |
|
122 | } |
||
123 | } |
||
124 |