Validator Readability and Efficiency This is my first validator I have created myself and I was wondering if there is anything about it that I could improve as far as:


Code readability
Code efficiency


Note: The reason why I put an underscore before each validation method name is so that non-validation methods cannot be called. E.g. setRule() can't be called if it's specified in the rule unless there is an actual validation method called "_setRule".

Any other suggestions are also welcome!

Validator class:

class Validator
{
    private $rules  = [];
    private $errors = [];

    public function setRule($id, Array $methods)
    {
        if (!array_key_exists($id, $this-&gt;rules)) {
            $this-&gt;rules[$id] = $methods;
        } else {
            throw new Exception('Rule "' . $id . '" already exists.');
        }
    }

    public function setRules(Array $rules) {
        foreach ($rules as $key =&gt; $value) {
            $this-&gt;setRule($key, $value);
        }
    }

    public function validate($rule, $input)
    {
        foreach ($this-&gt;rules[$rule] as $key =&gt; $value) {
            if (is_int($key)) { //$key =&gt; rule
                $value     = '_' . $value;
                $method    = $value;
                $arguments = [$input];
            } else {            //rule =&gt; [args]
                $key       = '_' . $key;
                $method    = $key;
                $arguments = array_merge([$input], is_array($value) ? $value : [$value]);
            }

            return call_user_func_array([$this, $method], $arguments);
        }
    }

    public function setError($error)
    {
        return $this-&gt;errors[] = $error;
    }

    public function getErrors()
    {
        return $this-&gt;errors;
    }

    private function _required($input)
    {
        return empty($input) ?: $this-&gt;errors[] = substr(__FUNCTION__, 1);
    }

    private function _min($input, $min)
    {
        return mb_strlen($input, 'utf8') &gt;= $min ?: $this-&gt;errors[] = substr(__FUNCTION__, 1);
    }

    private function _max($input, $max)
    {
        return mb_strlen($input, 'utf8') &lt;= $max ?: $this-&gt;errors[] = substr(__FUNCTION__, 1);
    }

    private function _between($input, $min, $max)
    {
        if ($return = $this-&gt;_min($input, $min) !== true) {
            return $this-&gt;errors[] = $return;
        }

        if ($return = $this-&gt;_max($input, $max) !== true) {
            return $this-&gt;errors[] = $return;
        }

        return true;
    }

    private function _email($input)
    {
        $sanitizedInput = filter_var($input, FILTER_SANITIZE_EMAIL);
        $vInput         = filter_var($sanitizedInput, FILTER_VALIDATE_EMAIL);

        return $input === $sanitizedInput &amp;&amp; $vInput ?: $this-&gt;errors[] = substr(__FUNCTION__, 1);
    }

    private function _alnum($input)
    {
        return ctype_alnum($input) ?: $this-&gt;errors[] = substr(__FUNCTION__, 1);
    }
}


Sample usage:

$v = new Validator();

$v-&gt;setRule('email', ['required', 'between' =&gt; [5, 255], 'email']);
$v-&gt;setRule('password', ['required']);

$vEmail        = $v-&gt;validate('email', $_POST['email']);
$vEmailConfirm = $v-&gt;validate('email', $_POST['emailConfirm']);
$vPassword     = $v-&gt;validate('password', $_POST['password']);

if ($vEmailConfirm === true &amp;&amp; $_POST['email'] !== $_POST['emailConfirm']) {
    $vEmailConfirm = $v-&gt;setError('match');
}

if (!$v-&gt;getErrors()) {
    # Input(s) have passed validation
} else {
   # Input(s) have not passed validation
   # $vEmail, $vEmailConfirm and $vPassword contain the errors
   # You can do something with that, like displaying an error message
}
Although you used a Validator class, this is not really object orientated. Actually I'm missing a Rule class. Your rules are applied by comparing strings right now, which is error prone and really ugly if you need to refactor it. Extending your code with new rules also requires changing the Validator itself.

class Rule {
     /**
      * @return bool
      */
     abstract function check($value);
     /**
      * @return string
      */
     abstract function getErrorMessage($value)
}

class BetweenRule extends Rule{ //Maybe there is also an IntegerRule to collect some operations.
     private $min;
     private $max;

     public function __construct ($min,$max) {...}

     public check($value)
     {
          return this-&gt;min&lt;=$value &amp;&amp; $this-&gt;max&gt;=$value;
     } 

     public function getErrorMessage($value) { return $valus." is not between ..." ;}
}
...


And the usage:

$v = new Validator();
$r=new RequiredRule();

$v-&gt;setRule('email', [$r, new BetweenRule(5,255), new EmailRule()]);
$v-&gt;setRule('password', [$r]);

if (!$v-&gt;getErrors()) {  //either as a list of string or list of map fields and rules
     ...
} else {
     ...
}


As you can't type hint the content of an array, you might also want to replace setRule by addRule and append new rules to the existing ones, instead setting the array of rules.

$v = new Validator();
$v-&gt;addRule('email', new RequiredRule());
$v-&gt;addRule('email', new BetweenRule(5,255));
$v-&gt;addRule('email', new EmailRule());