@@ 16-62 (lines=47) @@ | ||
13 | * |
|
14 | * @package Particle\Validator\Rule |
|
15 | */ |
|
16 | class Alnum extends Regex |
|
17 | { |
|
18 | /** |
|
19 | * A constant that will be used for the error message when the value is not alphanumeric. |
|
20 | */ |
|
21 | const NOT_ALNUM = 'Alnum::NOT_ALNUM'; |
|
22 | ||
23 | /** |
|
24 | * A constant indicating spaces are allowed. |
|
25 | */ |
|
26 | const ALLOW_SPACES = true; |
|
27 | ||
28 | /** |
|
29 | * A constant indicated spaces are *not* allowed. |
|
30 | */ |
|
31 | const DISALLOW_SPACES = false; |
|
32 | ||
33 | /** |
|
34 | * The message templates which can be returned by this validator. |
|
35 | * |
|
36 | * @var array |
|
37 | */ |
|
38 | protected $messageTemplates = [ |
|
39 | self::NOT_ALNUM => '{{ name }} may only consist out of numeric and alphabetic characters' |
|
40 | ]; |
|
41 | ||
42 | /** |
|
43 | * Construct the validation rule. |
|
44 | * |
|
45 | * @param bool $allowSpaces |
|
46 | */ |
|
47 | public function __construct($allowSpaces = self::DISALLOW_SPACES) |
|
48 | { |
|
49 | parent::__construct($allowSpaces ? '~^[\p{L}0-9\s]*$~iu' : '~^[\p{L}0-9]*$~iu'); |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * Checks whether $value consists solely out of alphanumeric characters. |
|
54 | * |
|
55 | * @param mixed $value |
|
56 | * @return bool |
|
57 | */ |
|
58 | public function validate($value) |
|
59 | { |
|
60 | return $this->match($this->regex, $value, self::NOT_ALNUM); |
|
61 | } |
|
62 | } |
|
63 |
@@ 16-62 (lines=47) @@ | ||
13 | * |
|
14 | * @package Particle\Validator\Rule |
|
15 | */ |
|
16 | class Alpha extends Regex |
|
17 | { |
|
18 | /** |
|
19 | * A constant that will be used for the error message when the value contains non-alphabetic characters. |
|
20 | */ |
|
21 | const NOT_ALPHA = 'Alpha::NOT_ALPHA'; |
|
22 | ||
23 | /** |
|
24 | * A constant indicated spaces are allowed. |
|
25 | */ |
|
26 | const ALLOW_SPACES = true; |
|
27 | ||
28 | /** |
|
29 | * A constant indicating spaces are *not* allowed. |
|
30 | */ |
|
31 | const DISALLOW_SPACES = false; |
|
32 | ||
33 | /** |
|
34 | * The message templates which can be returned by this validator. |
|
35 | * |
|
36 | * @var array |
|
37 | */ |
|
38 | protected $messageTemplates = [ |
|
39 | self::NOT_ALPHA => '{{ name }} may only consist out of alphabetic characters' |
|
40 | ]; |
|
41 | ||
42 | /** |
|
43 | * Construct the Alpha rule. |
|
44 | * |
|
45 | * @param bool $allowWhitespace |
|
46 | */ |
|
47 | public function __construct($allowWhitespace = self::DISALLOW_SPACES) |
|
48 | { |
|
49 | parent::__construct($allowWhitespace ? '~^[\p{L}\s]*$~iu' : '~^[\p{L}]*$~ui'); |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * Checks whether $value consists solely out of alphabetic characters. |
|
54 | * |
|
55 | * @param mixed $value |
|
56 | * @return bool |
|
57 | */ |
|
58 | public function validate($value) |
|
59 | { |
|
60 | return $this->match($this->regex, $value, self::NOT_ALPHA); |
|
61 | } |
|
62 | } |
|
63 |