1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dameter\abstracts\validators; |
4
|
|
|
|
5
|
|
|
use yii\validators\StringValidator; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class VariableNameValidator |
10
|
|
|
* Validates the variable name based mainly on SPSS variable name limitations |
11
|
|
|
* @link https://www.ibm.com/support/knowledgecenter/en/SSLVMB_23.0.0/spss/base/syn_variables_variable_names.html |
12
|
|
|
* @author Tõnis Ormisson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class VariableNameValidator extends StringValidator |
15
|
|
|
{ |
16
|
|
|
public $max = 64; |
17
|
|
|
|
18
|
|
|
/** @var string $containsSpacesMsg A message if the value contains spaces */ |
19
|
|
|
public $containsSpacesMsg; |
20
|
|
|
|
21
|
|
|
/** @var string $invalidFirstLetterMsg A message if invalid first letter */ |
22
|
|
|
public $invalidFirstLetterMsg; |
23
|
|
|
|
24
|
|
|
/** @var string $containsInvalidCharsMsg A message if contains invalid characters */ |
25
|
|
|
public $containsInvalidCharsMsg; |
26
|
|
|
|
27
|
|
|
const ALLOWED_NON_ALPHA_CHARACTERS = ['.', '-', '_']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function init() |
33
|
|
|
{ |
34
|
|
|
parent::init(); |
35
|
|
|
$this->containsSpacesMsg = \Yii::t('dmabstract', "{attribute} must not contain spaces!"); |
36
|
|
|
$this->invalidFirstLetterMsg = \Yii::t('dmabstract', "The first character of {attribute} must be a letter!"); |
37
|
|
|
$this->containsInvalidCharsMsg = \Yii::t('dmabstract', "{attribute} contains invalid characters!"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function validateAttribute($model, $attribute) |
45
|
|
|
{ |
46
|
|
|
parent::validateAttribute($model, $attribute); |
47
|
|
|
|
48
|
|
|
$value = $model->{$attribute}; |
49
|
|
|
if (!is_string($value)) { |
50
|
|
|
$this->addError($model, $attribute, $this->message); |
51
|
|
|
} else { |
52
|
|
|
if (strpos($model->{$attribute}, ' ') !== false) { |
53
|
|
|
$this->addError($model, $attribute, $this->containsSpacesMsg); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!ctype_alpha($model->{$attribute}[0])) { |
57
|
|
|
$this->addError($model, $attribute, $this->invalidFirstLetterMsg); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($this->containsInvalidCharacters($model->{$attribute})) { |
61
|
|
|
$this->addError($model, $attribute, $this->containsInvalidCharsMsg); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function validateValue($value) |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
$validation = parent::validateValue($value); |
74
|
|
|
|
75
|
|
|
if (!is_null($validation)) { |
76
|
|
|
return $validation; |
77
|
|
|
} |
78
|
|
|
if (strpos($value, ' ') !== false) { |
79
|
|
|
return [$this->containsSpacesMsg, []]; |
80
|
|
|
} |
81
|
|
|
if (!ctype_alpha($value[0])) { |
82
|
|
|
return [$this->invalidFirstLetterMsg, []]; |
83
|
|
|
} |
84
|
|
|
if ($this->containsInvalidCharacters($value)) { |
85
|
|
|
return [$this->containsInvalidCharsMsg, []]; |
86
|
|
|
} |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $value |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
private function containsInvalidCharacters($value) |
95
|
|
|
{ |
96
|
|
|
if (!ctype_alnum($value)) { |
97
|
|
|
foreach (str_split($value) as $char) { |
98
|
|
|
if (!ctype_alnum($char) && !in_array($char, self::ALLOWED_NON_ALPHA_CHARACTERS)) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
// allowed chars only |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
} |