RegExValidator::check()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Regular expression validator
4
 *
5
 * @file      RegExValidator.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Втр Авг 14 23:50:04 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Validators;
17
18
/**
19
 * Class RegExValidator
20
 * @author  Alexander Yancharuk <alex at itvault dot info>
21
 */
22
class RegExValidator implements ValidatorInterface
23
{
24
	protected $pattern;
25
26
	/**
27
	 * Constructor
28
	 * @param string $pattern Шаблон для валидации
29
	 */
30 3
	public function __construct($pattern)
31
	{
32 3
		$this->pattern = $pattern;
33
	}
34
35
	/**
36
	 * Validation
37
	 *
38
	 * @param mixed $value Value for checking
39
	 *
40
	 * @return bool
41
	 */
42 3
	public function check($value)
43
	{
44 3
		return (bool) preg_match($this->pattern, $value);
45
	}
46
47
	/**
48
	 * Validation (static version)
49
	 *
50
	 * @param string $pattern Pattern for validation
51
	 * @param mixed  $value   Value
52
	 *
53
	 * @return bool
54
	 */
55 3
	public static function validate($pattern, $value)
56
	{
57 3
		return (bool) preg_match($pattern, $value);
58
	}
59
}
60