diff --git a/src/Utility/Language.php b/src/Utility/Language.php index c21d48a0..00d07315 100644 --- a/src/Utility/Language.php +++ b/src/Utility/Language.php @@ -95,10 +95,11 @@ public static function parseLanguageList($languageList = null, $options = []) { * * @param array $accepted * @param array $available + * @param bool $onlyTwoLetters * @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) { return null; } @@ -116,9 +117,10 @@ public static function findFirstMatch(array $accepted, array $available = []) { * * @param array $accepted * @param array $available + * @param bool $onlyTwoLetters * @return array */ - public static function findMatches(array $accepted, array $available = []) { + public static function findMatches(array $accepted, array $available = [], bool $onlyTwoLetters = false) { $matches = []; if (!$available) { $available = static::parseLanguageList(); @@ -131,7 +133,7 @@ public static function findMatches(array $accepted, array $available = []) { } foreach ($availableValues as $availableValue) { - $matchingGrade = static::_matchLanguage($acceptedValue, $availableValue); + $matchingGrade = static::_matchLanguage($acceptedValue, $availableValue, $onlyTwoLetters); if ($matchingGrade > 0) { $q = (string)($availableQuality * $matchingGrade); if ($q === '1') { @@ -141,7 +143,7 @@ public static function findMatches(array $accepted, array $available = []) { $matches[$q] = []; } if (!in_array($availableValue, $matches[$q])) { - $matches[$q][] = $availableValue; + $matches[$q][] = $onlyTwoLetters ? $acceptedValue : $availableValue; } } } @@ -157,11 +159,15 @@ public static function findMatches(array $accepted, array $available = []) { * * @param string $a * @param string $b + * @param bool $onlyTwoLetters * @return float */ - protected static function _matchLanguage($a, $b) { + protected static function _matchLanguage($a, $b, bool $onlyTwoLetters = false) { $a = explode('-', strtolower($a)); $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++) { if ($a[$i] !== $b[$i]) { diff --git a/src/Utility/Text.php b/src/Utility/Text.php index 0820ddbd..919de59f 100644 --- a/src/Utility/Text.php +++ b/src/Utility/Text.php @@ -385,7 +385,7 @@ public function asciiToEntities($str) { public function entitiesToAscii($str, $all = true) { if (preg_match_all('/\&#(\d+)\;/', $str, $matches)) { for ($i = 0, $s = count($matches['0']); $i < $s; $i++) { - $digits = $matches['1'][$i]; + $digits = (int)$matches['1'][$i]; $out = ''; diff --git a/src/View/Icon/Collector/FontAwesome5IconCollector.php b/src/View/Icon/Collector/FontAwesome5IconCollector.php index 5f35adc9..2ae71932 100644 --- a/src/View/Icon/Collector/FontAwesome5IconCollector.php +++ b/src/View/Icon/Collector/FontAwesome5IconCollector.php @@ -24,7 +24,7 @@ public static function collect(string $filePath): array { switch ($ext) { case 'svg': preg_match_all('/symbol id="([a-z][^"]+)"/', $content, $matches); - if (!$matches) { + if (empty($matches[1])) { throw new RuntimeException('Cannot parse SVG: ' . $filePath); } $icons = $matches[1];