1 | <?php |
||
18 | class FieldParser |
||
19 | { |
||
20 | /** |
||
21 | * @var PreferenceBuilderInterface |
||
22 | */ |
||
23 | private $prefBuilder; |
||
24 | |||
25 | /** |
||
26 | * @var PreferenceBuilderInterface |
||
27 | */ |
||
28 | private $mimePrefBuilder; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Constructor. |
||
33 | * |
||
34 | * @param PreferenceBuilderInterface $prefBuilder |
||
35 | * @param PreferenceBuilderInterface $mimePrefBuilder |
||
36 | */ |
||
37 | 89 | public function __construct( |
|
44 | |||
45 | /** |
||
46 | * Accepts a tokenized Accept* HTTP field and returns an array of Preference value objects. |
||
47 | * |
||
48 | * @throws InvalidVariantException |
||
49 | * |
||
50 | * @param array<string> $tokenList Parsed tokens |
||
51 | * @param bool $serverField If true the field came from the server & we error on malformed data, otherwise we |
||
52 | * suppress errors for client preferences.. |
||
53 | * @param string $fromField Which field the tokens came from |
||
54 | * |
||
55 | * @return PreferenceInterface[] |
||
56 | */ |
||
57 | 89 | public function parse(array $tokenList, $serverField, $fromField) |
|
69 | |||
70 | /** |
||
71 | * Accepts tokens for a single variant and returns the Preference value object encapsulating that data. |
||
72 | * |
||
73 | * @throws InvalidVariantException |
||
74 | * |
||
75 | * @param array<string> $tokenBundle |
||
76 | * @param bool $serverField |
||
77 | * @param string $fromField |
||
78 | * |
||
79 | * @return null|PreferenceInterface |
||
80 | */ |
||
81 | 81 | private function parseBundle(array $tokenBundle, $serverField, $fromField) |
|
100 | |||
101 | /** |
||
102 | * Splits the token list into variant & parameter arrays. |
||
103 | * |
||
104 | * @throws InvalidVariantException |
||
105 | * |
||
106 | * @param array<string> $tokenBundle |
||
107 | * @param string $fromField |
||
108 | * |
||
109 | * @return string[][] |
||
110 | */ |
||
111 | 81 | private function splitVariantAndParamTokens(array $tokenBundle, $fromField) |
|
124 | |||
125 | /** |
||
126 | * Accepts the bundled tokens for variant & parameter data and builds the Preference value object. |
||
127 | * |
||
128 | * @param array<string> $variantTokenList |
||
129 | * @param array<array<string>> $paramBundleList |
||
130 | * @param bool $serverField |
||
131 | * @param string $fromField |
||
132 | * |
||
133 | * @return PreferenceInterface |
||
134 | */ |
||
135 | 78 | private function createPreference(array $variantTokenList, array $paramBundleList, $serverField, $fromField) |
|
153 | |||
154 | /** |
||
155 | * Returns true if the provided parameter bundle is a quality factor. |
||
156 | * |
||
157 | * @param array<string> $paramBundle |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 76 | private function isQualityFactor(array $paramBundle) |
|
167 | |||
168 | /** |
||
169 | * Splits token list up into one bundle per variant for later processing. |
||
170 | * |
||
171 | * @param array<string> $tokenList |
||
172 | * @param string $targetToken The token to split the list up by. |
||
173 | * |
||
174 | * @return array<array<string>> an array of arrays - the child array contains the tokens for a single variant. |
||
175 | */ |
||
176 | 89 | private function bundleTokens(array $tokenList, $targetToken) |
|
201 | |||
202 | /** |
||
203 | * Checks to see if the bundle is valid for a mime type, if an anomaly is detected then an exception is thrown. |
||
204 | * |
||
205 | * @throws InvalidVariantException |
||
206 | * |
||
207 | * @param array<string> $bundle |
||
208 | */ |
||
209 | 35 | private function validateBundleMimeVariant(array $bundle) |
|
221 | |||
222 | /** |
||
223 | * Checks to see if the parameters are correctly formed, if an anomaly is detected then an exception is thrown. |
||
224 | * |
||
225 | * @throws InvalidVariantException |
||
226 | * |
||
227 | * @param array<array<string>> $paramBundleList |
||
228 | * @param bool $serverField If true the field came from the server & we error on malformed data otherwise |
||
229 | * we suppress errors for client preferences. |
||
230 | */ |
||
231 | 79 | private function validateParamBundleList(array $paramBundleList, $serverField) |
|
244 | |||
245 | /** |
||
246 | * Validate a single parameter bundle. |
||
247 | * |
||
248 | * Due to the way we process tokens the only required should be for the correct number of tokens. |
||
249 | * |
||
250 | * @throws InvalidVariantException |
||
251 | * |
||
252 | * @param string[] $paramBundle |
||
253 | */ |
||
254 | 77 | private function validateParamBundle(array $paramBundle) |
|
263 | |||
264 | /** |
||
265 | * Get a preference builder for the specified HTTP field. |
||
266 | * |
||
267 | * @param string $fromField |
||
268 | * |
||
269 | * @return PreferenceBuilderInterface |
||
270 | */ |
||
271 | 78 | private function getBuilder($fromField) |
|
279 | } |
||
280 |