-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractValidator.php
More file actions
127 lines (112 loc) · 2.16 KB
/
AbstractValidator.php
File metadata and controls
127 lines (112 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php declare(strict_types=1);
/**
* Part of Windwalker project.
*
* @copyright Copyright (C) 2019 LYRASOFT.
* @license LGPL-2.0-or-later
*/
namespace Windwalker\Validator;
/**
* The AbstractValidator class.
*
* @since 2.0
*/
abstract class AbstractValidator implements ValidatorInterface
{
/**
* Property error.
*
* @var string
*/
protected $error = '';
/**
* Property message.
*
* @var string
*/
protected $message = '';
/**
* Property multiple.
*
* @var boolean
*/
protected $multiple = false;
/**
* Validate this value and set error message..
*
* @param mixed $value
*
* @return boolean
*/
public function validate($value)
{
if (!$this->test($value)) {
// TODO: Use exception after 4.0
$this->setError($this->formatMessage($this->getMessage(), $value));
return false;
}
return true;
}
/**
* Test value and return boolean
*
* @param mixed $value
*
* @return boolean
*/
abstract protected function test($value);
/**
* Get error message.
*
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* Method to set property error
*
* @param string $error
*
* @return static Return self to support chaining.
*/
public function setError($error = null)
{
$this->error = $error ?: $this->getMessage();
return $this;
}
/**
* Set error message.
*
* @param string $message
*
* @return static
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* Method to get property Message
*
* @return string
*/
protected function getMessage()
{
return $this->message;
}
/**
* formatMessage
*
* @param string $message
* @param mixed $value
*
* @return string
*/
protected function formatMessage($message, $value)
{
return $message;
}
}