1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\contents\validation\validators; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Abstract class for validators |
8
|
|
|
* @author jcheron <[email protected]> |
9
|
|
|
* @version 1.0.0 |
10
|
|
|
*/ |
11
|
|
|
abstract class Validator implements ValidatorInterface{ |
12
|
|
|
protected $modifiedMessage; |
13
|
|
|
protected $message; |
14
|
|
|
protected $member; |
15
|
|
|
protected $value; |
16
|
|
|
protected $severity; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param mixed $value |
21
|
|
|
* @return \Ubiquity\contents\validation\validators\ConstraintViolation|boolean |
22
|
|
|
*/ |
23
|
4 |
|
public function validate_($value){ |
24
|
4 |
|
$this->value=$value; |
25
|
4 |
|
if(!$this->validate($value)){ |
26
|
3 |
|
return new ConstraintViolation($this->_getMessage(), $value, $this->member, get_class($this),$this->severity); |
27
|
|
|
} |
28
|
4 |
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function setValidationParameters($member,$params,$severity='warning',$message=null){ |
32
|
4 |
|
$this->setParams($params); |
33
|
4 |
|
$this->member=$member; |
34
|
4 |
|
$this->modifiedMessage=$message; |
35
|
4 |
|
$this->severity=$severity; |
36
|
4 |
|
} |
37
|
|
|
|
38
|
4 |
|
protected function setParams(array $params){ |
39
|
4 |
|
foreach ($params as $member=>$value){ |
40
|
4 |
|
$this->$member=$value; |
41
|
|
|
} |
42
|
4 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
|
|
public function getMember() { |
48
|
|
|
return $this->member; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $member |
53
|
|
|
*/ |
54
|
|
|
public function setMember($member) { |
55
|
|
|
$this->member = $member; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
* @see \Ubiquity\contents\validation\validators\ValidatorInterface::getParameters() |
61
|
|
|
*/ |
62
|
1 |
|
public function getParameters(): array { |
63
|
1 |
|
return []; |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array|string |
69
|
|
|
*/ |
70
|
3 |
|
protected function mergeMessages(){ |
71
|
3 |
|
if(!isset($this->modifiedMessage)){ |
72
|
3 |
|
return $this->message; |
73
|
|
|
}else{ |
74
|
|
|
return $this->modifiedMessage; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
protected function _getMessage(){ |
79
|
3 |
|
$parameters=$this->getParameters(); |
80
|
3 |
|
$message=$this->mergeMessages(); |
81
|
3 |
|
foreach ($parameters as $param){ |
82
|
3 |
|
$message=str_replace("{".$param."}", $this->$param, $message); |
83
|
|
|
} |
84
|
3 |
|
return $message; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|