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 | |||
13 | /** |
||
14 | * Shared preference building implementation. |
||
15 | */ |
||
16 | abstract class AbstractPreferenceBuilder implements PreferenceBuilderInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $isFromServer = false; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $fromField; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $variant = ''; |
||
32 | |||
33 | /** |
||
34 | * @var float |
||
35 | */ |
||
36 | protected $qFactor = 1; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * @inheritDoc |
||
41 | */ |
||
42 | 95 | public function setFromServer($isFromServer) |
|
43 | { |
||
44 | 95 | $clone = clone $this; |
|
45 | 95 | $clone->isFromServer = $isFromServer; |
|
46 | |||
47 | 95 | return $clone; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @inheritDoc |
||
52 | */ |
||
53 | 102 | public function setFromField($fromField) |
|
54 | { |
||
55 | 102 | $clone = clone $this; |
|
56 | 102 | $clone->fromField = $fromField; |
|
57 | |||
58 | 102 | return $clone; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @inheritDoc |
||
63 | * |
||
64 | * @throws InvalidVariantException If the provided variant is not valid. |
||
65 | */ |
||
66 | 95 | public function setVariant($variant) |
|
67 | { |
||
68 | 95 | if ($this->isFromServer) { |
|
69 | 74 | $this->validateVariant($variant); |
|
70 | } |
||
71 | |||
72 | 92 | $clone = clone $this; |
|
73 | 92 | $clone->variant = $this->normalizeVariant($variant); |
|
74 | |||
75 | 92 | return $clone; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | * |
||
81 | * @throws InvalidVariantException If an invalid quality factor in encountered when building an server preference. |
||
82 | */ |
||
83 | 90 | public function setQualityFactor($qFactor) |
|
84 | { |
||
85 | 90 | if ($this->isFromServer && !$this->validQualityFactor($qFactor)) { |
|
86 | 3 | throw new InvalidVariantException('Invalid quality factor "' . $qFactor . '" in server preferences'); |
|
87 | } |
||
88 | |||
89 | 87 | $qFactor = $this->normalizeQualityFactor($qFactor); |
|
90 | |||
91 | 87 | $clone = clone $this; |
|
92 | 87 | $clone->qFactor = $qFactor; |
|
93 | |||
94 | 87 | return $clone; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Validate the variant, returning true if the variant is valid. |
||
99 | * |
||
100 | * @throws InvalidVariantException If the provided variant is not valid. |
||
101 | * |
||
102 | * @param string $variant |
||
103 | */ |
||
104 | abstract protected function validateVariant($variant); |
||
105 | |||
106 | /** |
||
107 | * Normalises the variant. |
||
108 | * |
||
109 | * @param string $variant |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 56 | protected function normalizeVariant($variant) |
|
114 | { |
||
115 | 56 | return $variant; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Validate the quality factor, returning true if the quality factor is valid. |
||
120 | * |
||
121 | * @param float $qFactor |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | 51 | private function validQualityFactor($qFactor) |
|
126 | { |
||
127 | 51 | return is_numeric($qFactor) && $qFactor >= 0 && $qFactor <= 1; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Normalises the provided quality factor, ensuring that the value returned is a float between 0 and 1 (inclusive). |
||
132 | * |
||
133 | * @param float $qFactor |
||
134 | * |
||
135 | * @return float |
||
136 | */ |
||
137 | 87 | private function normalizeQualityFactor($qFactor) |
|
138 | { |
||
139 | 87 | if (!is_numeric($qFactor)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
140 | 1 | $qFactor = 1.0; |
|
141 | |||
142 | 86 | } elseif ($qFactor < 0) { |
|
143 | 1 | $qFactor = 0.0; |
|
144 | |||
145 | 85 | } elseif ($qFactor > 1) { |
|
146 | 1 | $qFactor = 1.0; |
|
147 | |||
148 | } else { |
||
149 | 84 | $qFactor = floatval($qFactor); |
|
150 | } |
||
151 | |||
152 | 87 | return $qFactor; |
|
153 | } |
||
154 | } |
||
155 |