ソースを参照

Allow language parsing for 2letter only.

mscherer 1 年間 前
コミット
6a1dbdac7e

+ 12 - 6
src/Utility/Language.php

@@ -95,10 +95,11 @@ class Language {
 	 *
 	 *
 	 * @param array<string> $accepted
 	 * @param array<string> $accepted
 	 * @param array $available
 	 * @param array $available
+	 * @param bool $onlyTwoLetters
 	 * @return string|null
 	 * @return string|null
 	 */
 	 */
-	public static function findFirstMatch(array $accepted, array $available = []) {
-		$matches = static::findMatches($accepted, $available);
+	public static function findFirstMatch(array $accepted, array $available = [], bool $onlyTwoLetters = false) {
+		$matches = static::findMatches($accepted, $available, $onlyTwoLetters);
 		if (!$matches) {
 		if (!$matches) {
 			return null;
 			return null;
 		}
 		}
@@ -116,9 +117,10 @@ class Language {
 	 *
 	 *
 	 * @param array<string> $accepted
 	 * @param array<string> $accepted
 	 * @param array $available
 	 * @param array $available
+	 * @param bool $onlyTwoLetters
 	 * @return array
 	 * @return array
 	 */
 	 */
-	public static function findMatches(array $accepted, array $available = []) {
+	public static function findMatches(array $accepted, array $available = [], bool $onlyTwoLetters = false) {
 		$matches = [];
 		$matches = [];
 		if (!$available) {
 		if (!$available) {
 			$available = static::parseLanguageList();
 			$available = static::parseLanguageList();
@@ -131,7 +133,7 @@ class Language {
 				}
 				}
 
 
 				foreach ($availableValues as $availableValue) {
 				foreach ($availableValues as $availableValue) {
-					$matchingGrade = static::_matchLanguage($acceptedValue, $availableValue);
+					$matchingGrade = static::_matchLanguage($acceptedValue, $availableValue, $onlyTwoLetters);
 					if ($matchingGrade > 0) {
 					if ($matchingGrade > 0) {
 						$q = (string)($availableQuality * $matchingGrade);
 						$q = (string)($availableQuality * $matchingGrade);
 						if ($q === '1') {
 						if ($q === '1') {
@@ -141,7 +143,7 @@ class Language {
 							$matches[$q] = [];
 							$matches[$q] = [];
 						}
 						}
 						if (!in_array($availableValue, $matches[$q])) {
 						if (!in_array($availableValue, $matches[$q])) {
-							$matches[$q][] = $availableValue;
+							$matches[$q][] = $onlyTwoLetters ? $acceptedValue : $availableValue;
 						}
 						}
 					}
 					}
 				}
 				}
@@ -157,11 +159,15 @@ class Language {
 	 *
 	 *
 	 * @param string $a
 	 * @param string $a
 	 * @param string $b
 	 * @param string $b
+	 * @param bool $onlyTwoLetters
 	 * @return float
 	 * @return float
 	 */
 	 */
-	protected static function _matchLanguage($a, $b) {
+	protected static function _matchLanguage($a, $b, bool $onlyTwoLetters = false) {
 		$a = explode('-', strtolower($a));
 		$a = explode('-', strtolower($a));
 		$b = explode('-', strtolower($b));
 		$b = explode('-', strtolower($b));
+		if ($onlyTwoLetters) {
+			return $a[0] === $b[0] ? 1 : 0;
+		}
 
 
 		for ($i = 0, $n = min(count($a), count($b)); $i < $n; $i++) {
 		for ($i = 0, $n = min(count($a), count($b)); $i < $n; $i++) {
 			if ($a[$i] !== $b[$i]) {
 			if ($a[$i] !== $b[$i]) {

+ 1 - 1
src/Utility/Text.php

@@ -385,7 +385,7 @@ class Text extends CakeText {
 	public function entitiesToAscii($str, $all = true) {
 	public function entitiesToAscii($str, $all = true) {
 		if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
 		if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) {
 			for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
 			for ($i = 0, $s = count($matches['0']); $i < $s; $i++) {
-				$digits = $matches['1'][$i];
+				$digits = (int)$matches['1'][$i];
 
 
 				$out = '';
 				$out = '';
 
 

+ 1 - 1
src/View/Icon/Collector/FontAwesome5IconCollector.php

@@ -24,7 +24,7 @@ class FontAwesome5IconCollector {
 		switch ($ext) {
 		switch ($ext) {
 			case 'svg':
 			case 'svg':
 				preg_match_all('/symbol id="([a-z][^"]+)"/', $content, $matches);
 				preg_match_all('/symbol id="([a-z][^"]+)"/', $content, $matches);
-				if (!$matches) {
+				if (empty($matches[1])) {
 					throw new RuntimeException('Cannot parse SVG: ' . $filePath);
 					throw new RuntimeException('Cannot parse SVG: ' . $filePath);
 				}
 				}
 				$icons = $matches[1];
 				$icons = $matches[1];