|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\contents\validation\validators\multiples; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\contents\validation\validators\Validator; |
|
6
|
|
|
use Ubiquity\utils\base\UString; |
|
7
|
|
|
use Ubiquity\exceptions\ValidatorException; |
|
8
|
|
|
use Ubiquity\contents\validation\validators\HasNotNullInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class ValidatorMultiple extends Validator implements HasNotNullInterface{ |
|
11
|
|
|
|
|
12
|
|
|
protected $violation; |
|
13
|
|
|
protected $notNull; |
|
14
|
|
|
|
|
15
|
3 |
|
public function __construct(){ |
|
16
|
3 |
|
$this->message=[ |
|
17
|
|
|
"notNull"=>"This value should not be null" |
|
18
|
|
|
]; |
|
19
|
3 |
|
$this->notNull=false; |
|
20
|
3 |
|
} |
|
21
|
|
|
|
|
22
|
3 |
|
public function validate($value) { |
|
23
|
3 |
|
if(null==$value){ |
|
24
|
2 |
|
if($this->notNull===true){ |
|
25
|
1 |
|
$this->violation="notNull"; |
|
26
|
1 |
|
return false; |
|
27
|
|
|
}else{ |
|
28
|
2 |
|
return; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
2 |
|
if (!UString::isValid($value)) { |
|
32
|
|
|
throw new ValidatorException('This value can not be converted to string'); |
|
33
|
|
|
} |
|
34
|
2 |
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array|string |
|
39
|
|
|
*/ |
|
40
|
1 |
|
protected function mergeMessages(){ |
|
41
|
1 |
|
if(!isset($this->modifiedMessage)){ |
|
42
|
1 |
|
return $this->message; |
|
43
|
|
|
}else{ |
|
44
|
|
|
if(is_array($this->modifiedMessage) && is_array($this->message)){ |
|
45
|
|
|
return array_merge($this->message,$this->modifiedMessage); |
|
46
|
|
|
}else{ |
|
47
|
|
|
return $this->modifiedMessage; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
protected function _getMessage(){ |
|
53
|
1 |
|
$parameters=$this->getParameters(); |
|
54
|
1 |
|
$message=$this->mergeMessages(); |
|
55
|
1 |
|
if(isset($this->violation) && is_array($message)){ |
|
56
|
1 |
|
$message=$this->_getMessageViolation($message); |
|
57
|
|
|
} |
|
58
|
1 |
|
foreach ($parameters as $param){ |
|
59
|
1 |
|
$message=str_replace("{".$param."}", $this->$param, $message); |
|
60
|
|
|
} |
|
61
|
1 |
|
return $message; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
protected function _getMessageViolation($messages){ |
|
65
|
1 |
|
if(isset($messages[$this->violation])){ |
|
66
|
1 |
|
return $messages[$this->violation]; |
|
67
|
|
|
} |
|
68
|
|
|
return current($messages); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|