<commit_before><?php
/**
 * PHPCI - Continuous Integration for PHP
 *
 * @copyright    Copyright 2014, Block 8 Limited.
 * @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
 * @link         https://www.phptesting.org/
 */

namespace PHPCensor\Helper;

use b8\Config;

/**
 * Languages Helper Class - Handles loading strings files and the strings within them.
 *
 * @package PHPCensor\Helper
 */
class Lang
{
    /**
     * @var string
     */
    protected static $language  = null;

    /**
     * @var array
     */
    protected static $languages = [];

    /**
     * @var array
     */
    protected static $strings = [];

    /**
     * @var array
     */
    protected static $en_strings = [];

    /**
     * Get a specific string from the language file.
     *
     * @param $string
     * @return mixed|string
     */
    public static function get($string)
    {
        $vars = func_get_args();

        if (array_key_exists($string, self::$strings)) {
            $vars[0] = self::$strings[$string];
            return call_user_func_array('sprintf', $vars);
        } elseif ('en' !== self::$language && array_key_exists($string, self::$en_strings)) {
            $vars[0] = self::$en_strings[$string];
            return call_user_func_array('sprintf', $vars);
        }

        return '%%MISSING STRING: ' . $string . '%%';
    }

    /**
     * Output a specific string from the language file.
     */
    public static function out()
    {
        print call_user_func_array(['PHPCensor\Helper\Lang', 'get'], func_get_args());
    }

    /**
     * Get the currently active language.
     *
     * @return string|null
     */
    public static function getLanguage()
    {
        return self::$language;
    }

    /**
     * Try and load a language, and if successful, set it for use throughout the system.
     *
     * @param $language
     *
     * @return bool
     */
    public static function setLanguage($language)
    {
        if (in_array($language, self::$languages)) {
            self::$language = $language;
            self::$strings  = self::loadLanguage();
            return true;
        }

        return false;
    }

    /**
     * Return a list of available languages and their names.
     *
     * @return array
     */
    public static function getLanguageOptions()
    {
        $languages = [];
        foreach (self::$languages as $language) {
            $strings = include_once(SRC_DIR . 'Languages' . DIRECTORY_SEPARATOR . 'lang.' . $language . '.php');
            $languages[$language] = !empty($strings['language_name']) ? $strings['language_name'] : $language;
        }

        return $languages;
    }

    /**
     * Get the strings for the currently active language.
     *
     * @return string[]
     */
    public static function getStrings()
    {
        return self::$strings;
    }

    /**
     * Initialise the Language helper, try load the language file for the user's browser or the configured default.
     *
     * @param Config $config
     * @param string $language_force
     */
    public static function init(Config $config, $language_force = null)
    {
        self::$en_strings = self::loadLanguage('en');
        self::loadAvailableLanguages();

        if ($language_force && self::setLanguage($language_force)) {
            return;
        }
        
        // Try cookies first:
        if (isset($_COOKIE) && array_key_exists('php-censor-language', $_COOKIE) && self::setLanguage($_COOKIE['php-censor-language'])) {
            return;
        }

        // Try user language:
        if (isset($_SERVER) && array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
            $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

            foreach ($langs as $lang) {
                $parts    = explode(';', $lang);
                $language = strtolower($parts[0]);

                if (self::setLanguage($language)) {
                    return;
                }
            }
        }

        // Try the installation default language:
        $language = $config->get('php-censor.basic.language', null);
        if (self::setLanguage($language)) {
            return;
        }

        // Fall back to English:
        self::$language = 'en';
        self::$strings  = self::loadLanguage();
    }<commit_msg>Added const DEFAULT_LANGUAGE to Lang class<commit_after><?php
/**
 * PHPCI - Continuous Integration for PHP
 *
 * @copyright    Copyright 2014, Block 8 Limited.
 * @license      https://github.com/Block8/PHPCI/blob/master/LICENSE.md
 * @link         https://www.phptesting.org/
 */

namespace PHPCensor\Helper;

use b8\Config;

/**
 * Languages Helper Class - Handles loading strings files and the strings within them.
 *
 * @package PHPCensor\Helper
 */
class Lang
{
    const DEFAULT_LANGUAGE = 'en';
    
    /**
     * @var string
     */
    protected static $language  = null;

    /**
     * @var array
     */
    protected static $languages = [];

    /**
     * @var array
     */
    protected static $strings = [];

    /**
     * @var array
     */
    protected static $default_strings = [];

    /**
     * Get a specific string from the language file.
     *
     * @param $string
     * @return mixed|string
     */
    public static function get($string)
    {
        $vars = func_get_args();

        if (array_key_exists($string, self::$strings)) {
            $vars[0] = self::$strings[$string];
            return call_user_func_array('sprintf', $vars);
        } elseif (self::DEFAULT_LANGUAGE !== self::$language && array_key_exists($string, self::$default_strings)) {
            $vars[0] = self::$default_strings[$string];
            return call_user_func_array('sprintf', $vars);
        }

        return '%%MISSING STRING: ' . $string . '%%';
    }

    /**
     * Output a specific string from the language file.
     */
    public static function out()
    {
        print call_user_func_array(['PHPCensor\Helper\Lang', 'get'], func_get_args());
    }

    /**
     * Get the currently active language.
     *
     * @return string|null
     */
    public static function getLanguage()
    {
        return self::$language;
    }

    /**
     * Try and load a language, and if successful, set it for use throughout the system.
     *
     * @param $language
     *
     * @return bool
     */
    public static function setLanguage($language)
    {
        if (in_array($language, self::$languages)) {
            self::$language = $language;
            self::$strings  = self::loadLanguage();
            return true;
        }

        return false;
    }

    /**
     * Return a list of available languages and their names.
     *
     * @return array
     */
    public static function getLanguageOptions()
    {
        $languages = [];
        foreach (self::$languages as $language) {
            $strings = include_once(SRC_DIR . 'Languages' . DIRECTORY_SEPARATOR . 'lang.' . $language . '.php');
            $languages[$language] = !empty($strings['language_name']) ? $strings['language_name'] : $language;
        }

        return $languages;
    }

    /**
     * Get the strings for the currently active language.
     *
     * @return string[]
     */
    public static function getStrings()
    {
        return self::$strings;
    }

    /**
     * Initialise the Language helper, try load the language file for the user's browser or the configured default.
     *
     * @param Config $config
     * @param string $language_force
     */
    public static function init(Config $config, $language_force = null)
    {
        self::$default_strings = self::loadLanguage(self::DEFAULT_LANGUAGE);
        self::loadAvailableLanguages();

        if ($language_force && self::setLanguage($language_force)) {
            return;
        }
        
        // Try cookies first:
        if (isset($_COOKIE) && array_key_exists('php-censor-language', $_COOKIE) && self::setLanguage($_COOKIE['php-censor-language'])) {
            return;
        }

        // Try user language:
        if (isset($_SERVER) && array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
            $langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

            foreach ($langs as $lang) {
                $parts    = explode(';', $lang);
                $language = strtolower($parts[0]);

                if (self::setLanguage($language)) {
                    return;
                }
            }
        }

        // Try the installation default language:
        $language = $config->get('php-censor.language', null);
        if (self::setLanguage($language)) {
            return;
        }

        // Fall back to English:
        self::$language = self::DEFAULT_LANGUAGE;
        self::$strings  = self::loadLanguage();
    }
{'fraction_non_alphanumeric': 0.13402296237353642, 'fraction_numerical': 0.0022735023303398884, 'mean_word_length': 2.0421853388658366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '6861805', 'n_tokens_mistral': 2500, 'n_tokens_neox': 2462, 'n_words': 600}