Completed
Push — master ( 9458ed...7d322b )
by Henry
10:04
created

Alias::getFormPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript\Validator;
3
4
use function in_array;
5
use function preg_match;
6
7
/**
8
 * children class to validate general and default alias
9
 *
10
 * @since 2.2.0
11
 *
12
 * @package Redaxscript
13
 * @category Validator
14
 * @author Henry Ruhs
15
 * @author Sven Weingartner
16
 */
17
18
class Alias implements ValidatorInterface
19
{
20
	/**
21
	 * pattern for alias
22
	 *
23
	 * @var string
24
	 */
25
26
	protected $_pattern = '^[a-zA-Z0-9-]{3,100}$';
27
28
	/**
29
	 * array of system alias
30
	 *
31
	 * @var array
32
	 */
33
34
	protected $_systemArray =
35
	[
36
		'admin',
37
		'login',
38
		'recover',
39
		'reset',
40
		'logout',
41
		'register',
42
		'module',
43
		'search'
44
	];
45
46
	/**
47
	 * get the pattern
48
	 *
49
	 * @since 4.3.0
50
	 *
51
	 * @return string
52
	 */
53
54
	public function getPattern() : string
55
	{
56
		return $this->_pattern;
57
	}
58
59
	/**
60
	 * validate the alias
61
	 *
62
	 * @since 4.3.0
63
	 *
64
	 * @param string $alias alias to be validated
65
	 *
66
	 * @return bool
67 1
	 */
68
69 1
	public function validate(string $alias = null) : bool
70
	{
71
		return preg_match('/' . $this->_pattern . '/', $alias);
72
	}
73
74
	/**
75
	 * match system alias
76
	 *
77
	 * @since 4.3.0
78
	 *
79
	 * @param string $alias alias to be matched
80
	 *
81
	 * @return bool
82 7
	 */
83
84 7
	public function matchSystem(string $alias = null) : bool
85 7
	{
86
		return in_array($alias, $this->_systemArray);
87
	}
88
}
89