Skip to content

Commit

Permalink
Allow language parsing for 2letter only.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Sep 9, 2024
1 parent 30a5ba4 commit 6a1dbda
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions src/Utility/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ public static function parseLanguageList($languageList = null, $options = []) {
*
* @param array<string> $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;
}
Expand All @@ -116,9 +117,10 @@ public static function findFirstMatch(array $accepted, array $available = []) {
*
* @param array<string> $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();
Expand All @@ -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') {
Expand All @@ -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;
}
}
}
Expand All @@ -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]) {
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';

Expand Down
2 changes: 1 addition & 1 deletion src/View/Icon/Collector/FontAwesome5IconCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 6a1dbda

Please sign in to comment.