<commit_before><?php

namespace Sectorr\Core\Input;

use Sectorr\Core\Database\Database;

class Validator
{

    protected $db;
    protected $errors = [];
    protected $failed = false;

    public function __construct()
    {
        $this->db = new Database();
    }

    /**
     * Loops through the rules and checks the current input.
     *
     * @param $input
     * @param $rules
     * @return bool
     */
    public function validate($input, $rules)
    {
        foreach ($rules as $field => $arguments) {
            $this->callValidator($input[$field], $field, $arguments);
        }
        return ! $this->failed;
    }

    /**
     * Check if the given input isn't empty.
     *
     * @param $field
     * @param $input
     */
    protected function required($field, $input)
    {
        if (empty($input)) {
            $this->failed = true;
            $this->errors[] = "{$field} is a required field";
        }
    }

    /**
     * Check if the given input is a valid email adress.
     *
     * @param $input
     */
    protected function email($input)
    {
        if (!filter_var($input, FILTER_VALIDATE_EMAIL)) {
            $this->failed = true;
        }
    }

    /**
     * Check if the given input is a valid password.
     *
     * @param $input
     */
    protected function password($input)
    {
        if (!(strlen($input) >= 4)) {
            $this->failed = true;
        }
    }

    /**
     * Check if the given input is unique in the given database table and column.
     *
     * @param $input
     * @param $table
     * @param $column
     */
    protected function unique($input, $table, $column)
    {
        $email = $this->db->get($table, $column, [
            $column => $input
        ]);
        if ($email) {
            $this->failed = true;
        }
    }

    /**
     * Retrieves multiple arguments from argument string.
     *
     * @param $arg
     * @return array|bool
     */
    protected function multiple($arg)
    {
        if (count(explode('|', $arg)) > 1) {
            $args = explode('|', $arg);
            if (count(explode(':', $args[1]))) {
                $a       = $args[0];
                $specs = explode(':', $args[1]);
                return [true, $a, $specs];
            }
        } else {
            return false;
        }
    }

    /**
     * Loops through all arguments and checks what methods need to be called.
     *
     * @param $input
     * @param $field
     * @param $arguments
     */
    protected function callValidator($input, $field, $arguments)
    {
        foreach ($arguments as $arg) {
            if ($this->multiple($arg)) {
                $argument = $this->multiple($arg)[1];
                switch ($argument) {
                    case 'unique':
                        $table = $this->multiple($arg)[2][0];
                        $column = $this->multiple($arg)[2][1];
                        $this->unique($input, $table, $column);
                        break;
                }
            } else {
                switch ($arg) {
                    case 'required':
                        $this->required($field, $input);
                        break;
                    case 'email':
                        $this->email($input);
                        break;
                    case 'password':
                        $this->password($input);
                        break;
                }
            }
        }
    }
<commit_msg>Fixed Validator<commit_after><?php

namespace Sectorr\Core\Input;

use Sectorr\Core\Database\Database;

class Validator
{

    protected $db;
    protected $errors = [];
    protected $failed = false;
    const RULE_SEPERATOR = "|";
    const ARGUMENT_SEPERATOR = ":";

    public function __construct()
    {
        $this->db = new Database();
    }

    /**
     * Loops through the rules and checks the current input.
     *
     * @param $input
     * @param $rules
     * @return bool
     */
    public function passes($input, $rules)
    {
        foreach ($rules as $field => $rule) {
            $this->callValidator($input[$field], $field, $rule);
        }
        return ! $this->failed;
    }

    /**
     * Check if the given input isn't empty.
     *
     * @param $field
     * @param $input
     */
    protected function required($field, $input)
    {
        if (empty($input)) {
            $this->failed = true;
            var_dump("error: {$field} is a required field");
            //$this->errors[] = "{$field} is a required field";
        }
    }

    /**
     * Check if the given input is a valid email adress.
     *
     * @param $input
     */
    protected function email($input)
    {
        if (!filter_var($input, FILTER_VALIDATE_EMAIL)) {
            var_dump("error: {$input} not a valid email");
            $this->failed = true;
        }
    }

    /**
     * Check if the given input is a valid password.
     *
     * @param $input
     */
    protected function password($input)
    {
        if (!(strlen($input) >= 4)) {
            var_dump("error: {$input} not a valid password");
            $this->failed = true;
        }
    }

    /**
     * Check if the given input is unique in the given database table and column.
     *
     * @param $input
     * @param $table
     * @param $column
     */
    protected function unique($input, $table, $column)
    {
        $email = $this->db->get($table, $column, [
            $column => $input
        ]);
        if ($email) {
            var_dump("error: {$column} not unique");
            $this->failed = true;
        }
    }

    /**
     * Retrieves multiple arguments from argument string.
     *
     * @param $arg
     * @return array|bool
     */
    protected function multiple($value)
    {
        $arguments = explode(':', $value);

        if(count($arguments) > 1) {
            $rule = $arguments[0];
            unset($arguments[0]);

            return ['multiple' => true, 'rule' => $rule, 'arguments' => array_values($arguments)];
        }

        return ['multiple' => false];
    }

    /**
     * Loops through all arguments and checks what methods need to be called.
     *
     * @param $input
     * @param $field
     * @param $arguments
     */
    protected function callValidator($input, $field, $arguments)
    {
        $arguments = explode('|', $arguments);

        foreach ($arguments as $arg) {
            if ($this->multiple($arg)['multiple']) {
                switch ($this->multiple($arg)['rule']) {
                    case 'unique':
                        $table = $this->multiple($arg)['arguments'][0];
                        $column = $this->multiple($arg)['arguments'][1];
                        $this->unique($input, $table, $column);
                        break;
                }
            } else {
                switch ($arg) {
                    case 'required':
                        $this->required($field, $input);
                        break;
                    case 'email':
                        $this->email($input);
                        break;
                    case 'password':
                        $this->password($input);
                        break;
                }
            }
        }
    }

{'fraction_non_alphanumeric': 0.13873229753884228, 'fraction_numerical': 0.002199917503093634, 'mean_word_length': 1.4118037135278514, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 51, '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': '5914852', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1963, 'n_words': 506}