Completed
Push — master ( 587f5e...7e0186 )
by Jean-Christophe
04:40
created

ValidatorMultiple::_getMessageViolation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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 1
	public function __construct(){
16 1
		$this->message=[
17
				"notNull"=>"This value should not be null"
18
		];
19 1
		$this->notNull=false;
20 1
	}
21
22 1
	public function validate($value) {
23 1
		if(null==$value){
24 1
			if($this->notNull===true){
25 1
				$this->violation="notNull";
26 1
				return false;
27
			}else{
28 1
				return;
29
			}
30
		}
31
		if (!UString::isValid($value)) {
32
			throw new ValidatorException('This value can not be converted to string');
33
		}
34
		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