Passed
Push — master ( 0568ea...74fbac )
by Jean-Christophe
05:35
created

RegexValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 25
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 7 2
A getParameters() 0 2 1
1
<?php
2
3
namespace Ubiquity\contents\validation\validators\strings;
4
5
use Ubiquity\contents\validation\validators\ValidatorHasNotNull;
6
7
/**
8
 * Validates a string with a regex
9
 * Usage @validator("regex",pattern)
10
 *
11
 * @author jcheron <[email protected]>
12
 * @version 1.0.0
13
 */
14
class RegexValidator extends ValidatorHasNotNull {
15
	protected $ref;
16
	protected $match;
17
18 1
	public function __construct() {
19 1
		$this->message = "This value is not valid";
20 1
		$this->match = true;
21 1
	}
22
23 3
	public function validate($value) {
24 3
		parent::validate ( $value );
25 3
		if ($this->notNull !== false) {
26 3
			$value = ( string ) $value;
27 3
			return ! ($this->match xor preg_match ( $this->ref, $value ));
28
		}
29
		return true;
30
	}
31
32
	/**
33
	 *
34
	 * {@inheritdoc}
35
	 * @see \Ubiquity\contents\validation\validators\Validator::getParameters()
36
	 */
37 3
	public function getParameters(): array {
38 3
		return [ "value" ];
39
	}
40
}
41
42