Passed
Branch v1.4.0 (4a0ecd)
by Wanderson
01:13
created

Validator::hasError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Validation;
4
5
class Validator
6
{
7
	private $data = [];
8
	private $errors = [];
9
10
	const INDEX_NAME = 0;
11
	const INDEX_RULES = 1;
12
	const INDEX_MESSAGES = 'messages';
13
14
	/**
15
	 * Cria um validador
16
	 * @param mixed[] $data
17
	 * @return static
18
	 */
19
	public static function create($data)
20
	{
21
		return new static($data);
22
	}
23
24
	/* Cria um validador
25
	 * @param mixed[] $data
26
	 * @return static
27
	 */
28
	public function __construct($data)
29
	{
30
		$this->data = $data;
31
	}
32
33
	/**
34
	 * Retorna nome da validação
35
	 * @param string[] $validation
36
	 * @return string
37
	 */
38
	protected function getName($validation)
39
	{
40
		return $validation[self::INDEX_NAME];
41
	}
42
43
	/**
44
	 * Retorna as regras da validação
45
	 * @param string[] $validation
46
	 * @return string[]
47
	 */
48
	protected function getRules($validation)
49
	{
50
		if (key_exists(self::INDEX_RULES, $validation)) {
51
			return explode('|', $validation[self::INDEX_RULES]);
52
		}
53
54
		return [];
55
	}
56
57
	/**
58
	 * Retorna a mensagem personalizada desta validação
59
	 * @param string[] $validation
60
	 * @param string $rule
61
	 * @return string|null
62
	 */
63
	protected function getMessage($rule, $validation)
64
	{
65
		if (key_exists(self::INDEX_MESSAGES, $validation)) {
66
			if (key_exists($rule, $validation[self::INDEX_MESSAGES])) {
0 ignored issues
show
Bug introduced by
$validation[self::INDEX_MESSAGES] of type string is incompatible with the type array expected by parameter $search of key_exists(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
			if (key_exists($rule, /** @scrutinizer ignore-type */ $validation[self::INDEX_MESSAGES])) {
Loading history...
67
				return $validation[self::INDEX_MESSAGES][$rule];
68
			}
69
		}
70
71
		return null;
72
	}
73
74
	/**
75
	 * Valida e retorna os dados válidos
76
	 * @param string[] $validations
77
	 * @return mixed[]
78
	 */
79
	public function validate($validations)
80
	{
81
		foreach ($validations as $index => $validation) {
82
			$name = $this->getName($validation);
0 ignored issues
show
Bug introduced by
$validation of type string is incompatible with the type string[] expected by parameter $validation of Win\Validation\Validator::getName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
			$name = $this->getName(/** @scrutinizer ignore-type */ $validation);
Loading history...
83
			$rules = $this->getRules($validation);
0 ignored issues
show
Bug introduced by
$validation of type string is incompatible with the type string[] expected by parameter $validation of Win\Validation\Validator::getRules(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
			$rules = $this->getRules(/** @scrutinizer ignore-type */ $validation);
Loading history...
84
85
			foreach ($rules as $rule) {
86
				$message = $this->getMessage($rule, $validation);
0 ignored issues
show
Bug introduced by
$validation of type string is incompatible with the type string[] expected by parameter $validation of Win\Validation\Validator::getMessage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
				$message = $this->getMessage($rule, /** @scrutinizer ignore-type */ $validation);
Loading history...
87
				if (!$this->isFieldValid($index, $rule, $name, $message)) {
88
					break;
89
				}
90
			}
91
		}
92
93
		return $this->data;
94
	}
95
96
	/**
97
	 * Retorna TRUE se o campo é valido
98
	 * @param string $index
99
	 * @param string $rule
100
	 * @param string $name
101
	 * @param string|null $message
102
	 * @return bool
103
	 */
104
	private function isFieldValid($index, $rule, $name, &$message = null)
105
	{
106
		if (key_exists($index, $this->data)) {
107
			if (!Rules::isValid($this->data[$index], $rule)) {
108
				if (!$message) {
109
					$message = Rules::getError();
110
				}
111
				$this->errors[] = str_replace(':name', $name, $message);
112
			}
113
		}
114
115
		return !$this->hasError();
116
	}
117
118
	/**
119
	 * Retorna TRUE se tem algum erro
120
	 * @return bool
121
	 */
122
	public function hasError()
123
	{
124
		return count($this->errors) > 0;
125
	}
126
127
	/**
128
	 * Retorna a mensagem de erro
129
	 * @return string|null
130
	 */
131
	public function getError()
132
	{
133
		if (count($this->errors) > 0) {
134
			return $this->errors[0];
135
		}
136
137
		return null;
138
	}
139
}
140