<commit_before><?php

/*
 * PHP-I18N (https://github.com/delight-im/PHP-I18N)
 * Copyright (c) delight.im (https://www.delight.im/)
 * Licensed under the MIT License (https://opensource.org/licenses/MIT)
 */

namespace Delight\I18n;

/** Handles aspects of the HTTP protocol related to locales */
final class Http {

	/** @var string a regular expression matching significant HTTP “language ranges” or “language tags” */
	const LANGUAGE_RANGE_OR_TAG_REGEX = '/^([a-z]{2,3})(?:-([a-z]{4}))?(?:-([a-z]{2}|[0-9]{3}))?$/i';

	/**
	 * Finds the best match for the languages accepted by the HTTP client in the list of supported languages
	 *
	 * @param string[] $supportedLanguages
	 * @param int|null $leniency (optional) the desired leniency for the lookup as one of the constants from {@see Leniency}
	 * @param string|null $httpAcceptLanguage (optional) the value of the HTTP `Accept-Language` request header
	 * @return string|null
	 */
	public static function matchClientLanguages(array $supportedLanguages, $leniency = null, $httpAcceptLanguage = null) {
		$leniency = !empty($leniency) ? (int) $leniency : Leniency::MODERATE;

		foreach (self::determineClientLanguages($httpAcceptLanguage) as $httpClientLanguage) {
			$desired = self::parseClientLanguage($httpClientLanguage);
			$bestMatchCode = null;
			$bestMatchAffinity = null;

			foreach ($supportedLanguages as $supported) {
				$affinity = Affinity::calculate(
					$desired[0],
					$desired[1],
					$desired[2],
					Locale::toLanguageCode($supported),
					Locale::toScriptCode($supported),
					Locale::toRegionCode($supported)
				);

				if (empty($bestMatchCode) || $affinity > $bestMatchAffinity) {
					$bestMatchCode = $supported;<commit_msg>Refactor 'matchClientLanguages' into new 'matchPreferredLocales'<commit_after><?php

/*
 * PHP-I18N (https://github.com/delight-im/PHP-I18N)
 * Copyright (c) delight.im (https://www.delight.im/)
 * Licensed under the MIT License (https://opensource.org/licenses/MIT)
 */

namespace Delight\I18n;

/** Handles aspects of the HTTP protocol related to locales */
final class Http {

	/** @var string a regular expression matching significant HTTP “language ranges” or “language tags” */
	const LANGUAGE_RANGE_OR_TAG_REGEX = '/^([a-z]{2,3})(?:-([a-z]{4}))?(?:-([a-z]{2}|[0-9]{3}))?$/i';

	/**
	 * Finds the best match for the languages accepted by the HTTP client in the list of supported languages
	 *
	 * @param string[] $supportedLanguages
	 * @param int|null $leniency (optional) the desired leniency for the lookup as one of the constants from {@see Leniency}
	 * @param string|null $httpAcceptLanguage (optional) the value of the HTTP `Accept-Language` request header
	 * @return string|null
	 */
	public static function matchClientLanguages(array $supportedLanguages, $leniency = null, $httpAcceptLanguage = null) {
		return self::matchPreferredLocales(
			self::determineClientLanguages($httpAcceptLanguage),
			$supportedLanguages,
			$leniency
		);
	}

	/**
	 * Finds the best match for the specified locales in the list of supported locales
	 *
	 * @param string[] $supportedLocales
	 * @param string[] $preferredLocales a list of locales in order of preference (most favored first)
	 * @param int|null $leniency (optional) the desired leniency for the lookup as one of the constants from {@see Leniency}
	 * @return string|null
	 */
	public static function matchPreferredLocales(array $preferredLocales, array $supportedLocales, $leniency = null) {
		$leniency = !empty($leniency) ? (int) $leniency : Leniency::MODERATE;

		foreach ($preferredLocales as $preferredLocale) {
			$desired = self::parseClientLanguage($preferredLocale);
			$bestMatchCode = null;
			$bestMatchAffinity = null;

			foreach ($supportedLocales as $supported) {
				$affinity = Affinity::calculate(
					$desired[0],
					$desired[1],
					$desired[2],
					Locale::toLanguageCode($supported),
					Locale::toScriptCode($supported),
					Locale::toRegionCode($supported)
				);

				if (empty($bestMatchCode) || $affinity > $bestMatchAffinity) {
					$bestMatchCode = $supported;
{'fraction_non_alphanumeric': 0.13318616629874908, 'fraction_numerical': 0.007848908511160166, 'mean_word_length': 4.656033287101248, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2914965', 'n_tokens_mistral': 1380, 'n_tokens_neox': 1214, 'n_words': 344}