<commit_before><?php

/**
 * Common tools methods.
 *
 * @category Asymptix PHP Framework
 * @author <NAME> <<EMAIL>>
 * @copyright (c) 2009 - 2015, <NAME>
 * @license http://opensource.org/licenses/MIT
 */
class Tools {

    /**
     * Check if form was submitted.
     *
     * @return boolean
     */
    public static function isFormSubmitted() {
        return (self::getFieldValue("submitBtn") !== null);
    }

    /**
     * Gets value of the field from $_REQUEST or $_SESSION (is some REQUEST values
     * needs to be stored by scenario).
     *
     * @param mixed $fieldName String name of the field or complex name as array.
     *
     * @return mixed Value of the field, NULL otherwise.
     */
    public static function getFieldValue($fieldName) {
        $fieldName = self::parseComplexFieldName($fieldName);
        $value = null;

        try {
            $value = self::getArrayElement($_REQUEST, $fieldName);
        } catch (Exception $ex) {
            try {
                if (isset($_SESSION['_post'])) {
                    $value = self::getArrayElement($_SESSION['_post'], $fieldName);
                }
            } catch (Exception $ex) {
                return null;
            }
        }

        if (!is_null($value)) {
            if (is_array($value)) {
                return $value;
            } elseif (is_integer($value)) {
                return intval($value);
            }
            return $value;
        }

        return null;
    }

    /**
     * Sets value of the field or creates new field by pair $fieldName => $fieldValue.
     *
     * @global array<mixed> $_FIELDS Global fields list.
     * @param mixed $fieldName Name of the field as a string or complex name as
     *            an array.
     * @param mixed $fieldValue Value of the field.
     */
    public static function setFieldValue($fieldName, $fieldValue) {
        global $_FIELDS;

        $fieldName = self::parseComplexFieldName($fieldName);

        if (!is_array($fieldName)) {
            $_FIELDS[$fieldName] = $fieldValue;
        } else {
            $array = &$_FIELDS;
            for ($i = 0; $i < count($fieldName); $i++) {
                $key = $fieldName[$i];

                if ($i < (count($fieldName) - 1)) {
                    if (!isset($array[$key])) { // declare value as empty array as not last element
                        $array[$key] = array();
                    } else {
                        if (!is_array($array[$key])) { // detect if current value is array because not last element
                            throw new Exception("Try to assign value as array element to the not an array");
                        }
                    }
                    $array = &$array[$key];
                } else { // last element
                    $array[$key] = $fieldValue;
                }
            }
        }
    }

    /**
     * Returns global filter value if exists.
     *
     * @global array $_FILTER Global filters array.
     * @param string $filterName Name of the filter field.
     * @param mixed $defaultValue Default value if value doesn't exist.<commit_msg>isFilterExists() method added to the Tools class.<commit_after><?php

/**
 * Common tools methods.
 *
 * @category Asymptix PHP Framework
 * @author <NAME> <<EMAIL>>
 * @copyright (c) 2009 - 2015, <NAME>
 * @license http://opensource.org/licenses/MIT
 */
class Tools {

    /**
     * Check if form was submitted.
     *
     * @return boolean
     */
    public static function isFormSubmitted() {
        return (self::getFieldValue("submitBtn") !== null);
    }

    /**
     * Gets value of the field from $_REQUEST or $_SESSION (is some REQUEST values
     * needs to be stored by scenario).
     *
     * @param mixed $fieldName String name of the field or complex name as array.
     *
     * @return mixed Value of the field, NULL otherwise.
     */
    public static function getFieldValue($fieldName) {
        $fieldName = self::parseComplexFieldName($fieldName);
        $value = null;

        try {
            $value = self::getArrayElement($_REQUEST, $fieldName);
        } catch (Exception $ex) {
            try {
                if (isset($_SESSION['_post'])) {
                    $value = self::getArrayElement($_SESSION['_post'], $fieldName);
                }
            } catch (Exception $ex) {
                return null;
            }
        }

        if (!is_null($value)) {
            if (is_array($value)) {
                return $value;
            } elseif (is_integer($value)) {
                return intval($value);
            }
            return $value;
        }

        return null;
    }

    /**
     * Sets value of the field or creates new field by pair $fieldName => $fieldValue.
     *
     * @global array<mixed> $_FIELDS Global fields list.
     * @param mixed $fieldName Name of the field as a string or complex name as
     *            an array.
     * @param mixed $fieldValue Value of the field.
     */
    public static function setFieldValue($fieldName, $fieldValue) {
        global $_FIELDS;

        $fieldName = self::parseComplexFieldName($fieldName);

        if (!is_array($fieldName)) {
            $_FIELDS[$fieldName] = $fieldValue;
        } else {
            $array = &$_FIELDS;
            for ($i = 0; $i < count($fieldName); $i++) {
                $key = $fieldName[$i];

                if ($i < (count($fieldName) - 1)) {
                    if (!isset($array[$key])) { // declare value as empty array as not last element
                        $array[$key] = array();
                    } else {
                        if (!is_array($array[$key])) { // detect if current value is array because not last element
                            throw new Exception("Try to assign value as array element to the not an array");
                        }
                    }
                    $array = &$array[$key];
                } else { // last element
                    $array[$key] = $fieldValue;
                }
            }
        }
    }

    /**
     * Returns TRUE if filter exists or FALSE otherwise.
     *
     * @param string $filterName Name of the filter field.
     * @return boolean
     */
    public static function isFilterExists($filterName) {
        return isset($_FILTER[$filterName]);
    }

    /**
     * Returns global filter value if exists.
     *
     * @global array $_FILTER Global filters array.
     * @param string $filterName Name of the filter field.
     * @param mixed $defaultValue Default value if value doesn't exist.
{'fraction_non_alphanumeric': 0.1151220991961171, 'fraction_numerical': 0.003033520400424693, 'mean_word_length': 1.6631663974151858, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3774032', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1693, 'n_words': 549}