Completed
Push — master ( b274fd...913334 )
by Jean-Christophe
01:35
created

RegexValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 28
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 10 6
A getParameters() 0 3 1
1
<?php
2
3
namespace Ubiquity\contents\validation\validators\strings;
4
5
6
use Ubiquity\contents\validation\validators\Validator;
7
use Ubiquity\exceptions\ValidatorException;
8
9
/**
10
 * Validates a string
11
 * with @validator("regex",pattern)
12
 * @author jc
13
 *
14
 */
15
class RegexValidator extends Validator {
16
	protected $ref;
17
	protected $match;
18
	
19
	public function __construct(){
20
		$this->message="This value is not valid";
21
		$this->match=true;
22
	}
23
	
24
	public function validate($value) {
25
		if (null === $value || '' === $value) {
26
			return;
27
		}
28
		if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
29
			throw new ValidatorException('This value can not be converted to string');
30
		}
31
		$value = (string) $value;
32
		return $this->match xor preg_match($this->ref, $value);
33
	}
34
	
35
	/**
36
	 * {@inheritDoc}
37
	 * @see \Ubiquity\contents\validation\validators\Validator::getParameters()
38
	 */
39
	public function getParameters(): array {
40
		return ["value"];
41
	}
42
}
43
44