<commit_before><?php
/**
 * SimpleTypes
 *
 * Copyright (c) 2015, <NAME> <<EMAIL>>.
 *
 * @package   SimpleTypes
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015 <NAME> <<EMAIL>>
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 * @link      http://github.com/smetdenis/simpletypes
 */

namespace SmetDenis\SimpleTypes;

/**
 * Class Type
 * @package SmetDenis\SimpleTypes
 */
class Parser
{
    const ERROR_FATAL   = 1;
    const ERROR_NOTICE  = 2;
    const ERROR_DEFAULT = 3;

    /**
     * @var string
     */
    protected $default = '';

    /**
     * @var array
     */
    protected $rules = array();

    /**
     * @var array
     */
    protected $config = array(
        'error_mode' => self::ERROR_FATAL
    );

    /**
     * @param string $default
     * @param array  $ruleList
     */
    public function __construct($default = '', array $ruleList = array())
    {
        $this->rules   = $ruleList;
        $this->default = $default;
    }

    /**
     * @param mixed  $data
     * @param string $forceRule
     * @return array
     * @throws Exception
     */
    public function parse($data = null, $forceRule = null)
    {
        $value = null;
        $rule  = null;

        if (is_array($data)) {
            $value = isset($data[0]) ? $data[0] : null;
            $rule  = isset($data[1]) ? $data[1] : null;
            return $this->parse($value, $rule);

        } else {
            $data = trim($data);
            $reg  = '#(.*)(' . implode('|', $this->getCodeList()) . ')(.*)#iu';

            if (preg_match($reg, $data, $matches)) {
                $rule  = $matches[2];
                $value = 0;
                if (!empty($matches[1])) {
                    $value = $matches[1];
                } elseif (!empty($matches[3])) {
                    $value = $matches[3];
                }
            }
        }

        if (is_null($value)) {
            $value = $data;
        }

        $value = $this->clean($value);
        $rule  = $this->checkRule($rule);

        if ($forceRule) {
            $rule = $forceRule;
        }

        if (empty($rule)) {
            $rule = $this->default;
        }

        return array($value, $rule);
    }

    /**
     * @return array
     */
    public function getCodeList()
    {
        return array_keys($this->rules);
    }

    /**
     * @param $value
     * @return float|string
     */
    public function clean($value)
    {
        $value = trim($value);

        $value = preg_replace("#[^0-9-+eE,.]#", '', $value);
        if (!preg_match('#\d[eE][-+]\d#', $value)) { // remove exponential format
            $value = str_replace(array('e', 'E'), '', $value);
        }

        $value = str_replace(',', '.', $value);
        $value = (float)$value;
        $value = round($value, Formatter::ROUND_DEFAULT);

        return $value;
    }

    /**
     * @param string $rule
     * @return bool|string
     * @throws Exception
     */
    public function checkRule($rule)
    {
        $rule = strtolower(trim($rule));

        if (empty($rule)) {
            return $this->default;
        }

        if (isset($this->rules[$rule])) {
            return $rule;
        }

        if ($rule) {
            if ($this->config['error_mode'] == self::ERROR_FATAL) {
                throw new Exception('Undefined rule: ' . $rule);

            } elseif ($this->config['error_mode'] == self::ERROR_NOTICE) {
                trigger_error('Undefined currency: ' . $rule, E_USER_NOTICE);
            }

            return isset($this->rules[$this->default]) ? $this->default : '';
        }

        return false;
    }

    /**
     * @param $newRule
     */
    public function addRule($newRule)
    {
        $this->rules[$newRule] = $newRule;
    }

    /**
     * @param string $rule
     */
    public function removeRule($rule)
    {
        if (isset($this->rules[$rule])) {
            unset($this->rules[$rule]);
        }
    }
}<commit_msg>remove unuseful config<commit_after><?php
/**
 * SimpleTypes
 *
 * Copyright (c) 2015, <NAME> <<EMAIL>>.
 *
 * @package   SimpleTypes
 * @author    <NAME> <<EMAIL>>
 * @copyright 2015 <NAME> <<EMAIL>>
 * @license   http://www.gnu.org/licenses/gpl.html GNU/GPL
 * @link      http://github.com/smetdenis/simpletypes
 */

namespace SmetDenis\SimpleTypes;

/**
 * Class Type
 * @package SmetDenis\SimpleTypes
 */
class Parser
{
    /**
     * @var string
     */
    protected $default = '';

    /**
     * @var array
     */
    protected $rules = array();

    /**
     * @param string $default
     * @param array  $ruleList
     */
    public function __construct($default = '', array $ruleList = array())
    {
        $this->rules   = $ruleList;
        $this->default = $default;
    }

    /**
     * @param mixed  $data
     * @param string $forceRule
     * @return array
     * @throws Exception
     */
    public function parse($data = null, $forceRule = null)
    {
        $value = null;
        $rule  = null;

        if (is_array($data)) {
            $value = isset($data[0]) ? $data[0] : null;
            $rule  = isset($data[1]) ? $data[1] : null;
            return $this->parse($value, $rule);

        } else {
            $data = trim($data);
            $reg  = '#(.*)(' . implode('|', $this->getCodeList()) . ')(.*)#iu';

            if (preg_match($reg, $data, $matches)) {
                $rule  = $matches[2];
                $value = 0;
                if (!empty($matches[1])) {
                    $value = $matches[1];
                } elseif (!empty($matches[3])) {
                    $value = $matches[3];
                }
            }
        }

        if (is_null($value)) {
            $value = $data;
        }

        $value = $this->clean($value);
        $rule  = $this->checkRule($rule);

        if ($forceRule) {
            $rule = $forceRule;
        }

        if (empty($rule)) {
            $rule = $this->default;
        }

        return array($value, $rule);
    }

    /**
     * @return array
     */
    public function getCodeList()
    {
        return array_keys($this->rules);
    }

    /**
     * @param string $value
     * @return float
     */
    public function clean($value)
    {
        $value = trim($value);

        $value = preg_replace("#[^0-9-+eE,.]#", '', $value);
        if (!preg_match('#\d[eE][-+]\d#', $value)) { // remove exponential format
            $value = str_replace(array('e', 'E'), '', $value);
        }

        $value = str_replace(',', '.', $value);
        $value = (float)$value;
        $value = round($value, Formatter::ROUND_DEFAULT);

        return $value;
    }

    /**
     * @param string $rule
     * @return string
     * @throws Exception
     */
    public function checkRule($rule)
    {
        $rule = strtolower(trim($rule));

        if (empty($rule)) {
            return $this->default;
        }

        if (isset($this->rules[$rule])) {
            return $rule;
        }

        throw new Exception('Undefined rule: ' . $rule);
    }

    /**
     * @param string $newRule
     */
    public function addRule($newRule)
    {
        $this->rules[$newRule] = $newRule;
    }

    /**
     * @param string $rule
     * @return bool
     */
    public function removeRule($rule)
    {
        if (isset($this->rules[$rule])) {
            unset($this->rules[$rule]);
            return true;
        }

        return false;
    }
}
{'fraction_non_alphanumeric': 0.17428224828143954, 'fraction_numerical': 0.005795929370535112, 'mean_word_length': 1.6452762923351159, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 53, 'https://': 0, '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': '6223410', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2294, 'n_words': 461}