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

RegexValidator::validate()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 2.0185
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