Skip to content

Commit

Permalink
refactor: split up of the languge class (#3202)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Nov 3, 2024
1 parent 40d24b2 commit cbdacc2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 68 deletions.
2 changes: 2 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public function get(string $item): mixed

/**
* Fetches and returns all configuration items into an array.
*
* @return string[]
*/
public function getAll(): array
{
Expand Down
162 changes: 94 additions & 68 deletions phpmyfaq/src/phpMyFAQ/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ public function __construct(private readonly Configuration $configuration)

/**
* Returns an array of country codes for a specific FAQ record ID,
* specific category ID or all languages used by FAQ records , categories.
* specific category ID or all languages used by FAQ records, categories.
*
* @param int $id ID
* @param int $faqId ID
* @param string $table Specifies table
* @return string[]
*/
public function isLanguageAvailable(int $id, string $table = 'faqdata'): array
public function isLanguageAvailable(int $faqId, string $table = 'faqdata'): array
{
$output = [];

if ($id === 0) {
if ($faqId === 0) {
$distinct = ' DISTINCT ';
$where = '';
} else {
$distinct = '';
$where = ' WHERE id = ' . $id;
$where = ' WHERE id = ' . $faqId;
}

$query = sprintf(
Expand Down Expand Up @@ -93,69 +93,10 @@ public function isLanguageAvailable(int $id, string $table = 'faqdata'): array
*/
public function setLanguage(bool $configDetection, string $configLanguage): string
{
$detectedLang = [];
self::getUserAgentLanguage();

// Get language from: _POST, _GET, _COOKIE, phpMyFAQ configuration and the automatic language detection
$detectedLang['post'] = Filter::filterInput(INPUT_POST, 'language', FILTER_SANITIZE_SPECIAL_CHARS);
if (!is_null($detectedLang['post']) && !self::isASupportedLanguage($detectedLang['post'])) {
$detectedLang['post'] = null;
}

// Get the user language
$detectedLang['get'] = Filter::filterInput(INPUT_GET, 'lang', FILTER_SANITIZE_SPECIAL_CHARS);
if (!is_null($detectedLang['get']) && !self::isASupportedLanguage($detectedLang['get'])) {
$detectedLang['get'] = null;
}

// Get the faq record language
$detectedLang['artget'] = Filter::filterInput(INPUT_GET, 'artlang', FILTER_SANITIZE_SPECIAL_CHARS);
if (!is_null($detectedLang['artget']) && !self::isASupportedLanguage($detectedLang['artget'])) {
$detectedLang['artget'] = null;
}

// Get the language from the session
if (isset($_SESSION['lang']) && self::isASupportedLanguage($_SESSION['lang'])) {
$detectedLang['session'] = trim((string) $_SESSION['lang']);
}

// Get the language from the config
$confLangCode = str_replace(['language_', '.php'], '', $configLanguage);
if (self::isASupportedLanguage($confLangCode)) {
$detectedLang['config'] = $confLangCode;
}

// Detect the browser's language
if (($configDetection) && self::isASupportedLanguage($this->acceptLanguage)) {
$detectedLang['detection'] = strtolower($this->acceptLanguage);
}

// Select the language
if (isset($detectedLang['post'])) {
self::$language = $detectedLang['post'];
$detectedLang = null;
unset($detectedLang);
} elseif (isset($detectedLang['get'])) {
self::$language = $detectedLang['get'];
} elseif (isset($detectedLang['artget'])) {
self::$language = $detectedLang['artget'];
} elseif (isset($detectedLang['session'])) {
self::$language = $detectedLang['session'];
$detectedLang = null;
unset($detectedLang);
} elseif (isset($detectedLang['detection'])) {
self::$language = $detectedLang['detection'];
$detectedLang = null;
unset($detectedLang);
} elseif (isset($detectedLang['config'])) {
self::$language = $detectedLang['config'];
$detectedLang = null;
unset($detectedLang);
} else {
self::$language = 'en'; // just a last fallback
}

return $_SESSION['lang'] = self::$language;
$detectedLang = $this->detectLanguage($configDetection, $configLanguage);
self::$language = $this->selectLanguage($detectedLang);
$_SESSION['lang'] = self::$language;
return self::$language;
}

public function setLanguageByAcceptLanguage(): string
Expand Down Expand Up @@ -183,6 +124,91 @@ public function getLanguage(): string
return self::$language;
}

/**
* Detects the language.
*
* @param bool $configDetection Configuration detection
* @param string $configLanguage Language from configuration
* @return string[]
*/
private function detectLanguage(bool $configDetection, string $configLanguage): array
{
$detectedLang = [];
$this->getUserAgentLanguage();

$detectedLang['post'] = $this->getPostLanguage();
$detectedLang['get'] = $this->getGetLanguage();
$detectedLang['artget'] = $this->getArtGetLanguage();
$detectedLang['session'] = $this->getSessionLanguage();
$detectedLang['config'] = $this->getConfigLanguage($configLanguage);
$detectedLang['detection'] = $this->getDetectionLanguage($configDetection);

return $detectedLang;
}

private function getPostLanguage(): ?string
{
$lang = Filter::filterInput(INPUT_POST, 'language', FILTER_SANITIZE_SPECIAL_CHARS);
return $this->isASupportedLanguage($lang) ? $lang : null;
}

private function getGetLanguage(): ?string
{
$lang = Filter::filterInput(INPUT_GET, 'lang', FILTER_SANITIZE_SPECIAL_CHARS);
return $this->isASupportedLanguage($lang) ? $lang : null;
}

private function getArtGetLanguage(): ?string
{
$lang = Filter::filterInput(INPUT_GET, 'artlang', FILTER_SANITIZE_SPECIAL_CHARS);
return $this->isASupportedLanguage($lang) ? $lang : null;
}

private function getSessionLanguage(): ?string
{
$lang = $_SESSION['lang'] ?? null;
return $this->isASupportedLanguage($lang) ? trim((string) $lang) : null;
}

private function getConfigLanguage(string $configLanguage): ?string
{
$lang = str_replace(['language_', '.php'], '', $configLanguage);
return $this->isASupportedLanguage($lang) ? $lang : null;
}

private function getDetectionLanguage(bool $configDetection): ?string
{
return ($configDetection && $this->isASupportedLanguage($this->acceptLanguage))
?
strtolower($this->acceptLanguage)
:
null;
}

/**
* Selects the language.
*
* @param string[] $detectedLanguage Detected language
*/
private function selectLanguage(array $detectedLanguage): string
{
if (isset($detectedLanguage['post'])) {
return $detectedLanguage['post'];
} elseif (isset($detectedLanguage['get'])) {
return $detectedLanguage['get'];
} elseif (isset($detectedLanguage['artget'])) {
return $detectedLanguage['artget'];
} elseif (isset($detectedLanguage['session'])) {
return $detectedLanguage['session'];
} elseif (isset($detectedLanguage['detection'])) {
return $detectedLanguage['detection'];
} elseif (isset($detectedLanguage['config'])) {
return $detectedLanguage['config'];
} else {
return 'en';
}
}

/**
* Gets the accepted language from the user agent.
*
Expand Down

0 comments on commit cbdacc2

Please sign in to comment.