validator.php
Go to the documentation of this file.
1 <?php
7 namespace depage\htmlform\validators;
8 
14  class validator
15  {
19  protected $log;
20 
29  public function __construct($log = null)
30  {
31  $this->log = $log;
32  }
33 
44  public static function factory($argument, $log = null)
45  {
46  if (!is_string($argument) && is_callable($argument, false)) {
47  $closureValidator = new closure($log);
48  $closureValidator->setFunc($argument);
49 
50  return $closureValidator;
51  } elseif (($argument{0} === '/') && ($argument{strlen($argument)-1} ==='/')) {
52  $regExValidator = new regEx($log);
53  $regExValidator->setRegEx($argument);
54 
55  return $regExValidator;
56  } else {
57  $type = 'depage\\htmlform\\validators\\' . $argument;
58 
59  if (class_exists($type)) {
60  return new $type($log);
61  } else {
62  return new validator($log);
63  }
64  }
65  }
66 
76  public function validate($value, $parameters = array())
77  {
78  return true;
79  }
80 
88  protected function log($argument, $type)
89  {
90  if (is_callable(array($this->log, 'log'))) {
91  $this->log->log($argument, $type);
92  } else {
93  error_log($argument);
94  }
95  }
96 
102  public function getPatternAttribute()
103  {
104  if (isset($this->regEx)) {
105  return ' pattern="' . htmlspecialchars(substr($this->regEx, 1,-1), ENT_QUOTES) . '"';
106  }
107  }
108 
115  public static function isValid($input)
116  {
117  $el = \get_called_class();
118  $el = new $el();
119 
120  return $el->validate($input);
121  }
122 }
customizable validator for input elements
Definition: closure.php:11
customizable validator for input elements
Definition: regEx.php:11
getPatternAttribute()
returns validators&#39; regular expression as HTML5 pattern attribute
Definition: validator.php:102
__construct($log=null)
validator constructor
Definition: validator.php:29
validate($value, $parameters=array())
default validator.
Definition: validator.php:76
parent validator class
Definition: validator.php:14
static factory($argument, $log=null)
valdiator object factory
Definition: validator.php:44
log($argument, $type)
error logging method
Definition: validator.php:88