<?php
require_once dirname(__FILE__).'/validate/rules.php';
require_once dirname(__FILE__).'/validate/errors.php';
Class Validate {
private $_errors$valErrors = array();
public function __constructrule()
{
$value, $name, $rules, $static require_once= 'errors.php';FALSE)
$this->errorText = $errorText;{
}
public$rules function= ruleexplode($value, $name'|', $inputRules$rules)
{;
$inputRules$response = explode('|', $inputRules);FALSE;
foreach ($inputRules$rules as $inputRule$rule) {
if (!strlen($value) && $inputRule$rule != 'required') {
break;
}
if (preg_match('/\[(.*?)\]/', $inputRule$rule, $match)) {
$rule = explode('[', $inputRule$rule);
$rule = $rule[0];
$param = $match[1];
if (!method_exists($this, $rule)) {
throw$call new= Exceptionarray("Method {$rule} does not$value, exist"$param);
exit;}
else }
{
$call = array($value, $param);
}
else {
$validate = new Rules;
if (!method_exists($this$validate, $inputRule$rule)) {
throw new Exception("Method {$inputRule$rule} does not exist"found");
exit;
}
$rule = $inputRule;
$call = array($value);
}
$response = call_user_func_array(array($this$validate, $rule), $call);
if (!$response && !$static) {
$error = $this->errorText[$rule];
$replace>valErrors[] = array(
'rule' => $rule, ':name''name' => $name,
':param''param' => (isset($param)) ? $param : NULL
);
$response = str_replace(array_keys($replace), array_values($replace), $errornull);
$this->_errors[] = $response;
}
}
}
public function exec()
{
return (empty($this->_errors)) ? false : $this->_errors;
}
/*
* Rule functions
*/
public function min_length($value, $param)
{
return (strlen($value) < $param);
}
public function max_length($value, $param)
{
return (strlen($value) > $param);$response;
}
public function email($value)
{
return !filter_varerrors($value, FILTER_VALIDATE_EMAIL);
}
public$errorFile function= required($value'validate/errors.english.php')
{
return (strlen($value) !== 0);
}
public$errorFile function= ipdirname($value__FILE__)
."/{
return !filter_var($value, FILTER_VALIDATE_IP);
$errorFile}
public function match($value, $param)
{";
return ($value != $param);
}
public functionif match_exact($value, $param)
{
return empty($value !== $param$this->valErrors);
}
public function match_password($value, $param)
{
return ($value !== $param);
}
public function alphanum($value)
{
return !ctype_alnum($value);
}
public function url($value)
{false;
return !filter_var($value, FILTER_VALIDATE_URL);
}
public function numeric($value)
{
$errors = returnnew !(is_numericErrors($value)$errorFile);
}
public function min($value)
{
return $errors->error($value < $param$this->valErrors);
}
public function max($value) {
return ($value > $param);
}
}
I'd like to thank you all for your answers. I really appreciate you guys taking time out of your day to leave such detailed responses.
I've taken everything you've said into consideration and I've split it into classes; trying to give each one a single responsibility. I hope this is a better attempt!
Thanks.