depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Validator.php
Go to the documentation of this file.
1<?php
2
7
8namespace Depage\HtmlForm\Validators;
9
16{
20 protected $log;
21
30 public function __construct($log = null)
31 {
32 $this->log = $log;
33 }
34
45 public static function factory($argument, $log = null)
46 {
47 if (!is_string($argument) && is_callable($argument, false)) {
48 $closureValidator = new Closure($log);
49 $closureValidator->setFunc($argument);
50
51 return $closureValidator;
52 } elseif (($argument[0] === '/') && ($argument[strlen($argument) - 1] === '/')) {
53 $regExValidator = new RegEx($log);
54 $regExValidator->setRegEx($argument);
55
56 return $regExValidator;
57 } else {
58 $type = 'Depage\\HtmlForm\\Validators\\' . $argument;
59
60 if (class_exists($type)) {
61 return new $type($log);
62 } else {
63 return new Validator($log);
64 }
65 }
66 }
67
77 public function validate($value, $parameters = [])
78 {
79 return true;
80 }
81
89 protected function log($argument, $type)
90 {
91 if (is_callable([$this->log, 'log'])) {
92 $this->log->log($argument, $type);
93 } else {
94 error_log($argument);
95 }
96 }
97
103 public function getPatternAttribute()
104 {
105 if (isset($this->regEx)) {
106 return ' pattern="' . htmlspecialchars(substr($this->regEx, 1, -1), ENT_QUOTES) . '"';
107 }
108 }
109
116 public static function isValid($input)
117 {
118 $el = \get_called_class();
119 $el = new $el();
120
121 return $el->validate($input);
122 }
123}
customizable validator for input elements
Definition Closure.php:14
customizable validator for input elements
Definition RegEx.php:14
static isValid($input)
Returns a bool value indicating whether or not the input passes the given element's validation criter...
getPatternAttribute()
returns validators' regular expression as HTML5 pattern attribute
validate($value, $parameters=[])
default validator.
Definition Validator.php:77
__construct($log=null)
validator constructor
Definition Validator.php:30
static factory($argument, $log=null)
valdiator object factory
Definition Validator.php:45
log($argument, $type)
error logging method
Definition Validator.php:89