StringSetting.php
1.45 KB
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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
use YahnisElsts\AdminMenuEditor\Customizable\Storage\StorageInterface;
use YahnisElsts\AdminMenuEditor\Customizable\Validation\StringValidator;
class StringSetting extends Setting {
protected $dataType = 'string';
protected $minLength = 0;
protected $maxLength = null;
protected $validators = [];
public function __construct($id, StorageInterface $store = null, $params = array()) {
parent::__construct($id, $store, $params);
if ( array_key_exists('minlength', $params) ) {
$this->minLength = ($params['minlength'] === null) ? null : (int)$params['minlength'];
}
if ( array_key_exists('maxlength', $params) ) {
$this->maxLength = ($params['maxlength'] === null) ? null : (int)$params['maxlength'];
}
$this->validators[] = new StringValidator(
$this->minLength,
$this->maxLength,
false,
isset($params['regex']) ? $params['regex'] : null,
array_key_exists('trimmed', $params) && $params['trimmed']
);
}
public function validate($errors, $value, $stopOnFirstError = false) {
if ( $this->canTreatAsNull($value) ) {
return null;
}
$convertedValue = $value;
foreach ($this->validators as $validator) {
$result = call_user_func($validator, $convertedValue, $errors);
if ( is_wp_error($result) ) {
$errors = $result;
if ( $stopOnFirstError ) {
return $errors;
}
} else {
$convertedValue = $result;
}
}
return $convertedValue;
}
}