forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphaCompleteValidator.php
85 lines (67 loc) · 2.65 KB
/
AlphaCompleteValidator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace Phalcon\Validation\Validator;
use Phalcon\Validation;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
class AlphaCompleteValidator extends Validator implements ValidatorInterface
{
/**
* Executes the validation. Allowed options:
* 'min' : input value must not be shorter than it;
* 'max' : input value must not be longer than it.
*
* @param Validation $validator
* @param string $attribute
*
* @return boolean
*/
public function validate(\Phalcon\Validation $validator, $attribute)
{
$value = $validator->getValue($attribute);
$allowPipes = (bool)$this->getOption('allowPipes');
$allowPipes = $allowPipes ? '|' : '';
$allowBlackSlashes = (bool)$this->getOption('allowBackslashes');
$allowBlackSlashes = $allowBlackSlashes ? '\\\\' : '';
$allowUrlChars = (bool)$this->getOption('allowUrlChars');
$allowUrlChars = $allowUrlChars ? '=#' : '';
if (!preg_match('/^([-\p{L}*0-9_+!.,:\/;' . $allowPipes . $allowBlackSlashes . $allowUrlChars
. '?&\(\)\[\]\{\}\'\"\s])+$/u', $value)) {
$message = 'The value can contain only alphanumeric, underscore, white spaces, slashes, apostrophes, '
. 'brackets, punctuation characters';
if ($allowPipes) {
$message .= ', pipes';
}
if ($allowBlackSlashes) {
$message .= ', backslashes';
}
if ($allowUrlChars) {
$message .= ', equals and hashtags';
}
$message = $this->getOption('message', $message);
$validator->appendMessage(new Message($message, $attribute, 'AlphaComplete'));
}
if ($min = (int)$this->getOption('min')) {
if (strlen($value) < $min) {
$messageMin = $this->getOption(
'messageMinimum',
'The value must contain at least ' . $min . ' characters.'
);
$validator->appendMessage(new Message($messageMin, $attribute, 'AlphaComplete'));
}
}
if ($max = (int)$this->getOption('max')) {
if (strlen($value) > $max) {
$messageMax = $this->getOption(
'messageMaximum',
'The value can contain maximum ' . $max . ' characters.'
);
$validator->appendMessage(new Message($messageMax, $attribute, 'AlphaComplete'));
}
}
if (count($validator)) {
return false;
}
return true;
}
}