Completed
Push — master ( 4d725e...782061 )
by Jean-Christophe
01:38
created

Validator::getMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ubiquity\contents\validation\validators;
4
5
use Ubiquity\utils\base\UString;
6
use Ubiquity\exceptions\ValidatorException;
7
8
abstract class Validator implements ValidatorInterface{
9
	protected $modifiedMessage;
10
	protected $message;
11
	protected $member;
12
	protected $value;
13
	protected $notNull;
14
	
15
	/**
16
	 * @param mixed $value
17
	 * @param string $member
18
	 * @param array $params
19
	 * @param string $severity
20
	 * @param string $message
21
	 * @return \Ubiquity\contents\validation\validators\ConstraintViolation|boolean
22
	 */
23
	public function validate_($value,$member,$params,$severity='warning',$message=null){
24
		$this->value=$value;
25
		$this->setParams($params);
26
		if(!$this->validate($value)){
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->validate($value) of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
27
			$this->modifiedMessage=$message;
28
			return new ConstraintViolation($this->_getMessage(), $value, $member, get_class($this),$severity);
29
		}
30
		return true;
31
	}
32
	
33
	protected function setParams(array $params){
34
		foreach ($params as $member=>$value){
35
			$this->$member=$value;
36
		}
37
	}
38
	
39
	/**
40
	 * @return mixed
41
	 */
42
	public function getMember() {
43
		return $this->member;
44
	}
45
46
	/**
47
	 * @param mixed $member
48
	 */
49
	public function setMember($member) {
50
		$this->member = $member;
51
	}
52
	
53
	/**
54
	 * {@inheritDoc}
55
	 * @see \Ubiquity\contents\validation\validators\ValidatorInterface::getParameters()
56
	 */
57
	public function getParameters(): array {
58
		return [];
59
		
60
	}
61
	
62
	public function validate($value) {
63 View Code Duplication
		if ($this->notNull!==false && (null === $value || '' === $value)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
			return;
65
		}
66
		if (!UString::isValid($value)) {
67
			throw new ValidatorException('This value can not be converted to string');
68
		}
69
	}
70
	
71
	/**
72
	 * @return array|string
73
	 */
74
	protected function mergeMessages(){
75
		if(!isset($this->modifiedMessage)){
76
			return $this->message;
77
		}else{
78
			return $this->modifiedMessage;
79
		}
80
	}
81
	
82
	protected function _getMessage(){
83
		$parameters=$this->getParameters();
84
		$message=$this->mergeMessages();
85 View Code Duplication
		foreach ($parameters as $param){
86
			$message=str_replace("{".$param."}", $this->$param, $message);
87
		}
88
		return $message;
89
	}
90
91
}
92
93