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

Rules::required()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Win\Validation;
4
5
class Rules
6
{
7
	private static $error;
8
9
	public static function isValid($value, $rule)
10
	{
11
		static::$error = null;
0 ignored issues
show
Bug introduced by
Since $error is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $error to at least protected.
Loading history...
12
		$ruleParam = explode(':', $rule);
13
		$ruleName = array_shift($ruleParam);
14
		$method = Rules::class . '::' . $ruleName;
15
		array_unshift($ruleParam, $value);
16
		call_user_func_array($method, $ruleParam);
17
18
		return is_null(static::$error);
19
	}
20
21
	/**
22
	 * Valida se o campo foi preenchido
23
	 * @param string $value
24
	 */
25
	protected static function required($value)
26
	{
27
		if (strlen($value) < 1) {
28
			static::setError('O campo :name é obrigatório.');
29
		}
30
	}
31
32
	/**
33
	 * Valida se é um email
34
	 * @param string $email
35
	 */
36
	protected static function email($email)
37
	{
38
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
39
			static::setError('O campo :name precisa ser um e-mail válido.');
40
		}
41
	}
42
43
	/**
44
	 * Valida se é um inteiro
45
	 * @param mixed $value
46
	 */
47
	protected static function int($value)
48
	{
49
		if (!is_int($value)) {
50
			static::setError('O campo :name precisa ser um número.');
51
		}
52
	}
53
54
	/**
55
	 * Valida se é o valor mínimo esperado
56
	 * @param mixed $value
57
	 * @param int $min
58
	 */
59
	protected static function min($value, $min)
60
	{
61
		if ($value < $min) {
62
			static::setError(
63
				'O campo :name precisa ser maior do que ' . $min . '.'
64
			);
65
		}
66
	}
67
68
	/**
69
	 * Valida se é o valor máximo esperado
70
	 * @param mixed $value
71
	 * @param int $max
72
	 */
73
	protected static function max($value, $max)
74
	{
75
		if ($value > $max) {
76
			static::setError(
77
				'O campo :name precisa ser menor do que ' . $max . '.'
78
			);
79
		}
80
	}
81
82
	/**
83
	 * Valida se a opção foi marcada
84
	 * @param bool $value
85
	 */
86
	protected static function checked($value)
87
	{
88
		if (false === (bool) $value) {
89
			static::setError('Marque a opção ":name".');
90
		}
91
	}
92
93
	/**
94
	 * Retorna o Erro obtido durante a validação
95
	 * @return string
96
	 */
97
	public static function getError()
98
	{
99
		return static::$error;
0 ignored issues
show
Bug introduced by
Since $error is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $error to at least protected.
Loading history...
100
	}
101
102
	/**
103
	 * Atribui o erro
104
	 * @param string $error
105
	 */
106
	protected static function setError($error)
107
	{
108
		static::$error = $error;
0 ignored issues
show
Bug introduced by
Since $error is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $error to at least protected.
Loading history...
109
	}
110
}
111