-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaBasicDataValidator.php
72 lines (64 loc) · 2 KB
/
aBasicDataValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace phpWTL;
use phpWTL\aSingleton;
require_once 'aSingleton.php';
/**
* Abstract class for a content (logger field) validator.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.0
* @api
*/
abstract class aBasicDataValidator extends aSingleton {
protected static $loggerContent= null;
protected static $fieldDescriptor= null;
/**
* @param object $loggerContent Provide a LoggerContent object. Also store the format blueprint via the LoggerContent object (which knows its FormatDescriptor).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.1
*/
protected function __construct($loggerContent= null) {
static::$loggerContent= $loggerContent;
if (static::$loggerContent) static::$fieldDescriptor= $loggerContent->getFormatDescriptor();
}
/**
* Validate all log fields.
*
* @return array Validation errors (contains field names which did not validate), null if none
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.2
* @api
*/
public function validate() {
$err= null;
if (static::$fieldDescriptor && static::$loggerContent) {
foreach (static::$fieldDescriptor->getFieldNames() as $k=>$f) {
if (!static::isValid($f)) array_push($err, $f);
}
}
return $err;
}
/**
* Validate data for a single log field.
*
* @param string $field_name ID of log format field.
* @param string $value Provide an (optional) value to validate, so allowing for the validation of external data (by default validation will be performed on the LoggerContent object).
* @return boolean Field valid?.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public function isValid($field_name, $value= null) {
$ret= true;
if (static::$fieldDescriptor && static::$loggerContent) {
$validator= static::$fieldDescriptor->getValidator($field_name);
if (!$value) $value= static::$loggerContent->__get($field_name);
if ($validator) $ret= preg_match($validator, $value);
}
return $ret;
}
}
?>