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

RegexValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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