/** * Returns true if the server preference contains a partial language that matches the language in the client * preference. * * e.g. An server variant of en-* would match en, en-US but not es-ES * * @param string $fromField * @param MatchedPreferenceInterface $matchedPreference * @param PreferenceInterface $newClientPref * * @return bool */ private function partialLangMatches($fromField, MatchedPreferenceInterface $matchedPreference, PreferenceInterface $newClientPref) { $serverPref = $matchedPreference->getServerPreference(); $oldClientPref = $matchedPreference->getClientPreference(); // Note that this only supports the simplest case of (e.g.) en-* matching en-GB and en-US, additional // Language tags are explicitly ignored list($clientMainLang) = explode('-', $newClientPref->getVariant()); list($serverMainLang) = explode('-', $serverPref->getVariant()); return PreferenceInterface::LANGUAGE === $fromField && PreferenceInterface::PARTIAL_WILDCARD === $serverPref->getPrecedence() && $clientMainLang == $serverMainLang && $newClientPref->getPrecedence() > $oldClientPref->getPrecedence(); }
/** * Comparison function for quality factors of matched preferences. * * @param MatchedPreferenceInterface $lValue * @param MatchedPreferenceInterface $rValue * * @return int -1, 0, 1 (see usort() callback for meaning) */ private function compareQualityFactorPair(MatchedPreferenceInterface $lValue, MatchedPreferenceInterface $rValue) { // Build a list of quality factor comparisons to perform; highest preference given to quality factor products, // followed by those provided by the client & finally the server provided. $compareList = array(array('left' => $lValue, 'right' => $rValue), array('left' => $lValue->getClientPreference(), 'right' => $rValue->getClientPreference()), array('left' => $lValue->getServerPreference(), 'right' => $rValue->getServerPreference())); $result = 0; foreach ($compareList as $compare) { $result = $this->compareQualityFactor($compare['left'], $compare['right']); // If a non matching result was found then we have the result of our comparison if (0 !== $result) { break; } } return $result; }