/
AbstractRule.php
183 lines (148 loc) · 4.27 KB
/
AbstractRule.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare(strict_types = 1);
namespace Elie\Validator\Rule;
abstract class AbstractRule implements RuleInterface
{
/** @var int|string */
protected $key;
/** @var mixed */
protected $value;
/**
* Trim value before validation, if value is string.
* Default sets to true.
* @var bool
*/
protected $trim = true;
/**
* Contains the error text to be displayed in case an error was found.
* @var string
*/
protected $error = '';
/**
* List of default message patterns in case of error.
* supported default keys are %code%, %key% and %value%
*
* @var array
*/
protected $messages = [
self::UNDEFINED_CODE => 'Code message %code% is undefined',
self::EMPTY_KEY => '%key% is required and should not be empty: %value%',
];
/**
* If key's context is required or not.
* If required, value should not be empty.
* Default sets to false.
* @var bool
*/
protected $required = false;
/**
* Default constructor to set common params.
*
* @param int|string $key Key is used in the error message.
* @param mixed $value Value to be validated.
* @param array $params Default params.
*/
public function __construct($key, $value, array $params = [])
{
$this->key = $key;
if (isset($params[$this::REQUIRED])) {
$this->required = (bool) $params[$this::REQUIRED];
}
// trim constant is not available in all rules
if (isset($params['trim'])) {
$this->trim = (bool) $params['trim'];
}
if (isset($params[$this::MESSAGES])) {
// replace existent by given messages
$this->messages = array_merge($this->messages, $params[$this::MESSAGES]);
}
$this->setValue($value);
}
public function getKey()
{
return $this->key;
}
public function getValue()
{
return $this->value;
}
public function getError(): string
{
return $this->error;
}
public function validate(): int
{
$this->error = '';
if (! $this->isEmpty()) {
// Value is not empty so keep checking
return $this::CHECK;
}
if ($this->isRequired()) {
// Value is empty and required.
return $this->setAndReturnError($this::EMPTY_KEY);
}
// Value is empty but not required.
return $this::VALID;
}
public function setValue($value): void
{
if (is_string($value) && $this->trim) {
$value = trim($value);
}
$this->value = $value;
}
/**
* If value is required, it should not be empty.
* {@link error} is set to blank.
*
* @return bool True means that value is required and empty.
*/
protected function isRequired(): bool
{
return $this->required;
}
/**
* Empty value is null or '' only.
*
* @return bool
*/
protected function isEmpty(): bool
{
return $this->value === null || $this->value === '';
}
/**
* @return int RuleInterface::ERROR
*/
protected function setAndReturnError(string $errorCode, array $replace = []): int
{
$message = $this->messages[$errorCode] ?? $this->messages[$this::UNDEFINED_CODE];
// + is used to add non existent keys
$replace += [
'%key%' => $this->key,
'%value%' => $this->stringify($this->value),
'%code%' => $errorCode,
];
$this->error = str_replace(array_keys($replace), $replace, $message);
return RuleInterface::ERROR;
}
protected function stringify($value): string
{
if (is_object($value) && ! in_array('__toString', get_class_methods($value))) {
return get_class($value) . ' object';
}
if (is_array($value)) {
return str_replace(["\n", "\r"], '', var_export($value, true));
}
return $this->fromScalar($value);
}
protected function fromScalar($value): string
{
if (is_bool($value)) {
return $value ? '<TRUE>' : '<FALSE>';
}
if ($value === null) {
return '<NULL>';
}
return (string) $value;
}
}