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

ValidatorMultiple   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 77.42%

Importance

Changes 0
Metric Value
wmc 15
eloc 32
dl 0
loc 59
ccs 24
cts 31
cp 0.7742
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 4
A _getMessage() 0 10 4
A _getMessageViolation() 0 5 2
A mergeMessages() 0 8 4
A __construct() 0 5 1
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